【刷题】Leetcode 2. Add Two Numbers

**

2. Add Two Number

**

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {     
        int jinwei = 0;
        ListNode head = new ListNode(0);
        ListNode p = head;
        while(l1 != null || l2 != null || jinwei != 0){   
            int result = 0;    
            if(l1 != null){
                   result += l1.val;  
                   l1 = l1.next;
               }          
               if(l2 != null){
                   result += l2.val;
                   l2 = l2.next;
               }
            result += jinwei;
            p.next = new ListNode(result % 10);
            p = p.next;
            jinwei = result / 10;
        }
        return head.next;
    }
}

**

Add Strings

**

class Solution {
    public String addStrings(String num1, String num2) { 
        StringBuffer sb = new StringBuffer();
        int jinwei = 0;
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int k = 0;
        while(i >= 0 || j >= 0 || jinwei != 0){
            int x = 0;
            if(i >= 0){
                x += num1.charAt(i--) - '0';                
            }
            if(j >= 0){
                x += num2.charAt(j--)- '0';
            }
            x += jinwei;
            sb.insert(0, x % 10);
            jinwei = x/10;
        }

        return sb.toString();
    }
}

989. Add to Array-Form of Integer

Example 1:

Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234

class Solution {
    public List<Integer> addToArrayForm(int[] A, int K) {
        int i = A.length - 1;
        int jinwei = 0;
        List<Integer> result = new ArrayList<>();
        while(i >= 0 || K != 0 || jinwei != 0){
            int x = 0;
            if(i >= 0){
                x += A[i--];
            }
            if(K != 0){
                x += K % 10;
                K /= 10;
            }
            x += jinwei;
            result.add(0, x % 10);
            jinwei = x / 10;
        }
        return result;
    }
}

**

66. Plus One

**

class Solution {
    public int[] plusOne(int[] digits) {
        int i = digits.length - 1   
        while(i >= 0){
            int result = 0;
            if(digits[i] != 9){
                digits[i]++;
                return digits;
            }            
            digits[i--] = 0;
        }
        int[] arr = new int[digits.length + 1];
        arr[0] = 1;
        return arr;
    }
}

43. Multiply Strings

class Solution {
    public String multiply(String num1, String num2) {
        int[] result = new int[num1.length() + num2.length()];
        for(int i = num1.length() - 1; i >= 0; i--){
            for(int j = num2.length() - 1; j >= 0; j--){
                int sum = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                int p = i + j, q = i + j + 1;
                sum += result[q];
                result[q] = sum % 10;
                result[p] += sum / 10;
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int x: result){
           if(sb.length() == 0 && x == 0)
               continue;
           sb.append(x);
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }
}

出错的地方:

  • if(sb.length() == 0 && x == 0)
    continue;
    在这里插入图片描述
  • return sb.length() == 0 ? “0” : sb.toString();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值