首页 > 遇到一个算法题,求大神给个更好的思路

遇到一个算法题,求大神给个更好的思路

题目大意是这样的,就是:

 "one" => 1
 "twenty" => 20
 "two hundred forty-six" => 246
 "seven hundred eighty-three thousand nine hundred and nineteen" => 783919

转换的范围是0到1000,000,注意转换需要支持'and'。

先声明这个不是作业帮哈,我自己完成了已经,也pass了,使用递归的思路做的,按thousand、hundred这种有特殊意义的词分段,之后再转换,不过总感觉不是最好的方法,应该还有更简单的方法吧

求大神给讲解讲解。


const numberLib = {
  //0-9
  'zero': 0,
  'one': 1,
  'two': 2,
  'three': 3,
  'four': 4,
  'five': 5,
  'six': 6,
  'seven': 7,
  'eight': 8,
  'nine': 9,

  //10-19
  'ten': 10,
  'eleven': 11,
  'twelve': 12,
  'thirteen': 13,
  'fourteen': 14,
  'fifteen': 15,
  'sixteen': 16,
  'seventeen': 17,
  'eighteen': 18,
  'nineteen': 19,

  //20-99
  'twenty': 20,
  'thirty': 30,
  'forty': 40,
  'fifty': 50,
  'sixty': 60,
  'seventy': 70,
  'eighty': 80,
  'ninety': 90,

  //100-999
  'hundred': 100,

  //1000-9999
  'thousand': 1000,

  //9999-1000,000
  'million': 1000000,

  //other
  'and': 1
};

const quantifier = {
  'thousand': 1,
  'million': 1,
  'hundred': 1,
  'and': 1
};

function parseInt(str) {
  const arr = str.split(/\b\s\b/);
  let num = 0, num1 = 0;

  arr.forEach(e=> {
    if (quantifier[e]) {
      if (e == 'hundred')
        return num *= 100;
      if (e == 'and')
        return;

      num1 += num * numberLib[e];
      num = 0;
    }
    else {
      const arr1 = e.split(/\b-\b/);
      num += (arr1.length == 1 ? numberLib[e] : numberLib[arr1[0]] + numberLib[arr1[1]]);
    }
  });

  return num + num1;
};

最终综合各种版本在我原来基础上改的,可能还是有点乱,不过比以前好多了。

其实这个问题确实按一楼那种思路来着,但是千万别忘记hundred这个词比较特殊,因为它既可以修饰量词,比如thousand这种的,又可以当做量词,比如three thousand,所以不能一棒子打死,大概就这样。

由于只能采纳一个,我就采纳有明的了,其他的我都点赞了。


var Small = {
    'zero': 0,
    'one': 1,
    'two': 2,
    'three': 3,
    'four': 4,
    'five': 5,
    'six': 6,
    'seven': 7,
    'eight': 8,
    'nine': 9,
    'ten': 10,
    'eleven': 11,
    'twelve': 12,
    'thirteen': 13,
    'fourteen': 14,
    'fifteen': 15,
    'sixteen': 16,
    'seventeen': 17,
    'eighteen': 18,
    'nineteen': 19,
    'twenty': 20,
    'thirty': 30,
    'forty': 40,
    'fifty': 50,
    'sixty': 60,
    'seventy': 70,
    'eighty': 80,
    'ninety': 90
};

var Magnitude = {
    'thousand':     1000,
    'million':      1000000,
    'billion':      1000000000,
    'trillion':     1000000000000,
    'quadrillion':  1000000000000000,
    'quintillion':  1000000000000000000,
    'sextillion':   1000000000000000000000,
    'septillion':   1000000000000000000000000,
    'octillion':    1000000000000000000000000000,
    'nonillion':    1000000000000000000000000000000,
    'decillion':    1000000000000000000000000000000000,
};

var a, n, g;

function text2num(s) {
    a = s.toString().split(/[\s-]+/);
    n = 0;
    g = 0;
    a.forEach(feach);
    return n + g;
}

function feach(w) {
    var x = Small[w];
    if (x != null) {
        g = g + x;
    }
    else if (w == "hundred") {
        g = g * 100;
    } else if (w == "and") {
        return;
    }
    else {
        x = Magnitude[w];
        if (x != null) {
            n = n + g * x
            g = 0;
        }
        else { 
            alert("Unknown number: "+w); 
        }
    }
}

来自:http://stackoverflow.com/a/12014376 ,我修改下支持“and”


可以直接从左往右过词,
例如 seven hundred eighty-three thousand nine hundred and nineteen
设置 num = 0
seven ,则把 num 设为 7
hundred ,则把 num 乘以 100,变成 700
eighty-three ,则把 num 加上 83 变成 783
... 就是遇到读到数字就相加,读到单位就相乘,以此类推

【热门文章】
【热门文章】