Last active
September 5, 2017 13:47
-
-
Save luojiyin1987/5b4547c0973bfc748c2bd2a4b3ab0dfa to your computer and use it in GitHub Desktop.
Add Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func addStrings(num1 string, num2 string) string { | |
| n1, n2 := len(num1), len(num2) | |
| n := n1 | |
| if n < n2 { | |
| n = n2 | |
| } | |
| res := make([]byte, n+1, n+1) | |
| sum := 0 | |
| for i := 0; i <= n; i++ { | |
| if i < n1 { | |
| sum = sum + int(num1[n1-i-1]) - int('0') | |
| } | |
| if i < n2 { | |
| sum = sum + int(num2[n2-i-1]) - int('0') | |
| } | |
| res[n-i] = byte(sum%10 + int('0')) | |
| sum = sum / 10 | |
| } | |
| if res[0] == '0' { | |
| return string(res[1:]) | |
| } | |
| return string(res) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for i := 0; i < n; i++ { //it will ????
if i < n1 {
sum = sum + int(num1[n1-i-1]) - int('0')
}
if i < n2 {
sum = sum + int(num2[n2-i-1]) - int('0')
}
res[n-i] = byte(sum%10 + int('0'))
sum = sum / 10
}