zoukankan      html  css  js  c++  java
  • [LeetCode] 445. 两数相加 II

    自己想的太复杂了。。。。。

    /**
     * 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 pre=null;
            ListNode head=null;
            ListNode l1node=l1,l2node=l2;
            pre.next=head;
            int l1len=0;
            int l2len=0;
            whille(l1node!=null){
                l1len++;
                l1node=l1node.next;
            }
            whille(l2node!=null){
                l2len++;
                l2node=l2node.next;
            }
            if(l1len>l2len){
                int m=l1len-l2len;
                while(m!=0){
                    head=l1;
                    pre=head;
                    head=head.next;
                    l1=l1.next; 
                    m--; 
                }
            }else{
                int m=l2len-l1len;
                while(m!=0){
                    head=l1;
                    pre=head;
                    head=head.next;
                    l2=l2.next; 
                    m--; 
                }
            }
            while(l1!=null){
                int c1=l1==null?0:c1.val;
                int c2=l2==null?0:c2.val;
                int sum=(c1+c2)%10;
                int carry=(c1+c2)/10;
                if(carry==1) pre+=1;
                head=sum;
                pre=sum;
                head=head.next;
            }
        }
    }
    View Code

    然而可以用栈放进去,出来就是数字从低位到高位了。

    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 
            Stack<Integer> stack1 = new Stack<>();
            Stack<Integer> stack2 = new Stack<>();
            while (l1 != null) {
                stack1.push(l1.val);
                l1 = l1.next;
            }
            while (l2 != null) {
                stack2.push(l2.val);
                l2 = l2.next;
            }
            
            int carry = 0;
            ListNode head = null;
            while (!stack1.isEmpty() || !stack2.isEmpty() || carry > 0) {
                int sum = carry;
                sum += stack1.isEmpty()? 0: stack1.pop();
                sum += stack2.isEmpty()? 0: stack2.pop();
                ListNode node = new ListNode(sum % 10);
                node.next = head;
                head = node;
                carry = sum / 10;
            }
            return head;
        }
    }

    或者是直接翻转链表,然后按照第2题那样的方法去做更快

    /**
     * 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 dummyHead = new ListNode(0);
            ListNode p = reverseList(l1);
            ListNode q = reverseList(l2);
            ListNode cur = dummyHead;
            int carry = 0;
            while (p != null || q != null){
                int x = (p != null) ? p.val : 0;
                int y = (q != null) ? q.val : 0;
                int sum = x + y + carry;
                carry = sum/10;
                cur.next = new ListNode(sum%10);
                cur = cur.next;
                if (p != null){
                    p = p.next;
                }
                if (q != null){
                    q = q.next;
                }
            }
            if (carry == 1){
                cur.next = new ListNode(carry);
            }
            return reverseList(dummyHead.next);
        }
        public static ListNode reverseList(ListNode cur) {
            ListNode pre = null;
            while (cur != null) {
                ListNode temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
    }

  • 相关阅读:
    SQL表结构
    Mssql 行转列
    动态Order by
    Nopi Excel导入
    使用SyncToy 同步两台机器上的文件夹
    ueditor1.4.3 在IE8下的 BUG
    WebService国内省市县接口
    AsyncTask的参数介绍
    Json分割并解析
    JQuery iframe页面操作父页面中的元素与方法
  • 原文地址:https://www.cnblogs.com/doyi111/p/12702260.html
Copyright © 2011-2022 走看看