[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 ...
随机推荐
- Django编写RESTful API(四):认证和权限
欢迎访问我的个人网站:www.comingnext.cn 前言: 按照前面几篇文章里那样做,使用Django编写RESTful API的基本功能已经像模像样了.我们可以通过不同的URL访问到不同的资源 ...
- Servlet的执行流程、生命周期
下面这幅图的Request和Response的箭头方向反了,应该是客户端发出请求,然后web服务器返回响应. servlet生命周期阶段包括初始化.加载.实例化.服务和销毁. 编写Servlet的d ...
- Maven 中央仓库及阿里云仓库地址
Maven 中央仓库地址: 1. http://www.sonatype.org/nexus/ 2. http://mvnrepository.com/ 3. http://repo1.maven.o ...
- 【C#多线程编程实战笔记】一、 线程基础
创建线程 Thread :所执行的方法不能有参数. class Program { static void Main(string[] args) { Console.WriteLine(" ...
- [ASP.NET MVC]笔记(四 UnobtruSive AJAX和客户端验证
UnobtruSive AJAX和客户端验证 ASP.NET MVC 已经默认开启非侵入试js和客户端验证,在web.config可以看到如下配置: <configuration> < ...
- JDBC(一)之细说JDBC
Properties info = new Properties();//要参考数据库文档 info.setProperty("user", "root"); ...
- Sublime Text 安装插件
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等.还可自定义键绑定,菜单和工具栏.Sublime Text 的主要功能包括:拼写检查,书签,完整的 ...
- Android微信登录、分享、支付
转载需要著名出处: http://blog.csdn.net/lowprofile_coding/article/details/78004224 之前写过微信登录分享支付第一版: http://bl ...
- 转:【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派
转载请注明出处:http://blog.csdn.net/ns_code/article/details/17965867 方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法 ...
- swing-窗体添加背景图片的2种方法
在美化程序时,常常需要在窗体上添加背景图片.通过搜索和测试,发现了2种有效方式.下面分别介绍.1.利用JLabel加载图片利用JLabel自带的setIcon(Icon icon)加载icon,并设置 ...