[leetcode-541-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]

思路:

以2*k 为一组,每一组前k个字符翻转,然后处理剩余字符。

写出来感觉好啰嗦。。另外简洁的代码看到大神有用stl的reverse的,回头试一下,省不少事儿。

string reverseStr(string s, int k)
     {
         int group = s.size()/(2*k);
         int i = 0;
         for (; i < group;i++)
         {
             for (int j = 0; j < k/2;j++)
             {
                 swap(s[i * 2 * k + j], s[i * 2 * k + k-j-1]);
             }
         }
         int remain = s.size() % (2 * k);
         int end = (remain >= k) ? k : remain ;
         for (int j = 0; j < end/2;j++)
         {
             swap(s[i * 2 * k + j], s[i * 2 * k + end - j - 1]);
         }
         return s;
     }
时间: 2024-08-09 22:00:19

[leetcode-541-Reverse String II]的相关文章

[LeetCode] 344 Reverse String &amp; 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

541. 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

[LeetCode] 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 eq

leetCode 344. Reverse String 字符串

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 思路1: 使用一个新的string来存放结果. class Solution { public:     string reverseString(string s) {        

【Leetcode】Reverse String

题目链接:https://leetcode.com/problems/reverse-string/ 题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 思路: easy 算法: public String reverseString(String s) { char

leetcode --344. Reverse String

Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 1 class Solution { 2 public: 3 string reverseString(string s) { 4 int i = 0 ; 5 int j = s.size()-1; 6 while(i<

LeetCode(69)-Reverse String

题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 思路: 题意:反转字符串 不用考虑为空的情况,这种情况sb.toString()也是null,倒叙遍历,StringBuffer相加 代码: public class Solution { public String

Python [Leetcode 344]Reverse String

题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 解题思路: 见代码. 代码如下: class Solution(object): def reverseString(self, s): """ :type s: str :rtype

[Algorithm] Reverse String II

给定一个字符串,要求把字符串前面的若干字符移动到字符串尾部. 解法一:蛮力法 首先想考虑将一个字符移到尾部的方法.代码如下: void LeftShiftOne(char* s, int n) { char t = s[0]; for (int i = 1; i != n; i++) s[i - 1] = s[i]; s[n - 1] = t; } 如果要移动m个字符串,则依次对函数LeftShiftOne执行m次即可.代码如下: void LeftRotateString(char* s, i

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman