• leetcode——38. 报数


    class Solution:
        def countAndSay(self, n: int) -> str:
            r={}
            r[1]='1'
            r[2]='11'
            r[3]='21'
            r[4]='1211'
            r[5]='111221'
            if n in r:
                return r[n]
            else:
                i=6
                while i<=n:
                    j=0
                    k=1
                    c=1
                    s=''
                    while j<len(r[i-1])-1:
                        if r[i-1][j]==r[i-1][k]:
                            c+=1
                            j,k=j+1,k+1
                        else:
                            s+=str(c)+r[i-1][j]
                            j,k=j+1,k+1
                            c=1
                    print(j,k)
                    if j ==len(r[i-1])-1:
                        s+=str(c)+r[i-1][j]
                    r[i]=s
                    i+=1
                return r[n]
    执行用时 :48 ms, 在所有 python3 提交中击败了82.33%的用户
    内存消耗 :14 MB, 在所有 python3 提交中击败了5.00%的用户
     
                                                      ——2019.10.24

    就用很简单而又直观的套路完成了这道题。

    public String countAndSay(int n) {
            if(n == 1){
                return "1";
            }
            if(n == 2){
                return "11";
            }
            String s = "11";
            StringBuilder sb = new StringBuilder();
            for(int i = 3;i<=n;i++){
                int j = 0;
                while (j<s.length()){
                    char c = s.charAt(j);
                    int num = 1;
                    j++;
                    while(j<s.length()){
                        if(s.charAt(j) == c){
                            j++;
                            num++;
                        }else{
                            sb.append(num);
                            sb.append(c);
                            num = 1;
                            c = s.charAt(j);
                            j++;
                        }
                    }
                    sb.append(num);
                    sb.append(c);
                }
                s = sb.toString();
                sb = new StringBuilder();
            }
            return s;
        }

    ——2020.7.7

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Longest Common Prefix
    Roman to Integer
    Intger to Roman
    Container With Most Water
    Regular Expression Matching
    atoi
    Rotate List
    54. Search a 2D Matrix && Climbing Stairs (Easy)
    53. Minimum Window Substring
    52. Sort Colors && Combinations
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11734336.html
Copyright © 2020-2023  润新知