Rotate String

原创 2018年04月16日 13:40:46

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"
这个题目比较简单的,但是没有考虑全面,忽略了两个点,一个是空字符串,一个是当数值超过数值长度,写下纪念
class Solution {
public:
    /**
     * @param str: An array of char
     * @param offset: An integer
     * @return: nothing
     */
    void rotateString(string &str, int offset) {
        if(!str.empty()){
        int newoffset=offset%str.length();
        string str1(str, str.length() - newoffset, str.length() );
		str.erase(str.length() - newoffset, str.length());
		str = str1 + str;
        }
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。如果您有更好的想法,请留言 https://blog.csdn.net/qq_34552886/article/details/79959517

[Lintcode]Rotate String 两步翻转法

两步翻转法 public class Solution { /** * @param str: an array of char ...
  • jc69186918
  • jc69186918
  • 2016-04-10 18:43:50
  • 836

[Leetcode] 796. Rotate String 解题报告

题目: We are given two strings, A and B. A shift on A consists of taking string A and moving...
  • magicbean2
  • magicbean2
  • 2018-03-30 20:08:14
  • 24

【LeetCode】796. Rotate String

题目:思路:这道题目关键在思路吧,没什么想法的话,这里先给一个提示:string str = A + A,先思考一下然后再看答案代码。下面贴代码,一些解释写在代码注释里#include &lt...
  • Mint_r
  • Mint_r
  • 2018-03-13 17:55:35
  • 62

左旋转字符串 left rotate string

左旋转字符串 题目:字符串的左旋转操作:把字符串前面的若干字符移到字符串的后面。例如:字符串abcdefg左旋转2位得到cdefgab。请实现左旋转字符串函数,要求对于长度为n的字符串,时间复杂度为...
  • jiyanfeng1
  • jiyanfeng1
  • 2012-08-24 04:46:21
  • 4889

python--leetcode796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the left...
  • wenqiwenqi123
  • wenqiwenqi123
  • 2018-03-19 20:23:30
  • 31

189. Rotate Array [easy] (Python)

题目链接https://leetcode.com/problems/rotate-array/题目原文 Rotate an array of n elements to the right by ...
  • coder_orz
  • coder_orz
  • 2016-07-28 10:32:27
  • 1727

Lintcode8 Rotate String solution 题解

【题目描述】 Given a string and an offset, rotate string by offset. (rotate from left to right) ...
  • jane_silva
  • jane_silva
  • 2017-04-05 12:41:20
  • 213

【LintCode】Rotate String 旋转字符串

给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)样例 对于字符串 “abcdefg”.offset=0 => “abcdefg” offset=1 => “gabcdef” of...
  • wutingyehe
  • wutingyehe
  • 2015-07-14 22:36:06
  • 2289

#8 Rotate String

题目描述: Given a string and an offset, rotate string by offset. (rotate from left to right) ...
  • haifischxia
  • haifischxia
  • 2016-08-17 11:33:07
  • 112
收藏助手
不良信息举报
您举报文章:Rotate String
举报原因:
原因补充:

(最多只允许输入30个字)