摘要:題目要求對(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
摘要:月下半旬攻略道題,目前已攻略題。目前簡(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ū)別...
摘要:在線網(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...
摘要:并查集包括查詢和聯(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)決定不同的...
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...
摘要:只有一個(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...
閱讀 3423·2021-11-24 09:38
閱讀 3198·2021-11-22 09:34
閱讀 2113·2021-09-22 16:03
閱讀 2374·2019-08-29 18:37
閱讀 383·2019-08-29 16:15
閱讀 1774·2019-08-26 13:56
閱讀 870·2019-08-26 12:21
閱讀 2210·2019-08-26 12:15