求字串的len組合數(java程式)

來源:互聯網
上載者:User

import java.util.List;<br />import java.util.ArrayList;<br />/**<br /> * 求字串的len組合數<br /> *<br /> * @author wenin819<br /> *<br /> */<br />public class Combination{<br />/**<br /> * 求組合數的主要方法<br /> */<br />public static List<String> combination(String inStr, int len){<br />StringBuffer noDoubleStr =new StringBuffer(inStr);<br />noDouble(noDoubleStr);<br />if( len >= noDoubleStr.length()) return null;<br />return combinationItem(noDoubleStr , len);<br />}<br />/**<br /> * 去除重複的字元<br /> */<br />public static void noDouble(StringBuffer input){<br />for(int i = 0; i < input.length(); i++){<br />char temp = input.charAt(i);<br />for(int j = i+1; j < input.length(); ){<br />if(temp == input.charAt(j)){<br />input.deleteCharAt(j);<br />}else{<br />j++;<br />}<br />}<br />}<br />}<br />/**<br /> * 求組合數<br /> */<br />public static List<String> combinationItem(StringBuffer input, int len){<br />List<String> rsList = new ArrayList<String>();<br />boolean[] array = new boolean[input.length()];<br />for(int i = 0; i < array.length; i++){<br />array[i] = false;<br />}<br />do{<br />if(len == countOfTrue(array)){<br />StringBuffer temp = new StringBuffer(input);<br />for(int j = temp.length() - 1;j >= 0;j--){<br />if(array[j]) continue;<br />temp.deleteCharAt(j);<br />}<br />rsList.add(new String(temp));<br />}<br />}while(booleanAdd(array));<br />return rsList;<br />}<br />/**<br /> * 求boolean數組元素為true的個數<br /> */<br />public static int countOfTrue(boolean[] array){<br />int count = 0;<br />for(int i = 0;i < array.length;i++){<br />if(array[i]) count++;<br />}<br />return count;<br />}<br />/**<br /> * boolean數組自加(同2進位加1)<br /> */<br />public static boolean booleanAdd(boolean[] array){<br />boolean flag = true;<br />for(int i = 0;i < array.length; i++){<br />if(false == flag) return true;<br />if(array.length-1 == i && true == array[i]){<br />array[i] = false;<br />return false;<br />}<br />if(false == array[i]){<br />array[i] = true;<br />return true;<br />}<br />array[i] = false;<br />}<br />return false;<br />}<br />public static void main(String[] args){<br />String input = new String("abcde");<br />int len = 3;<br />if(args.length > 1){<br />input = args[0];<br />len = Integer.parseInt(args[1]);<br />}<br />List list = combination(input,len);<br />if(list != null){<br />System.out.println(input+"的"+len+"組合數的個數為:"+list.size()+"/n組合數分別為:");<br />for(int i = 0; i < list.size(); i++){<br />System.out.println(i+1+": "+list.get(i).toString());<br />}<br />}<br />}<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.