-
[LeetCode] Count and Say알고리즘 2019. 10. 22. 16:48
문제
The count-and-say sequence is the sequence of integers with the first five terms as following:1. 1 2. 11 3. 21 4. 1211 5. 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 where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1 Output: "1"
Example 2:
Input: 4 Output: "1211"
Approach
하라는대로 구현하면 된다. 숫자 몇개인지 입력하고 그 다음 그 숫자 입력, 또 숫자 몇개인지 입력하고 그 다음 그 숫자 입력...
vector로 n번째 수열을 기록한 다음 마지막에 string으로 변환하였다.
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_CountAndSay.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Trapping Rain Water (0) 2019.10.23 [LeetCode] First Missing Positive (0) 2019.10.22 [LeetCode] Valid Sudoku (0) 2019.10.21 [LeetCode] Find First and Last Position of Element in Sorted Array (0) 2019.10.21 [LeetCode] Search in Rotated Sorted Array (0) 2019.10.15