摘要:反轉(zhuǎn)字符法復(fù)雜度時(shí)間空間思路因?yàn)閿?shù)字不好從前向后遍歷每一位要先統(tǒng)計(jì)一共有多少位,比較麻煩,所以我們直接從后向前計(jì)數(shù),最后把結(jié)果倒置就行了。
Count Consecutive Digits in Integer
反轉(zhuǎn)字符法 復(fù)雜度Count consecutive digits and say it. For example, return 132341 if input is 1112224. There are three 1s, three 2s and one 4.
時(shí)間 O(N) 空間 O(1)
思路因?yàn)閿?shù)字不好從前向后遍歷每一位(要先統(tǒng)計(jì)一共有多少位,比較麻煩),所以我們直接從后向前計(jì)數(shù),最后把結(jié)果倒置就行了。
注意退出循環(huán)后還要額外執(zhí)行一次append,將最后的連續(xù)數(shù)字補(bǔ)齊
代碼public String countAndSay(int n) { if(n <= 0) return ""; int last = n % 10, cnt = 1; n = n / 10; StringBuilder sb = new StringBuilder(); while(n > 0){ int digit = n % 10; if(digit == last){ cnt++; } else { sb.append(cnt); sb.append(last); cnt = 1; last = digit; } n = n / 10; } sb.append(cnt); sb.append(last); sb.reverse(); return sb.toString(); }Count and Say
遞歸法 復(fù)雜度The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
時(shí)間 O(N) 空間 O(N) 遞歸??臻g
思路因?yàn)榈趎個(gè)數(shù)count and say的結(jié)果是基于第n-1個(gè)數(shù)的,我們可以用遞歸解決這個(gè)問題。
代碼public class Solution { public String countAndSay(int n) { if(n == 0){ return ""; } if(n == 1){ return "1"; } String s = countAndSay(n-1); char last = s.charAt(0); int cnt = 1; StringBuilder sb = new StringBuilder(); for(int i = 1; i < s.length(); i++){ if(s.charAt(i)==last){ cnt++; } else { sb.append(cnt); sb.append(last); cnt = 1; last = s.charAt(i); } } sb.append(cnt); sb.append(last); return sb.toString(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64517.html
摘要:遞歸解法復(fù)雜度時(shí)間空間遞歸棧思路該序列又叫做外觀序列,無論如何我們都得將前一個(gè)序列元素算出來,才能計(jì)算后一個(gè)序列元素。當(dāng)遞歸至的時(shí)候返回初始數(shù)字。另外,比如初始數(shù)字,第一次變成了,我們可以發(fā)現(xiàn)大于的數(shù)都只會(huì)一個(gè)一個(gè)出現(xiàn)了。 Count And Say The count-and-say sequence is the sequence of integers beginning as...
摘要:題目要求英文的題目有點(diǎn)繞口,所以去網(wǎng)上找了一下題目的意思。題目的核心邏輯在于將口語化的數(shù)數(shù)字轉(zhuǎn)化為字符串。 題目要求 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as one 1 or 11...
摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...
摘要:微信公眾號(hào)記錄截圖記錄截圖目前關(guān)于這塊算法與數(shù)據(jù)結(jié)構(gòu)的安排前。已攻略返回目錄目前已攻略篇文章。會(huì)根據(jù)題解以及留言內(nèi)容,進(jìn)行補(bǔ)充,并添加上提供題解的小伙伴的昵稱和地址。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目錄 不...
摘要:而讀起來是兩個(gè),所以第三個(gè)字符串就應(yīng)當(dāng)是。同理第四個(gè)字符串是一個(gè)一個(gè),因此是。依次類推而我們的目的是,對(duì)于輸入的正整數(shù),我們要給出第個(gè)字符串是什么。這里采用了是為了減少內(nèi)存的開銷。解法設(shè)置初始字符串將重新賦值當(dāng)前字符字符計(jì)數(shù) 題目詳情 The count-and-say sequence is the sequence of integers with the first five t...
閱讀 1950·2021-09-22 15:55
閱讀 3584·2021-09-07 10:26
閱讀 704·2019-08-30 15:54
閱讀 742·2019-08-29 16:34
閱讀 903·2019-08-26 14:04
閱讀 3339·2019-08-26 11:47
閱讀 2186·2019-08-26 11:33
閱讀 2348·2019-08-23 15:17