415. Add Strings
来源:互联网 发布:c语言 最长单词 编辑:程序博客网 时间:2024/06/05 15:00
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both num1 and num2 is < 5100.
- Both num1 and num2 contains only digits 0-9.
- Both num1 and num2 does not contain any leading zero.
- You must not use any built-in BigInteger library or convert the inputs to integer directly.
代码实现
public string AddStrings(string num1, string num2) { StringBuilder sb = new StringBuilder(); int carryBit = 0; //carry bit for (int i = num1.Length - 1, j = num2.Length - 1; i >= 0 || j >= 0 || carryBit == 1; i--, j--) //think: num1=3, num2=9 { int x = i < 0 ? 0 : charToInt(num1[i]); int y = j < 0 ? 0 : charToInt(num2[j]); sb.Append((x + y + carryBit) % 10); carryBit = (x + y + carryBit) / 10; } char[] chars = sb.ToString().Reverse().ToArray(); return new string(chars); } private int charToInt(char c) { return c - '0'; }
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp
阅读全文
2 0
- 415. Add Strings
- Leetcode-415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- [LeetCode]--415. Add Strings
- LeetCode 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- LeetCode 415. Add Strings
- 415. Add Strings
- 415. Add Strings
- 415. Add Strings(unsolved)
- leetcode 415. Add Strings
- Delphi 自带GIF控件的动画图片加载和播放
- 走出象牙塔
- Python安装完成 运行的时候报错 0xc000007b
- python函数式编程高阶函数之sorted()
- 关于matlab GUI重命名的问题。
- 415. Add Strings
- 《Tomcat架构解析》一书出版上市
- C#项目编译报“LC.exe已退出,代码为-1错误”解决办法
- redis 的学习路线
- Maven初识(三)——Maven的常用命令(插件),生命周期
- 【教程】InstallShield使用完全教程
- 解决win7无法更新defender(错误提示无法定义更新)
- swjtu 2385 Maximize The Beautiful Value
- 推荐两款实用的json数据请求及处理插件