LeetCode 541 Reverse String II
2018-01-14 17:08
363 查看
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:
Restrictions:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]
给一个字符串以及整数k,每2k个分一组,前k个倒置后k个不变。
不得不说python大法好。
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]
给一个字符串以及整数k,每2k个分一组,前k个倒置后k个不变。
不得不说python大法好。
class Solution(object): def reverseStr(self, s, k): """ :type s: str :type k: int :rtype: str """ return ''.join(s[begin:begin+k][::-1]+s[begin+k:begin+2*k] for begin in xrange(0,len(s),2*k))
相关文章推荐
- leetcode541 Reverse StringII java
- leetcode 541. Reverse String II 反转字符串
- [leetcode]541. Reverse String II
- [leetcode]: 541. Reverse String II
- LeetCode 541. Reverse String II (反转字符串 II)
- Leetcode 541. Reverse String II(Easy)
- [LeetCode]541. Reverse String II(间隔反转字符串)
- LeetCode 541:Reverse String II (c++)
- LeetCode:541. Reverse String II
- The Solution to Leetcode 541 Reverse String II
- [LeetCode] 344 Reverse String && 541 Reverse String II
- leetcode 541 Reverse String II C++
- LeetCode 541. Reverse String II (字符串翻转)
- LeetCode 541. Reverse String II(C++版)
- Leetcode OJ Contest23 Reverse String II_Leetcode541
- Leetcode #541 Reverse String II
- leetcode -- 541. Reverse String II 【字符串反转 + 双指针 + 状态记录 + 数组与堆内存】
- LeetCode-541. Reverse String II (Java)
- leetcode 541. Reverse String II(easy)
- [Leetcode] 541. Reverse String II 解题报告