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

資訊專欄INFORMATION COLUMN

[Leetcode] Count and Say 數(shù)個數(shù)

whjin / 2058人閱讀

摘要:反轉(zhuǎn)字符法復(fù)雜度時間空間思路因為數(shù)字不好從前向后遍歷每一位要先統(tǒng)計一共有多少位,比較麻煩,所以我們直接從后向前計數(shù),最后把結(jié)果倒置就行了。

Count Consecutive Digits in Integer

Count consecutive digits and say it. For example, return 132341 if input is 1112224. There are three 1s, three 2s and one 4.

反轉(zhuǎn)字符法 復(fù)雜度

時間 O(N) 空間 O(1)

思路

因為數(shù)字不好從前向后遍歷每一位(要先統(tǒng)計一共有多少位,比較麻煩),所以我們直接從后向前計數(shù),最后把結(jié)果倒置就行了。

注意

退出循環(huán)后還要額外執(zhí)行一次append,將最后的連續(xù)數(shù)字補齊

代碼
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

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.

遞歸法 復(fù)雜度

時間 O(N) 空間 O(N) 遞歸??臻g

思路

因為第n個數(shù)count and say的結(jié)果是基于第n-1個數(shù)的,我們可以用遞歸解決這個問題。

代碼
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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

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

相關(guān)文章

  • [Leetcode] Count And Say 外觀序列

    摘要:遞歸解法復(fù)雜度時間空間遞歸棧思路該序列又叫做外觀序列,無論如何我們都得將前一個序列元素算出來,才能計算后一個序列元素。當(dāng)遞歸至的時候返回初始數(shù)字。另外,比如初始數(shù)字,第一次變成了,我們可以發(fā)現(xiàn)大于的數(shù)都只會一個一個出現(xiàn)了。 Count And Say The count-and-say sequence is the sequence of integers beginning as...

    Towers 評論0 收藏0
  • leetcode38 count and say 數(shù)數(shù)游戲

    摘要:題目要求英文的題目有點繞口,所以去網(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...

    dabai 評論0 收藏0
  • 前端 | 每天一個 LeetCode

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

    張漢慶 評論0 收藏0
  • LeetCode 攻略 - 2019 年 7 月上半月匯總(55 題攻略)

    摘要:微信公眾號記錄截圖記錄截圖目前關(guān)于這塊算法與數(shù)據(jù)結(jié)構(gòu)的安排前。已攻略返回目錄目前已攻略篇文章。會根據(jù)題解以及留言內(nèi)容,進(jìn)行補充,并添加上提供題解的小伙伴的昵稱和地址。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目錄 不...

    warmcheng 評論0 收藏0
  • leetcode 38 count and say

    摘要:而讀起來是兩個,所以第三個字符串就應(yīng)當(dāng)是。同理第四個字符串是一個一個,因此是。依次類推而我們的目的是,對于輸入的正整數(shù),我們要給出第個字符串是什么。這里采用了是為了減少內(nèi)存的開銷。解法設(shè)置初始字符串將重新賦值當(dāng)前字符字符計數(shù) 題目詳情 The count-and-say sequence is the sequence of integers with the first five t...

    不知名網(wǎng)友 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<