415:字符串相加

This commit is contained in:
轩辕龙儿 2022-04-15 15:20:21 +08:00
parent a3b440a9bb
commit c1b36dd293
2 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,88 @@
//<p>给定两个字符串形式的非负整数&nbsp;<code>num1</code> <code>num2</code>&nbsp;计算它们的和并同样以字符串形式返回</p>
//
//<p>你不能使用任何內建的用于处理大整数的库比如 <code>BigInteger</code>&nbsp;也不能直接将输入的字符串转换为整数形式</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1</strong></p>
//
//<pre>
//<strong>输入</strong>num1 = "11", num2 = "123"
//<strong>输出</strong>"134"
//</pre>
//
//<p><strong>示例 2</strong></p>
//
//<pre>
//<strong>输入</strong>num1 = "456", num2 = "77"
//<strong>输出</strong>"533"
//</pre>
//
//<p><strong>示例 3</strong></p>
//
//<pre>
//<strong>输入</strong>num1 = "0", num2 = "0"
//<strong>输出</strong>"0"
//</pre>
//
//<p>&nbsp;</p>
//
//<p>&nbsp;</p>
//
//<p><strong>提示</strong></p>
//
//<ul>
// <li><code>1 &lt;= num1.length, num2.length &lt;= 10<sup>4</sup></code></li>
// <li><code>num1</code> <code>num2</code> 都只包含数字&nbsp;<code>0-9</code></li>
// <li><code>num1</code> <code>num2</code> 都不包含任何前导零</li>
//</ul>
//<div><div>Related Topics</div><div><li>数学</li><li>字符串</li><li>模拟</li></div></div><br><div><li>👍 541</li><li>👎 0</li></div>
package leetcode.editor.cn;
/**
* Classname ${NAME}
* Description 415:字符串相加
* Date ${DATE} ${TIME}
* author ${USER}
*/
public class AddStrings {
public static void main(String[] args) {
Solution solution = new AddStrings().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder str = new StringBuilder();
StringBuilder sb1 = new StringBuilder(num1);
StringBuilder sb2 = new StringBuilder(num2);
sb1.reverse();
sb2.reverse();
int index = 0;
int sum = 0;
while (index < sb1.length() || index < sb2.length()) {
if (index < sb1.length()) {
sum += (sb1.charAt(index) - '0');
}
if (index < sb2.length()) {
sum += (sb2.charAt(index) - '0');
}
if (sum >= 10) {
str.insert(0, (sum % 10));
sum /= 10;
} else {
str.insert(0, sum);
sum = 0;
}
index++;
}
if (sum > 0) {
str.insert(0, sum);
}
return str.toString();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,39 @@
<p>给定两个字符串形式的非负整数&nbsp;<code>num1</code><code>num2</code>&nbsp;,计算它们的和并同样以字符串形式返回。</p>
<p>你不能使用任何內建的用于处理大整数的库(比如 <code>BigInteger</code>&nbsp;也不能直接将输入的字符串转换为整数形式。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>num1 = "11", num2 = "123"
<strong>输出:</strong>"134"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>num1 = "456", num2 = "77"
<strong>输出:</strong>"533"
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>num1 = "0", num2 = "0"
<strong>输出:</strong>"0"
</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num1.length, num2.length &lt;= 10<sup>4</sup></code></li>
<li><code>num1</code><code>num2</code> 都只包含数字&nbsp;<code>0-9</code></li>
<li><code>num1</code><code>num2</code> 都不包含任何前导零</li>
</ul>
<div><div>Related Topics</div><div><li>数学</li><li>字符串</li><li>模拟</li></div></div><br><div><li>👍 541</li><li>👎 0</li></div>