[LeetCode] 344 Reverse String && 541 Reverse String II
原题地址:
344 Reverse String:
https://leetcode.com/problems/reverse-string/description/
541 Reverse String II:
https://leetcode.com/problems/reverse-string-ii/description/
题目&&解法:
1.Reverse String:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
这个直接上代码就行了,关于字符串翻转,不管字符数目是奇数还是偶数,都是一样的方法(当然可以调用库函数):
class Solution {
public:
string reverseString(string s) {
int size = s.size();
for (int i = ; i <= (size - ) / ; i++) {
int temp = s[i];
s[i] = s[size - i - ];
s[size - i - ] = temp;
}
return s;
}
};
2. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Restrictions:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
也是很简单的一道题目,我的做法是这样的:先对前面满足2k的部分进行前k位的翻转,剩余不足的进行讨论,确认有几位需要翻转:
class Solution {
public:
string reverseStr(string s, int k) {
int double_k = * k;
int m = s.size() / double_k;
int n = s.size() % double_k; //剩余部分
for (int i = ; i < m; i++) {
for (int j = ; j <= (k - ) / ; j++) {
char temp = s[i * double_k + j];
s[i * double_k + j] = s[double_k * i + k - j - ];
s[double_k * i + k - j - ] = temp;
}
}
if (n == ) return s;
int end = n >= k ? k : n;
for (int j = ; j <= (end - ) / ; j++) {
char temp = s[m * double_k + j];
s[m * double_k + j] = s[double_k * m + end - j - ];
s[double_k * m + end - j - ] = temp;
}
return s;
}
};
[LeetCode] 344 Reverse String && 541 Reverse String II的更多相关文章
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- LeetCode 541. 反转字符串 II(Reverse String II)
541. 反转字符串 II 541. Reverse String II
- [LeetCode] 344. Reverse String 翻转字符串
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- LeetCode 344. Reverse String(反转字符串)
题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...
- Leetcode 344:Reverse String 反转字符串(python、java)
Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
随机推荐
- 读书笔记 之 《阿里巴巴Java开发手册》
一.前言 这本书主要定义了一些代码的规范以及一些注意事项.我只根据我自己的不足,摘录了一些内容,方便以后查阅. 二.读书笔记 命名 1.代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符 ...
- ASP.NET Core MVC – Tag Helper 组件
ASP.NET Core Tag Helpers系列目录,这是第五篇,共五篇: ASP.NET Core MVC – Tag Helpers 介绍 ASP.NET Core MVC – Caching ...
- Java中面向字符的输入流
Java中面向字符的输入流 2016-12-04 Java程序员联盟 Java程序员联盟 Java程序员联盟 微信号 javalm 功能介绍 莫道君行早,更有早行人 全心敲代码,天道自酬勤 字符流是针 ...
- 微信JS-API封装接口——node.js版
github:https://github.com/xjnotxj/wechat_interaction_jsapi Wechat JS-API接口 功能: 用于管理和获取微信 JSSDK 生产的ac ...
- 源码编译安装bind
author:JevonWei 版权声明:原创作品 编译bind 准备阶段: 下载bind软件包,然后传输到系统中 https://www.isc.org/downloads/ 安装开发包组 yum ...
- [Unity 设计模式]桥接模式(BridgePattern)
1.前言 继上一讲IOC模式的基础上继续本讲桥接模式,笔者感觉桥接模式是23种设计模式中桥接模式是最好用但也是最难理解的设计模式之一,23中设计模式就好武侠剧中一本武功秘籍,我们在工作过程中想要熟练运 ...
- centos6.6配置rsync+sersync实现实时同步分布式多客户端分发同步
1.sersync项目: sersync项目利用inotify与rsync技术实现对服务器数据实时同步到解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rsync是目 ...
- 第一周作业.四则运算生成器(基于python)
题目 从<构建之法>第一章的 "程序" 例子出发,像阿超那样,花二十分钟写一个能自动生成小学四则运算题目的命令行 "软件",满足以下需求: 除了整数 ...
- 201521123039 《java程序设计》第八周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 总结: 1.集合可以动态修改大小,但是不可以存放基本数据类型: 2.java中任何对象都是is-a Objec ...
- 201521123113《Java程序设计》第7周学习总结
1. 本周学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 contains源代码: public boolean contains( ...