国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

leetcode443. String Compression

nifhlheimr / 1165人閱讀

摘要:題目要求對(duì)字符串進(jìn)行簡(jiǎn)單的壓縮操作,壓縮的規(guī)則是,如果出現(xiàn)多個(gè)重復(fù)的字母,則用字母加上字母出現(xiàn)的字?jǐn)?shù)進(jìn)行表示。如果字母只出現(xiàn)一次,則不記錄次數(shù)。

題目要求
Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

 
Follow up:
Could you solve it using only O(1) extra space?

 
Example 1:

Input:
["a","a","b","b","c","c","c"]

Output:
Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]

Explanation:
"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".
 

Example 2:

Input:
["a"]

Output:
Return 1, and the first 1 characters of the input array should be: ["a"]

Explanation:
Nothing is replaced.
 

Example 3:

Input:
["a","b","b","b","b","b","b","b","b","b","b","b","b"]

Output:
Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].

Explanation:
Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".
Notice each digit has it"s own entry in the array.
 

Note:

All characters have an ASCII value in [35, 126].
1 <= len(chars) <= 1000.

對(duì)字符串進(jìn)行簡(jiǎn)單的壓縮操作,壓縮的規(guī)則是,如果出現(xiàn)多個(gè)重復(fù)的字母,則用字母加上字母出現(xiàn)的字?jǐn)?shù)進(jìn)行表示。如果字母只出現(xiàn)一次,則不記錄次數(shù)。

思路和代碼

核心思路是用三個(gè)指針?lè)謩e記錄三個(gè)下標(biāo):
p1: 記錄壓縮后的內(nèi)容的插入下標(biāo)
p2: 記錄當(dāng)前相同字符串的起始位置
p3: 記錄當(dāng)前和起始位置比較的字符串的位置

一旦出現(xiàn)p3的值不等于p2或是p3的值大于字符數(shù)組的長(zhǎng)度,則將壓縮結(jié)果從p1開(kāi)始填寫(xiě),實(shí)現(xiàn)O(1)的空間復(fù)雜度。

    public int compress(char[] chars) {
        int p1 = 0;
        int p2 = 0;
        int p3 = 1;
        while(p2 < chars.length) {
            if(p3 >= chars.length || chars[p3] != chars[p2]) {
                int length = p3 - p2;
                chars[p1++] = chars[p2];
                if(length != 1) {
                    int count = 0;
                    while(length != 0) {
                        int num = length % 10;
                        for(int i = p1+count ; i>p1 ; i--) {
                            chars[i] = chars[i-1];
                        }
                        chars[p1] = (char)("0" + num);
                        length /= 10;
                        count++;
                    }
                    p1 += count;
                }
                p2 = p3;
            }
            p3++;

        }
        return p1;
    }

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/74119.html

相關(guān)文章

  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)

    摘要:月下半旬攻略道題,目前已攻略題。目前簡(jiǎn)單難度攻略已經(jīng)到題,所以后面會(huì)調(diào)整自己,在刷算法與數(shù)據(jù)結(jié)構(gòu)的同時(shí),攻略中等難度的題目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道題,目前已攻略 100 題。 一 目錄 不折騰的前端,和咸魚(yú)有什么區(qū)別...

    tain335 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語(yǔ)言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • Leetcode之Union-Find(并查集)

    摘要:并查集包括查詢和聯(lián)合,主要使用不相交集合查詢主要是用來(lái)決定不同的成員是否在一個(gè)子集合之內(nèi)聯(lián)合主要是用來(lái)把多個(gè)子集合成一個(gè)集合的實(shí)際運(yùn)用計(jì)算機(jī)網(wǎng)絡(luò)檢查集群是否聯(lián)通電路板檢查不同的電路元件是否聯(lián)通初始化聯(lián)通與檢測(cè)與是否聯(lián)通返回聯(lián)通的數(shù) 并查集(Union-Find)包括查詢(Find)和聯(lián)合(Union),主要使用不相交集合(Disjoint-Sets)查詢(Find)主要是用來(lái)決定不同的...

    roland_reed 評(píng)論0 收藏0
  • [LintCode] String Compression

    String Compression Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the compressed string w...

    Cruise_Chan 評(píng)論0 收藏0
  • [Leetcode] Graph Valid Tree 判斷一個(gè)圖是否為樹(shù)

    摘要:只有一個(gè)連通分量還沒(méi)有環(huán),就是樹(shù),無(wú)根樹(shù)。無(wú)向圖邊的兩端是對(duì)稱(chēng)的,無(wú)向圖講究連通這個(gè)概念,沒(méi)有方向,沒(méi)有拓?fù)洌芟窦希苑浅_m合用并查集來(lái)解決。并查集寫(xiě)法參考注意方法用找的老大哥,合并的是的老大哥。 Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each...

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

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

0條評(píng)論

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