Created
September 16, 2018 04:54
-
-
Save jianminchen/70607f7f45ae8d58a3f378d3cb8119fd to your computer and use it in GitHub Desktop.
Leetcode 415 add string - failed last test case since stack overflow
This file contains 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
public class Solution { | |
public string AddStrings(String num1, String num2) | |
{ | |
if (num1 == null || num1.Length == 0) | |
return num2; | |
if (num2 == null || num2.Length == 0) | |
return num1; | |
var length1 = num1.Length; | |
var length2 = num2.Length; | |
int rightmostDigit1 = num1[length1 - 1] - '0'; | |
int rightmostDigit2 = num2[length2 - 1] - '0'; | |
var sum = rightmostDigit1 + rightmostDigit2; | |
if (sum < 10) | |
return AddStrings(num1.Substring(0, length1 - 1), num2.Substring(0, length2 - 1)) + sum.ToString(); | |
else | |
{ | |
var sum1 = AddStrings(num1.Substring(0, length1 - 1), num2.Substring(0, length2 - 1)); | |
return AddStrings(sum1, "1") + (sum%10).ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment