美文网首页Java 杂谈
LeetCode算法题:两数相加

LeetCode算法题:两数相加

作者: z七夜 | 来源:发表于2018-06-25 21:43 被阅读2次

    需求

    两数相加:


    需求

    思路

    链表是倒序存储数值,432 在链表存储是2-3-4,当求和之后,还需将值转成链表,
    1.根据链表,得到链表存储的值,用集合存储
    2.将集合中存储的值,转换成真正的数值
    3.求和
    4.将值转成集合
    5.将集合转成链表

    实现

    package jk.zmn.suanfa; /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
       2  4 5
        542
    
      思路:
       将链表转成数组,
       根据数组生成值
       计算总和
       生成链表
    
     */
    import java.util.*;
    
    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    
            ArrayList<Integer> integers = listnodetoList(l1);
            Collections.reverse(integers);
            ArrayList<Integer> integers1 = listnodetoList(l2);
            Collections.reverse(integers1);
            int i = listtoNumber(integers);
            int i1 = listtoNumber(integers1);
            int sum = i +i1;
            ArrayList<Integer> integers2 = numbertoList(sum);
            ListNode head = listtolistnode(integers2);
    
            return head;
        }
    
    
        //链表转成数组
        public ArrayList<Integer> listnodetoList(ListNode l1){
            ArrayList<Integer> list1 = new ArrayList<Integer>();
            while(l1 != null){
                list1.add(l1.val);
                l1 = l1.next;
            }
            return list1;
        }
    
        //根据集合生成值
        public int listtoNumber(ArrayList<Integer> list){
            int num=0;
            for(int i=0;i<list.size();i++){
                num+=list.get(i)*Math.pow(10,list.size()-1-i);
            }
            return num;
        }
        // 234  432根据值生成逆向集合
        public ArrayList<Integer> numbertoList(int number){
    
            ArrayList<Integer> integers = new ArrayList<>();
            while (number / 10 >= 0 ){
                int gewei = number % 10  ;
                integers.add(gewei);
                if (number /10 == 0){
                    break;
                }
                number = number/10;
            }
            return integers;
        }
    
        //集合转链表
        public ListNode listtolistnode(ArrayList<Integer> list1){
            ListNode head = null;
            ListNode rear = null;
            for(int i=0;i < list1.size(); i++){
    
                if ( head == null)
                {
                    head = new ListNode(list1.get(i));
                    rear = head;
                    continue;
                }
                rear.next = new ListNode(list1.get(i));
                rear = rear.next;
            }
            ListNode temp = head;
            while (temp != null){
                System.out.println(temp.val+" ");
                temp = temp.next;
            }
            System.out.println();
            return head;
        }
    
        public static void main(String[] args){
            ListNode n1 = new ListNode(9);
    
            ListNode n4 = new ListNode(1);
            ListNode n5 = new ListNode(9);
            ListNode n6 = new ListNode(9);
            n4.next = n5;
            n5.next = n6;
    
            //new Solution().swap(n1);
            ListNode listNode = new Solution().addTwoNumbers(n1, n4);
            while (listNode != null){
                System.out.println(listNode.val);
                listNode = listNode.next;
            }
    
        }
    
        static class ListNode {
            int val;
            ListNode next;
            ListNode(int x) { val = x; }
        }
    }
    

    效果如图:

    public static void main(String[] args){
            ListNode n1 = new ListNode(9);
    
            ListNode n4 = new ListNode(1);
            ListNode n5 = new ListNode(9);
            ListNode n6 = new ListNode(9);
            n4.next = n5;
            n5.next = n6;
    
            //new Solution().swap(n1);
            ListNode listNode = new Solution().addTwoNumbers(n1, n4);
            while (listNode != null){
                System.out.println(listNode.val);
                listNode = listNode.next;
            }
    
        }
    
    image.png

    QQ交流群:552113611
    更多资源与源码,请加群

    相关文章

      网友评论

        本文标题:LeetCode算法题:两数相加

        本文链接:https://www.haomeiwen.com/subject/igsuyftx.html