[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 ...
随机推荐
- VisualSVN安装图解
VisualSVN安装教程... ----------------------------------- 参考网址:https://www.visualsvn.com/server/download/ ...
- java注释中使用注解@see
缘起 在写java时,有时需要写注释,而为了更好的描述,需要引用和参考其他代码.为了让阅读者更好的体验,javadoc中支持链接跳转,这就需要用到注解@see. @see用法 注解@see可以在注释中 ...
- 灾难恢复-boot分区的恢复方法
boot分区是系统启动中最重要的部分,如果服务器由于病毒攻击又或者被管理员误删除了boot分区.那么就会存在潜在的风险.为什么说是潜在的风险?因为boot分区被删除后系统仍在继续运行,看似无状况但是在 ...
- webpack常见的配置总结 ---只是一些常见的配置
早期的构建工具grunt ,gulp 帮助我们配置一些开发环境,省去一些我们调试和重复的工作 现在我们的构建工具一般是webpack ,目前建议大家用3.0以上的版本 现在市场上比较优秀的构建工具,个 ...
- java初步—参数的值传递
校招季,本人匆匆忙忙地参加各种宣讲会,几次笔试下来都遇到同一个题目,而且全都错在同一想法上,方知自己的基础实在不太牢固,因此特别写在博客上提醒自己要脚踏实地地学习!不多说了,题目如下: public ...
- 通过官网找到spring的jar包
1.官网为:https://spring.io/ 2.打开之后,点击:PROJECTS,如图所示: 3.点击第三个:SPRING FRAMEWORK,如图所示: 4.进入之后,找到features,点 ...
- MySQL(九)之数据表的查询详解(SELECT语法)二
上一篇讲了比较简单的单表查询以及MySQL的组函数,这一篇给大家分享一点比较难得知识了,关于多表查询,子查询,左连接,外连接等等.希望大家能都得到帮助! 在开始之前因为要多表查询,所以搭建好环境: 1 ...
- ajax请求成功前loading
1.jquery方式 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- 详解java设计模式之责任链模式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt175 从击鼓传花谈起 击鼓传花是一种热闹而又紧张的饮酒游戏.在酒宴上宾客依次 ...
- TitleLayout——一个Android轻松实现标题栏的库
TitleLayout 多功能.通用的.可在布局或者使用Java代码实现标题栏: 支持沉浸式状态栏: 支持左侧返回按钮不需要手动实现页面返回: 支持左侧按钮,中间标题,右边按钮点击 左侧支持图片+文字 ...