[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, int n, int m)
{
    while (m--)
        LeftShiftOne(s, n);
}

时间复杂度:将一个字符移动到尾部需要n次操作,如果要移动m个字符,则需要操作m * n次。所以时间复杂度为O(m * n)

空间复杂度:O(1)

解法二:三步反转法

将一个字符串分为2个部分,一部分为m个需要旋转的字符记为X(abc),另一部分后半部分为n - m个字符记为Y(defg)。将X,Y反转。得到(cba)(gfed),之后将(cba)(gfed)整体反转得到(defgabc)。相当巧妙的算法。代码如下:

void ReverseString(char* s, int left, int right)
{
    while (left < right)
        swap(s[left++], s[right--]);
}
void LeftRotateString(char* s, int n, int m)
{
    ReverseString(s, 0, m - 1);
    ReverseString(s, m, n - 1);
    ReverseString(s, 0, n - 1);
}

时间复杂度:每移动2个字符则要进行1次交换,所以时间复杂度正比与字符串的总数O(n)

空间复杂度:O(1)

举一反三

1. 链表反转。给定一个链表和一个数k

时间: 2024-12-21 11:46:11

[Algorithm] 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

[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

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 (自己的做法很笨)

Reverse String II 自己的解法: 就是用一个StringBuffer来进行字符串的接收,利用一个指针来指示当前是哪一个k部分,当 i + k < s.length() 时,证明从 i 到 i + k - 1部分是可以进行反转的,而从i + k 部分到 i + 2 * k部分是直接进行拼接,利用条件 j < s.length()即可进行限制,不过对于最后剩余的一小部分如果是需要反转的,那么就不能使用上面反转时使用的条件 : 从j = i + k - 1 ,而是从 j = s.le

[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 characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "t

[LeetCode] 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 left =

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.如果对指针操作还不熟练,可以通过另分配n个字节的内存来进行翻转.即将字符串从后向前读,然后从前向后保存在新内存中,再将指针指向新内存. 代码如下:(如果不把strlen(s)保

344. Reverse String【easy】

344. Reverse String[easy] 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 start = 0, e

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标