lintcode-easy-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.

Example

Given n = 5, return "111221".

Note

The sequence of integers will be represented as a string.

思路也很直接,但是写的时候要注意数组index越界一类的问题

public class Solution {
    /**
     * @param n the nth
     * @return the nth sequence
     */
    public String countAndSay(int n) {
        // Write your code here
        if(n < 1)
            return null;

        if(n == 1)
            return "1";

        String prev = "1";
        String next = "";

        for(int i = 2; i <= n; i++){
            char[] temp = prev.toCharArray();

            int index = 0;
            while(index < temp.length){
                int count = 0;

                count++;
                index++;
                while(index < temp.length && temp[index] == temp[index - 1]){
                    count++;
                    index++;
                }
                next = next + String.valueOf(count) + temp[index - 1];
            }
            prev = next;
            next = "";
        }

        return prev;
    }
}
时间: 2024-10-20 10:30:55

lintcode-easy-Count and Say的相关文章

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lincode easy]Count and Say

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" o

LintCode Triangle Count

原题链接在这里:http://www.lintcode.com/en/problem/triangle-count/# 题目: Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find? Example Given a

[lintcode easy]Compare Strings

Compare Strings Compare two strings A and B, determine whether A contains all of the characters in B. The characters in string A and B are all Upper Case letters. Example For A = "ABCD", B = "ACD", return true. For A = "ABCD"

lintcode easy Happy Number

Happy Number Write an algorithm to determine if a number is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process unt

[lintcode easy]Valid Sudoku

Valid Sudoku Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character .. Example The following partially filed sudoku is valid. Note A valid Sudoku board (partially filled) is no

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay