javascript 24line


  • 1
    9
    var letterCombinations = function (digits) {
      var words = 'abcdefghijklmnopqrstuvwxyz'.split('');
      var numbers = {};
      var index=0;
      for (var n = 2; n <= 9; n++) {
        numbers[n] = words.slice(index, (n===7||n===9)?index+=4:index+=3); // 含头不含尾
      }
      var returnArr = [];
      for (var i = 0; i < digits.length; i++) {
        i===0 && (returnArr=['']);
        var number = digits[i];
        var items = numbers[number];
        var arr = [];
        for (var t = 0; t < items.length; t++) {
          var arr1 = [];
          for (var r = 0; r < returnArr.length; r++) {
            arr1.push(returnArr[r] + items[t]);
          }
          arr = arr.concat(arr1); //concat不修改原属组,需要重新接收。。。坑了一会
        }
        returnArr = arr;
      }
      return returnArr;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.