415. Add Strings
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
思路:由于一个字符与一个非负整数等价,所以直接对两个字符串对应字符进行加法运算。诸多细节:首先是向一个string对象指定位置插入字符;然后,为了避免后续处理的麻烦,我直接将两个字符串归一化成一样的长度,用0来补齐;0的ascii为48,在进行字符加法时要十分注意加减48的处理。
时间: 2025-01-10 14:02:02