415.两个字符串相加 Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

Subscribe to see which companies asked this question

  1. public class Solution {
  2. public string AddStrings(string num1, string num2) {
  3. string s = "";
  4. int maxLength = Math.Max(num1.Length, num2.Length);
  5. num1 = num1.PadLeft(maxLength, ‘0‘);
  6. num2 = num2.PadLeft(maxLength, ‘0‘);
  7. int carry = 0;
  8. int digit = 0;
  9. int i = maxLength - 1;
  10. while (i >= 0 || carry>0)
  11. {
  12. digit = carry;
  13. if (i >= 0)
  14. {
  15. digit += ((int)num1[i] - 48) + ((int)num2[i] - 48);
  16. }
  17. if (digit >= 10)
  18. {
  19. carry = digit / 10;
  20. }
  21. else
  22. {
  23. carry = 0;
  24. }
  25. s = (digit % 10).ToString() + s;
  26. i--;
  27. }
  28. return s;
  29. }
  30. }

来自为知笔记(Wiz)

时间: 2024-10-18 06:46:01

415.两个字符串相加 Add Strings的相关文章

输入两个字符串a和b,字符串内容为二进制数字,求两个字符串相加的结果,加法计算方法以二进制方式计算,并返回对应的字符串结果。

上代码 public static void main(String[] args) { String a = "11010"; String b = "111101"; int result = toInt(a)*toInt(b); System.out.println(Integer.toBinaryString(result)); } static int toInt(String str) { char[] c = str.toCharArray(); in

LeetCode 43. 字符串相乘(Multiply Strings)

43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. LeetCode43. Multiply Strings中等 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例?2: 输入: num1 = "123", num2 = "456&q

[LeetCode] 415. Add Strings 字符串相加

Given two non-negative numbers 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.

[LeetCode] Add Strings 字符串相加

Given two non-negative numbers 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.

36. leetcode 415. Add Strings

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 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain

LeetCode:字符串相加【415】

LeetCode:字符串相加[415] 题目描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100.num1 和num2 都只包含数字 0-9.num1 和num2 都不包含任何前导零.你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式. 题目分析 这道题其实很简单,我们要搞清楚手工计算两数之和的流程.两数相加,和如果大于10的话就有进位,进位最高为1,默认为0,该位相加的和应为sum%

LeetCode Add Binary 两个二进制数相加

1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 if(a==""&&b=="") return ""; 5 if(a=="") return b; 6 if(b=="") return a; 7 char *pa=&a[0],*pb=&b[0]; 8 int na=0,nb=0;

[CareerCup] 2.5 Add Two Numbers 两个数字相加

2..5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the su

[LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string. Example 1: Input: "sea", "eat" Output: 2 Explanation: You ne