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

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

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

/*
 * @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

 

 

 

 

 

 

 

你可能感兴趣的:(研究生涯的300算法)