卜若的代码笔记-算法系列-第七个算法案例分析:两数相加

这个题目乍看一眼挺简单,两个链表循环求和除余就搞定了,但是,如果你这样搞就会出现一个有意思的问题 ,溢出:

/*
 * @lc app=leetcode.cn id=2 lang=java
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        
        ListNode nd = l1;
        long s1 = 0;
        int index = 0;
        while(nd!= null){
            s1 += nd.val*Math.pow(10,index);
            nd = nd.next;
            index++;
        }
        index = 0;

        long s2 = 0;
        ListNode nd2 = l2;
        while(nd2!= null){

            s2 += nd2.val*Math.pow(10,index);
            nd2 = nd2.next;
            index = index+1;
        }
        long sum = s1+s2;

        ListNode ndx = null;
        ListNode head = null;
         if(sum == 0){
            
            ListNode nds = new ListNode();
            nds.val = 0;
            return nds;
        }

        System.out.println(sum);
        //807
        while(sum != 0){
            
            
            if(ndx == null){

                ndx = new ListNode();
                head = ndx;
                System.out.println(sum);
                System.out.println((int)sum%10);
                ndx.val = (int)sum%10;
                System.out.println(ndx.val+"<><>");
                
            }else{
                ListNode temp = new ListNode();

                temp.val = (int)sum%10;
                ndx.next = temp;
                ndx = ndx.next;

            }
            System.out.println(sum%10);
            sum = sum/10;

        }
       
        ListNode temp = head;
        while(temp!= null){

            System.out.println(temp.val);
            temp  = temp.next;
        }



        // System.out.println(s1+"<><>><>"+s2);

        return head;

    }
}
// @lc code=end

这是我一开始写的代码,写完之后,无法通过,挺扯淡的,然后搜了下,发现,解决问题的思路是通过进位去解决。

2,4,3+5,6,4

初始化进位carry = 0;

2+5+carry  = 7,没有进位,7入链表

4+6+carry = 10,有进位,carry = 1,0入链表

3+4+carry = 8,没有进位,8如链表

按照这个思路写下去,其实还是挺简单的

/*
 * @lc app=leetcode.cn id=2 lang=java
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        
        ListNode nd = l1;

        int index = 0;
        int carry = 0;


        ListNode t1 = l1;
        ListNode t2 = l2;
        ListNode head = null;
        ListNode temp = null;
        int t1v = 0;
        int t2v = 0;
        while(true){
            if(t1 == null){
                t1v = 0;
                
            }else{

                t1v = t1.val;
                t1 = t1.next;

            }
            if(t2 == null){
                t2v = 0;

            }else{

                t2v = t2.val;
                t2 = t2.next;
            }
            
            int vl = t1v + t2v+carry;
            carry = vl/10;

            if(head == null){

                head = new ListNode();
                head.val = vl%10;
                temp = head;
            }else{
                ListNode temp3 = new ListNode();
                temp3.val = vl%10;
                temp.next = temp3;
                temp = temp.next;
            }

            if(t1==null&&t2 == null){
                
                //注意l1 = [5] l2 = [5]这种情况
                if(carry != 0){

                    ListNode temp3 = new ListNode();
                    temp3.val = carry;
                    temp.next = temp3;
                    temp = temp.next;
                }

                break;
            }


            

        }

        // System.out.println(s1+"<><>><>"+s2);

        return head;

    }
}
// @lc code=end

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值