Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

大数加法 #161

Open
TieMuZhen opened this issue Apr 8, 2022 · 0 comments
Open

大数加法 #161

TieMuZhen opened this issue Apr 8, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

image

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 * 计算两个数之和
 * @param s string字符串 表示第一个整数
 * @param t string字符串 表示第二个整数
 * @return string字符串
 */
function solve( s ,  t ) {
    let add = 0;
    let i = s.length - 1;
    let j = t.length - 1;
    let stack = [];
    while(i > -1 && j > -1){
        let st = (s[i] - 0) + (t[j] - 0) + add;  
        stack.push(st % 10);
        add = st - 0 > 9? 1: 0;
        i--;
        j--;
    }
    while(i > -1){
        let tmp = (s[i] - 0) + (add - 0);
        stack.push(tmp % 10);
        add = tmp - 0 > 9? 1: 0;
        i--;
    }
    while(j > -1){
        let tmp = (t[j] - 0) + (add - 0);
        stack.push(tmp % 10);
        add = tmp - 0 > 9? 1: 0;
        j--;
    }
    if(add){
        stack.push(add);
    }
    let result = "";
    while(stack.length){
        result += stack.pop();
    }
    return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant