美文网首页
2020-05-17 5 kyu Sum and Rest th

2020-05-17 5 kyu Sum and Rest th

作者: 苦庭 | 来源:发表于2020-05-18 05:17 被阅读0次

https://www.codewars.com/kata/5603a9585480c94bd5000073

My answer / AC

function sumDifRev(n){
  var table = {};
  var count = 0;
  var num=1;
  while(count<n) {
    var rev = String(num).split("").reverse().join("");
    if(rev[0]=="0") { num++; continue; }
    var sum = Number(num)+Number(rev);
    var dif = Math.abs(num-rev);
    if(sum%dif == 0){
      count++;
      table[count] = num;
    }
    num++;
  }
  return table[n];
}

Recap

一道常规训练题,一开始没看懂题目说的"All the integers that its reversed has leading zeroes should be discarded"。原来是要将所有reverse后以0开头的跳过,不纳入考虑。我还以为要把零处理掉正常考虑。

相关文章

网友评论

      本文标题:2020-05-17 5 kyu Sum and Rest th

      本文链接:https://www.haomeiwen.com/subject/fsdyohtx.html