LintCode: Rotate String

C++,

time: O(n)

space:O(n)

 1 class Solution {
 2 public:
 3     /**
 4      * @param str: a string
 5      * @param offset: an integer
 6      * @return: nothing
 7      */
 8     void rotateString(string &str, int offset) {
 9         // wirte your code here
10         // empty string->return
11         if ("" == str) {
12             return;
13         }
14         // offset is 0, return
15         int size = str.size();
16         offset = offset%size;
17         if (0 == offset) {
18             return;
19         }
20         // tmp = str+str->substr
21         string tmp(str);
22         tmp.reserve(size<<1);
23         tmp.insert(tmp.end(), str.begin(), str.end());
24         str = tmp.substr(size-offset, size);
25     }
26 };

C++,

time:O(n)

space:O(1)

 1 class Solution {
 2 public:
 3     /**
 4      * @param str: a string
 5      * @param offset: an integer
 6      * @return: nothing
 7      */
 8     void rotateString(string &str, int offset) {
 9         // wirte your code here
10         // empty string->return
11         if ("" == str) {
12             return;
13         }
14         // offset is 0, return
15         int size = str.size();
16         offset = offset%size;
17         if (0 == offset) {
18             return;
19         }
20         // 3-steps reverse
21         reverse(str, 0, size-offset-1);
22         reverse(str, size-offset, size-1);
23         reverse(str, 0, size-1);
24     }
25     void reverse(string &str, int from, int to) {
26         char tmp;
27         while( from<to ) {
28             tmp = str[from];
29             str[from] = str[to];
30             str[to] = tmp;
31             from++;
32             to--;
33         }
34     }
35 };
时间: 2024-10-23 19:36:43

LintCode: Rotate String的相关文章

Rotate String

Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given "abcdefg". offset=0 => "abcdefg" offset=1 => "gabcdef" offset=2 => "fgabcde" offset=3 => "efgabcd&quo

LeetCode算法题-Rotate String(Java实现)

这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是796).给定两个字符串A和B,在A上进行移位操作,规则是将A最左边的字符移动到最右边去.例如,如果A ='abcde',那么在A上移位一次后,它将是'bcdea'.当且仅当A在A上移位一定次数后可以变为B时返回True.

LeetCode 796. Rotate String

原题链接在这里:https://leetcode.com/problems/rotate-string/ 题目: We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' aft

[LintCode] Scramble String 爬行字符串

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

LintCode Interleaving String

Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Example For s1 = "aabcc", s2 = "dbbca" When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. For

796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

[LeetCode] Rotate String 旋转字符串

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

LeetCode题解之Rotate String

1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 1 bool rotateString(string A, string B) { 2 if( A.size() != B.size() ) 3 return false; 4 if( A.empty() && B.empty() ) 5 return true; 6 int i = 0 ; 7 while( i < A.size() ){ 8 char c = A[0] ; 9 A += c; 10 A.er

796. Rotate String旋转字符串

[抄题]: We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can