标签:i++ mst ++ sid represent and -- var cte
总的来说这是一个将字符串转化为数字进行计算再转化为字符串的计算,醉生与死;
Given the string representations of two integers, return the string representation of the sum of those integers.
For example:
sumStrings(‘1‘,‘2‘) // => ‘3‘
A string representation of an integer will contain no characters besides the ten numerals "0" to "9".
function sumStrings(a,b) {
var arr=[];
var a=a.split("").reverse();
var b=b.split("").reverse();
if(a.length<b.length){
var c=a;
a=b;
b=c;
}
for(var i=0;i<a.length;i++){
if(i<b.length){
arr[i]=parseInt(a[i])+parseInt(b[i])
}else{
arr[i]=parseInt(a[i])
}
}
arr.reverse();
for(var i=arr.length-1;i>0;i--){
if(arr[i]>=10){
arr[i]=arr[i]%10;
arr[i-1]=arr[i-1]+1;
}
console.log(arr[i])
}
for(var i=0;i<arr.length;i++){
if(arr[i]==0){
arr.splice(i,1);
}else{
break;
}
}
return arr.join("");
}
测试使用的数字:
11.总数字作为数字Sum Strings as Numbers
标签:i++ mst ++ sid represent and -- var cte
原文地址:http://www.cnblogs.com/chengnanbei/p/7251513.html