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

字符串相加 #367

Open
lzxjack opened this issue Jan 11, 2023 · 1 comment
Open

字符串相加 #367

lzxjack opened this issue Jan 11, 2023 · 1 comment

Comments

@lzxjack
Copy link
Contributor

lzxjack commented Jan 11, 2023

No description provided.

@lxy-Jason
Copy link
Contributor

var addStrings = function(num1, num2) {
    let p1 = num1.length - 1
    let p2 = num2.length - 1
    let temp = 0
    let res = []

    while(p1 >= 0 || p2 >= 0){
        let n1 = Number(num1[p1] || 0) 
        let n2 = Number(num2[p2] || 0) 
        let sum = n1 + n2 + temp
        res.unshift(sum % 10)
        temp = (sum / 10) | 0
        p1--
        p2--
    }
    if(temp !== 0){
        res.unshift(temp)
    }

    return res.join('')
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants