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开头的跳过,不纳入考虑。我还以为要把零处理掉正常考虑。
网友评论