成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Rotate String [周而復(fù)始]

CarlBenjamin / 654人閱讀

Problem

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.

Note

完成challenge,三步翻轉(zhuǎn)法。見(jiàn)Sol.2。

Solution

1.

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)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65457.html

相關(guān)文章

  • [LintCode/LeetCode] Rotate Array

    Problem Given an array, rotate the array to the right by k steps, where k is non-negative. Example Example 1: Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the r...

    chanthuang 評(píng)論0 收藏0
  • [LintCode/LeetCode] Rotate Image

    摘要:兩種方法,轉(zhuǎn)置鏡像法和公式法。首先看轉(zhuǎn)置鏡像法原矩陣為轉(zhuǎn)置后水平鏡像翻轉(zhuǎn)后所以,基本的思路是兩次遍歷,第一次轉(zhuǎn)置,第二次水平鏡像翻轉(zhuǎn)變換列坐標(biāo)。公式法是應(yīng)用了一個(gè)翻轉(zhuǎn)的公式如此翻轉(zhuǎn)四次即可。二者均可,并無(wú)分別。 Problem You are given an n x n 2D matrix representing an image.Rotate the image by 90 de...

    BenCHou 評(píng)論0 收藏0
  • [LintCode/LeetCode] Rotate List

    摘要:而后吾當(dāng)依除取余之法,化大為小,則指針不致于越界也。后欲尋右起第結(jié)點(diǎn),令快指針先行數(shù)日,及至兩指針相距為,便吟鞭東指,與慢指針策馬共進(jìn)??炻羔樢嘀褂谄渌伞N鑴?dòng)長(zhǎng)劍,中宮直入,直取首級(jí),而一掌劈空,已鴻飛冥冥。自此,一代天驕,霸業(yè)已成。 Problem Given a list, rotate the list to the right by k places, where k is...

    Blackjun 評(píng)論0 收藏0
  • SVG + CSS 實(shí)現(xiàn) Material Design Loading

    摘要:在研究的過(guò)程中,發(fā)現(xiàn)有大神用在上實(shí)現(xiàn)了它。由制定,是一個(gè)開(kāi)放標(biāo)準(zhǔn)。省略這時(shí),你就能看到線段周而復(fù)始地從一根細(xì)線變?yōu)橐粋€(gè)圓圈。這次感覺(jué)是不是很相像了,只是現(xiàn)在它的開(kāi)口一直處于一個(gè)位置,就沒(méi)什么魔性了。 showImg(http://upload-images.jianshu.io/upload_images/1258730-02e5f2eca07eaa59.gif?imageMogr2/...

    txgcwm 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<