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. Windows 点击 模拟

    点击模拟  安卓模拟器  爬虫

  2. 爬虫定时任务 redis 减轻 mysql 读的压力 加层

    非工作时间,定时任务爬虫大量mysq短链接,影响了其他业务的,mysql 报 too many connections  错误 将爬虫url池放入到redis中,单独的脚本维护redis url池的更 ...

  3. text recognizer (OCR) Engine 光学字符识别

    https://github.com/tesseract-ocr/tesseract/wiki https://github.com/UB-Mannheim/tesseract/wiki C:\Use ...

  4. node.js开发错误——DeprecationWarning: Mongoose: mpromise

    原文地址 使用mongoose进行数据库操作时,总是提示: (node:5684) DeprecationWarning: Mongoose: mpromise (mongoose's default ...

  5. E20170611-hm

    ascending   adj. 上升的,向上的; ascend   vt. 攀登; 继承; 占领;   vi. 上升; 爬坡; 追溯; descending  n. 递减;  descend   v ...

  6. HTTP请求 响应状态码

      1.请求响应方式       get是从服务器上获取数据,post是向服务器传送数据.get 数据在地址栏中明文的形式发送, post则不是,传递的数据比get多 据RFC2616标准(现行的HT ...

  7. bzoj 1029: [JSOI2007]建筑抢修【贪心+堆】

    第一想法是按照结束时间贪心,但是这样有反例 所以先按照t贪心,能选则选,把选的楼的持续时间放进大根堆里,当当前的楼不能选的时候如果当前的持续时间比大根堆里最大的要小,就用这个替换最大,这样总数不变但是 ...

  8. bzoj 2730: [HNOI2012]矿场搭建【tarjan】

    先tarjan找割点和点双连通分量,然后对一个点双,如果没有割点,那么需要建立两个出口(割掉一个另一个备用):如果只有一个割点,出口可以设立在任意一个非割点的地方:如果有两个及以上个割点,就不用建出口 ...

  9. 洛谷 P2365 任务安排【dp】

    其实是可以斜率优化的但是没啥必要 设st为花费时间的前缀和,sf为Fi的前缀和,f[i]为分组到i的最小花费 然后枚举j转移,考虑每次转移都是把j到i分为一组这样意味着j及之后的都要增加s的时间,同时 ...

  10. CSS实现居中的方式

    在介绍居中方式之前,简单介绍一下行内元素和块级元素. 行内元素 和其他元素都在同一行 高,行高及外边距和内边距部分可以改变 宽度只与内容有关 行内元素只能容纳文本或者其他行内元素 常用内联元素:a,i ...