【LeetCode415】Add Strings

题目描述:

解决思路:

  此题较简单,和前面【LeetCode67】方法一样。

Java代码:

 1 public class LeetCode415 {
 2     public static void main(String[] args) {
 3         String a="1",b="9";
 4         System.out.println(a+"和"+b+"相加的结果是:"+new Solution().addStrings(a, b));
 5     }
 6 }
 7 class Solution {
 8     public String addStrings(String num1, String num2) {
 9         int len1=num1.length(),len2=num2.length();
10         int i=len1-1,j=len2-1;
11         int carry=0;
12         StringBuilder sb=new StringBuilder();
13         while(i>=0||j>=0){
14             int sum=carry;
15             if(i>=0) sum+=num1.charAt(i--)-‘0‘;
16             if(j>=0) sum+=num2.charAt(j--)-‘0‘;
17             sb.append(sum%10);
18             carry=sum/10;
19         }
20         if(carry==1) sb.append(carry);
21         return (sb.length()==0?"0":sb.reverse().toString());
22     }
23 }

程序结果:

时间: 2024-08-07 08:21:24

【LeetCode415】Add Strings的相关文章

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

【POJ2406】【KMP】Power Strings

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

【LeetCode】Add Two Numbers 解题报告

[题目] You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -

【LeetCode】Multiply Strings 解题报告

[题目] Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. [解析] 题意:两个字符串表示的非负整数相乘,用字符串的形式返回积. 思路:逐位相乘. 关键:中间结果如何保存?如果用字符串保存中间结果,频繁该值不太方便,所以还是用整数数组保

【leetcode】Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.

LeetCode【67】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 看起来挺好写的样子,没想到墨迹了半天. string add(string a, string b,int lena,int lenb) { if(lena<lenb) return add(b,a,lenb,lena); //

【leetcode】Isomorphic Strings(easy)

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.