• leetcode — count-and-say


    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * Source : https://oj.leetcode.com/problems/count-and-say/
     *
     * Created by lverpeng on 2017/7/14.
     *
     * 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.
     *
     */
    public class CountAndSay {
    
    
        /**
         * 循环n次,根据上一个字符串找到下一个
         *
         * @param n
         * @return
         */
        public String countAndSay (int n) {
            if (n == 0) {
                return "1";
            }
            if (n == 1) {
                return "11";
            }
            List<String> list = new ArrayList<String>();
            list.add("1");
            list.add("11");
            for (int i = 2; i <= n; i++) {
                getNext(list);
            }
    
            System.out.println(Arrays.toString(list.toArray()));
            return list.get(list.size() - 1);
        }
    
        private void getNext (List<String> list) {
            String last = list.get(list.size() - 1);
            String next = "";
            int count = 1;
            for (int i = 1; i < last.length(); i++) {
                if (last.charAt(i) == last.charAt(i - 1)) {
                    count ++;
                    if (i == last.length() - 1) {
                        next += count + "" +  last.charAt(i - 1);
                    }
                } else {
                    next += count + "" +  last.charAt(i - 1);
                    count = 1;
                    if (i == last.length() - 1) {
                        next += count + "" +  last.charAt(i);
                    }
                }
            }
            list.add(next);
        }
    
        public static void main(String[] args) {
            CountAndSay countAndSay = new CountAndSay();
            System.out.println(countAndSay.countAndSay(2));
            System.out.println(countAndSay.countAndSay(3));
            System.out.println(countAndSay.countAndSay(4));
            System.out.println(countAndSay.countAndSay(5));
            System.out.println(countAndSay.countAndSay(6));
        }
    }
    
  • 相关阅读:
    aws-lambda之异步实现文件的下载上传
    aws实例部署flask报错script-timed-out-before-returning-headers-application-py
    aws上部署scrapy,出现 Out of Memory,内存溢出
    在线UserAgent,爬虫UA
    ubuntu10.24 下安装 unixODBC coreseek4.1 手记
    ubuntu12.04 安装 python2.6
    coreseek/sphinx CentOS6.4下安装
    Elasticsearch 6.2.3 崩溃经历
    (转)梳理在线教育的几大金矿
    kangaroo-open 开源在线公开课平台
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7456387.html
Copyright © 2020-2023  润新知