2016-03-31 46 views
5

我試圖實現的是從給定輸入數字中獲取字母組合。例如。如果我輸入111,輸出應該是['AAA','KA','AK']。如果輸入是1111,那麼輸出應該是['AAAA','KAA','AKA','AAK','KK']。 局部工作代碼如下哪裏獲得['K','K']爲輸入111:顯示給定輸入數字的數據集中字母表的所有可能組合

<html> 
<head> 
    <h1>Javascript</h1> 
</head> 
<body> 
    <script> 
     var dataset = 
     {A:'1',B:'2',C:'3',D:'4',E:'5',F:'6',G:'7',H:'8',I:'9', 
     J:'10',K:'11',L:'12',M:'13',N:'14',O:'15',P:'16',Q:'17',R:'18', 
     S:'19',T:'20',U:'21',V:'22',W:'23',X:'24',Y:'25',Z:'26'}; 

     var arr = []; 
     var z; 
     var result = []; 

     var find = function(input){ 
      for(var key in dataset) { 
       if(dataset[key] === input) { 
        return key; 
       } 
      } 
     } 

     var foo = function(x){ 
      z = x.toString(); 

      for(var i=0;i<z.length;i++){ 
       arr.push(z.charAt(i)); 
      } 


      for(var i=0;i<arr.length;i++){ 
       if(arr[i]+arr[i+1] <= 26){ 
        var returnedkey = find(arr[i]+arr[i+1]); 
        result.push(returnedkey); 
       }    
      } 

     } 

     foo(111); 
     console.log(arr); 
     console.log(result); 

    </script> 
</body> 

我很困惑如何進一步進行,這是正確的方法, 提前感謝!

+0

相關http://codereview.stackexchange.com/questions/7001/generating-all-combinations-of-an-array – thangngoc89

回答

3

該建議使用一個對象來查找並回顧字符串的迭代。如果可能的話

getCombination('1111')c()

第一迭代的在單個字符的呼叫,然後實施例遍歷兩個字符。

left right letter result 
---- ----- ------ ----- 
1111   one 
111 A  one 
    11 AA  one 
    1 AAA one 
     AAAA one AAAA 
    11 AA  one 
     AAK two AAK 
111 A  one 
    1 AK  two 
     AKA one AKA 
    11 K  two 
    1 KA  one 
     KAA one KAA 
    11 K  two 
     KK  two KK 

function getCombination(s) { 
 
    function c(left, right) { 
 
     if (!left) { 
 
      result.push(right); 
 
      return; 
 
     } 
 
     // checks if the first character of left is in the letter object 
 
     // then call c without the first letter of left 
 
     // and right with the letter from the letter object of the first character 
 
     letters[left[0]] && c(left.substring(1), right + letters[left[0]]); 
 

 
     // it is basically the same as above, but with two characters 
 
     left.length > 1 && letters[left.substring(0, 2)] && c(left.substring(2), right + letters[left.substring(0, 2)]); 
 
    } 
 

 
    var letters = { 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z' }, 
 
     result = []; 
 

 
    c(s, ''); 
 
    return result; 
 
} 
 

 
document.write('<pre>' + JSON.stringify(getCombination('1111'), 0, 4) + '</pre>'); 
 
document.write('<pre>' + JSON.stringify(getCombination('1011121314'), 0, 4) + '</pre>');

+0

什麼'信[ left [0]] && c(left.substring(1),right + letters [left [0]]);'do?我的意思是我不知道它究竟會做什麼。你能解釋一下嗎? – Apb

+0

我試過解決方案,它按預期工作,但我並不真正瞭解這些行做字母[left [0]] && c(left.substring(1),right + letters [left [0]]); left.length> 1 && letters [left.substring(0,2)] && c(left.substring(2),right + letters [left.substring(0,2)]); –

+0

如果第一個字母在對象中,這個'letters [left [0]]'吱吱作響,如果是這樣,則用左邊的剩餘部分調用'c'並且向右添加一個字母。 –

相關問題