第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

輸入:單詞的字符串。打印給定字符串的所有重新排列而不重復(fù)。

輸入:單詞的字符串。打印給定字符串的所有重新排列而不重復(fù)。

瀟湘沐 2023-11-10 16:34:39
我正在嘗試用 Java 實(shí)現(xiàn)這個(gè),但不知道如何實(shí)現(xiàn)。有什么建議么?例子。輸入:ABC輸出:abcacbbacbcacabcba輸入:艾布輸出:啊啊 啊啊啊
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是我能想到的最簡單的方法


 String str= "abc";

            ArrayList<String> letters = new ArrayList<String>();

            HashSet<String> combinations = new HashSet<String>();

            for(int i = 0; i<str.length();i++) {

//Break String into letters

                letters.add(str.substring(i,i+1));

            }

            for(int i = (str.length()*2)+1;i>0;i--) {

//Will loop through maximum possible outcomes

                String result = "";

                String bin = Integer.toBinaryString(i);

//System.out.println(bin);

                while(bin.length()<3) {

                    bin="0"+bin;

                }

                for(int b = bin.length();b>0;b--) {

                    if(bin.substring(b-1,b).equals("1"))result = result.concat(letters.get(b-1));

                }

                for(int b = bin.length();b>0;b--) {

                    if(bin.substring(b-1,b).equals("0"))result = result.concat(letters.get(b-1));

                }

                combinations.add(result);

            }

            System.out.println(str);

            for(String c : combinations) {

                System.out.println(c);

            }

它使用二進(jìn)制循環(huán)遍歷原始字符串的數(shù)組,然后將結(jié)果添加到哈希集以刪除重復(fù)項(xiàng)


查看完整回答
反對(duì) 回復(fù) 2023-11-10
  • 1 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)