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"
offset=4 => "defgabc"
offset=5 => "cdefgab"
offset=6 => "bcdefga"
offset=7 => "abcdefg"
public class Solution {
    /**
     * @param str: an array of char
     * @param offset: an integer
     * @return: nothing
     */
    public void rotateString(char[] str, int offset) {
        // write your code here
        if (str == null || str.length == 0) return;
        
        int len = str.length;
        offset = offset % len;
        
        char[] temp = new char[offset];
        
        for (int i = 0; i < offset; i++) {
            temp[i] = str[str.length-1-i];
        }
        
        for (int i = 0; i<str.length - offset; i++) {
            str[str.length -1 - i] = str[str.length -1 - offset - i];
        }
        
        for (int i = 0; i < offset; i++) {
            str[i] = temp[offset- 1 -i];
        }
        
    }
}

public class Solution { 
    public void rotateString(char[] str, int offset) { 
        int len = str.length; 
        char[] newstr = new char[len+1]; 
        if (str == null || str.length == 0) return; 
        //modify offset if offset > length; 
        offset = offset % len; 
        for (int i = 0; i < offset; i++) { 
            newstr[i] = str[len-offset+i]; 
         } 
         for (int i = offset; i < len; i++) { 
            newstr[i] = str[i-offset]; 
        } 
        for (int i = 0; i < len; i++) { 
            str[i] = newstr[i]; 
        } 
        return; 
    }
}

三步翻轉(zhuǎn)法?O(1) extra memory

public class Solution { 
      public void rotateString(char[] str, int offset) { 
            if (str == null || str.length == 0) { return; } 
            int len = str.length; 
            offset = offset % len; 
            reverse(str, 0, len-offset-1); 
            reverse(str, len-offset, len-1); 
            reverse(str, 0, len-1); return; } 
     public void reverse(char[] str, int start, int end) { 
            while (start <= end) { 
                  char temp = str[start]; 
                  str[start] = str[end]; 
                  str[end] = temp; 
                  start++; 
                  end--; 
            } 
      }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,908評(píng)論 0 33
  • 【題目描述】 Given a string and an offset, rotate string by off...
    代碼碼著玩閱讀 376評(píng)論 0 0
  • 首先看一下南山型的腹部贅肉,也有很多人擁有這樣的腹部。 先要從有氧減肥運(yùn)動(dòng)開(kāi)始,之后再進(jìn)行肋骨兩側(cè)減肥。 肋骨兩側(cè)...
    掌上健身房GYM閱讀 278評(píng)論 0 1
  • 06 其實(shí)我倆基本每天都吵架,后來(lái)都不怎么說(shuō)話,一說(shuō)就罵人,不罵人不說(shuō)話,他罵我,我就罵他,也總是說(shuō)離婚,他也說(shuō)離...
    小小圣女果閱讀 273評(píng)論 0 2
  • 1.截取特定view 2.截取scrollView的所有,包括下面沒(méi)顯示的(UITabelview,UIWebVi...
    雪中客閱讀 480評(píng)論 0 1

友情鏈接更多精彩內(nèi)容