leetcode Reverse String II 反转字符串
来源:互联网 发布:北大光华mba知乎 编辑:程序博客网 时间:2024/06/08 11:10
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:
1、The string consists of lower English letters only.
2、Length of the given string and k will in the range [1, 10000]
题意:反转字符串s中下标为2k的倍数位置开始的长度为k的字符串
思路:一层循环找位置,一层循环反转字符串
class Solution {public: string reverseStr(string s, int k) { for (int left = 0; left <s.size(); left += 2 * k) { for (int i = left, j = min(left + k - 1, (int)s.size() - 1); i < j; ++i, --j) { swap(s[i], s[j]); } } return s; }};
阅读全文
0 0
- leetcode Reverse String II 反转字符串
- leetcode 541. Reverse String II 反转字符串
- [LeetCode]541. Reverse String II(间隔反转字符串)
- 字符串反转--Leetcode Reverse String
- LeetCode Reverse String(字符串反转)
- [Leetcode,python] Reverse String 反转字符串
- LeetCode----344. Reverse String 字符串反转
- [LeetCode]344. Reverse String(反转字符串)
- leetcode 344. Reverse String 反转字符串
- Leetcode 344 Reverse String 反转字符串
- LeetCode--Reverse String 反转字符串(Python)
- leetcode -- 541. Reverse String II 【字符串反转 + 双指针 + 状态记录 + 数组与堆内存】
- Reverse String 字符串反转
- 字符串-leetcode 541. Reverse String II
- LeetCode Reverse Words in a String(反转字符串)
- LeetCode Reverse Words in a String(反转字符串)
- 字符串反转 reverse-words-in-a-string @LeetCode
- 【LeetCode】字符串反转函数的几个算法 Reverse String
- C语言字符串处理的一些函数strok,strstr, strchr,strsub
- UVALive
- Java WebService 简单实例(有图)
- 【PHP】使用MySQLi方式连接数据库,增删改查
- CEOI2017 day1-Sure Bet【三分】
- leetcode Reverse String II 反转字符串
- F:\workspace-maven\e3-manager\e3-manager-interface\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)
- EsLint入门
- ubuntu如何安装libz库
- 线程同步辅助类(一)
- Java字节码框架ASM-读写字节码的用法
- 视差与深度信息
- ml_svm
- Android 端天气预报APP的实现(二)阿里云天气预报API的获取