zoukankan      html  css  js  c++  java
  • 38. 报数

    38. 报数

    https://leetcode-cn.com/problems/count-and-say/description/

    package com.test;
    
    /**
     * @Author stono
     * @Date 2018/8/20 上午11:11
     *
     */
    public class Lesson038 {
        public static void main(String[] args) {
            int n = 6;
            String s = countAndSay(n);
            System.out.println(s);
        }
        public static String countAndSay(int n) {
            if (n - 1 == 0) {
                return "1";
            }else {
                // 使用递归进行计算
                String n2 = countAndSay(n-1);
                char[] chars = n2.toCharArray();
                StringBuilder res = new StringBuilder();
                // 找出与第i个所有相同的数字,进行计数统计
                for (int i=0;i<chars.length;i++) {
                    char aChar = chars[i];
                    int count = 1;
                    while (i + 1 < chars.length && chars[i + 1] - aChar == 0) {
                        count ++;
                        i++;
                    }
                    res.append(count).append(aChar);
                }
                return res.toString();
            }
        }
    }

    还可以用模式空间:

    https://blog.csdn.net/wanglelelihuanhuan/article/details/51591809

    但是不是特别明白;

  • 相关阅读:
    MySQL数据库分页
    Spring MVC
    Spring框架
    Java学习计划(转载)
    开发用户注册模块
    Ajax技术
    Jodd Email 发送邮件
    DOM技术
    MD5加密
    final关键字的使用
  • 原文地址:https://www.cnblogs.com/stono/p/9504977.html
Copyright © 2011-2022 走看看