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 n-th sequence.
Note: The sequence of integers will be represented as a string.
Look-and-Say Sequence - GeeksforGeeks
http://rleetcode.blogspot.com/2014/02/count-and-say-java.html
https://gist.github.com/Kevin-Lee/3678544
http://rosettacode.org/wiki/Look-and-say_sequence#Java
Read full article from LeetCode - Count and Say | Darren's Blog
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 n-th sequence.
Note: The sequence of integers will be represented as a string.
public String countAndSay(int n) {
if (n < 0)
return "";
// Go n-1 steps starting from the first integer "1"
String result = "1";
for (int i = 1; i < n; i++) {
// StringBuilder: a mutable sequence of characters similar to StringBuffer
// but with no guarantee of synchronization; recommended for most cases
StringBuilder builder = new StringBuilder();
int count = 1; // The number of appearances of the same digit
for (int j = 1; j < result.length(); j++) {
if (result.charAt(j) == result.charAt(j-1)) { // Same as the preceding digit
count++;
} else { // A different digit
builder.append(count); // Append the count of appearances of the character
builder.append(result.charAt(j-1)); // Append the character itself
count = 1; // Reset count
}
}
// The last digit and its count of appearances have not been included in builder
builder.append(count);
builder.append(result.charAt(result.length()-1));
// Get the next integer in the sequence
result = builder.toString();
}
// Out of loop; we arrive at the n-th integer
return result;
}
http://tech-wonderland.net/blog/leetcode-count-and-say.html public String countAndSay(int n) { |
03 | if (n < 1) |
04 | return ""; |
05 | StringBuffer strBuffer = new StringBuffer("1"); |
06 | while (--n > 0) { |
07 | int i = 0; |
08 | int j = 0; |
09 | int len = strBuffer.length(); |
10 |
11 | StringBuffer strNext = new StringBuffer(); |
12 | while (j < len) { |
13 | while (j < len && |
14 | (j == i || strBuffer.charAt(j) == strBuffer.charAt(j-1))) |
15 | ++j; |
16 |
17 | strNext.append(j - i); |
18 | strNext.append(strBuffer.charAt(i)); |
19 | i = j; |
20 | } |
21 | strBuffer = strNext; |
22 | } |
23 |
24 | return strBuffer.toString(); |
25 | } |
// Returns n'th term in look-and-say sequencestring countnndSay(int n){ // Base cases if (n == 1) return "1"; if (n == 2) return "11"; // Find n'th term by generating all terms from 3 to // n-1. Every term is generated using previous term string str = "11"; // Initialize previous term for (int i = 3; i<=n; i++) { // In below for loop, previous character // is processed in current iteration. That // is why a dummy character is added to make // sure that loop runs one extra iteration. str += '$'; int len = str.length(); int cnt = 1; // Initialize count of matching chars string tmp = ""; // Initialize i'th term in series // Process previous term to find the next term for (int j = 1; j < len; j++) { // If current character does't match if (str[j] != str[j-1]) { // Append count of str[j-1] to temp tmp += cnt + '0'; // Append str[j-1] tmp += str[j-1]; // Reset count cnt = 1; } // If matches, then increment count of matching // characters else cnt++; } // Update str str = tmp; } return str;}| public String countAndSay(int n) { // Start typing your Java solution below | |
| // DO NOT write main() function | |
| if (n<=1){ | |
| return String.valueOf(1); | |
| } | |
| else{ | |
| return say(countAndSay(n-1)); | |
| } | |
| } | |
| private String say(String s){ | |
| int i=0; | |
| int count=1; | |
| StringBuffer sb=new StringBuffer(); | |
| while(i<s.length()){ | |
| int j=i+1; | |
| while(j<s.length()&&s.charAt(j)==s.charAt(i)){ | |
| count++; | |
| j++; | |
| } | |
| sb.append(count); | |
| sb.append(s.charAt(i)); | |
| i=j; | |
| count=1; | |
| } | |
| return sb.toString(); | |
| } |
http://rosettacode.org/wiki/Look-and-say_sequence#Java
Read full article from LeetCode - Count and Say | Darren's Blog