Java日記2018-07-23

昨天在老家,停更一天,今天恢復(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容