昨天在老家,停更一天,今天恢復(fù)
String字符串類
Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lowercase.
題意:給定一個字符串?dāng)?shù)組,返回所有是“換位詞”的字符串。
所謂“換位詞/變位詞”就是包含相同字母,但字母順序可能不同的字符串。比如“abc", "bca", "cab", "acb", "bac", "cba"都互為換位詞。
之前寫過,具體實現(xiàn)時候for循環(huán)忘了加了
public class Solution0723 {
public static void Anagrams(String str){
StringBuffer sb = new StringBuffer(str);
acore(sb,0,sb.length()-1);
}
public static void acore(StringBuffer sb,int st,int end){
if (st > end)
return;
if(st==end){
System.out.println(sb);
} else{
for (int i = st; i <= end; i++){
swap(sb,st,i);
acore(sb,st+1,end);
swap(sb,st,i);
}
}
}
public static void swap(StringBuffer sb,int a,int b){
char temp = sb.charAt(a);
sb.setCharAt(a, sb.charAt(b));
sb.setCharAt(b, temp);
}
public static void main(String[] args){
String s ="abc";
Anagrams(s);
}
}
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. Note: The sequence of integers will be represented as a
string.
其實就是
1讀成1個1 就變成11
11 讀成2個1,變成21
21 讀成一個2,一個1,變成1211