Description

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"

Challenge

Rotate in-place with O(1) extra memory.

Solution

     /**
* @param str: An array of char
* @param offset: An integer
* @return: nothing
*/
void reverseString(string &str, int start, int end)
{
int swap = ; while(start < end){
// Exchange the element between the start and the end.
swap = str[start];
str[start] = str[end];
str[end] = swap; // Move to the next index.
start++; end--;
} return ;
} void rotateString(string &str, int offset) {
// Get the length of the string.
int len = str.length();
if( !len || offset <= ) return ;
if( offset > len ) offset %= len; // Reverse the first section.
reverseString(str, , len-offset-);
// Reverse the second section.
reverseString(str, len-offset, len-);
// Reverse the entire section.
reverseString(str, , len-); return ;
}

[Algorithm] 8. Rotate String的更多相关文章

  1. Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  2. Lintcode: Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

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

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  4. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  5. 8. Rotate String

    8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...

  6. 【Leetcode_easy】796. Rotate String

    problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...

  7. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  8. [Swift]LeetCode796. 旋转字符串 | Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  9. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

随机推荐

  1. 【OI】单调队列

    所谓单调队列,就是一个保持着某种性质的队列,通常是队列从队头到队尾,维护一种递增递减的关系. 这种队列通常用来解决一些连续区间的最值问题. 这种队列的入队要保证符合当前的性质,例如一个递增的单调序列( ...

  2. Bootstrap popover源码分析

    /* ======================================================================== * Bootstrap: popover.js ...

  3. YTU 2631: B1 能存各种类型数据的Store类

    2631: B1 能存各种类型数据的Store类 时间限制: 1 Sec  内存限制: 128 MB 提交: 245  解决: 177 题目描述 有一种类,海纳百川,可以对任意类型的数据进行存取,造就 ...

  4. MongoDB全文搜索——目前尚不支持针对特定field的搜索

    > db.articles.createIndex( { subject: "text" } ) { "createdCollectionAutomatically ...

  5. POJ1050 To the Max 最大子矩阵

    POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...

  6. bzoj3262 陌上花开——CDQ分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3262 第一道CDQ分治题! 看博客:https://www.cnblogs.com/Narh ...

  7. C#面向过程之冒泡排序

    //定义一个数组,准备冒泡排序 ,,-,,,,-,}; //定义一个中间变量 ; //n个数字比较需要进行n-1次比较 ; j < arr.Length - - i; j++) { //每一趟的 ...

  8. 【175】Easy CHM的使用

    首先下载软件,EasyCHM3.84完美破解版.rar! 安装好之后,打开程序,点击“新建”,然后浏览到存放 htm 文件的目录. 鼠标右键,选择“添加目录项”!如下图所示. 在弹出的文本框中,首先选 ...

  9. 【145】◀▶ .NET Framework类库索引

    C#编程基础: A1 ………… 基础A2 ………… using 关键字A3 ………… as 关键字A4 ………… is 关键字A5 ………… switch 关键字A6 ………… return 语句关键 ...

  10. CodeForces 731B Coupons and Discounts (水题模拟)

    题意:有n个队参加CCPC,然后有两种优惠方式,一种是一天买再次,一种是买两天,现在让你判断能不能找到一种方式,使得优惠不剩余. 析:直接模拟,如果本次是奇数,那么就得用第二种,作一个标记,再去计算下 ...