昨天在老家,停更一天,今天恢复
String字符串类
Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lowercase.
题意:给定一个字符串数组,返回所有是“换位词”的字符串。
所谓“换位词/变位词”就是包含相同字母,但字母顺序可能不同的字符串。比如“abc", "bca", "cab", "acb", "bac", "cba"都互为换位词。
之前写过,具体实现时候for循环忘了加了
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