zoukankan      html  css  js  c++  java
  • 2. Add Two Numbers

    就是先加,加到其中一个是NULL

    都是NULL 就看进不进位,进位就加1 返还RES.NEXT

    其中一个不是的话 找到不是的那个

    再加,最后再看进不进位

    主要就是加完之后有2次进位要注意

    逻辑上不难 琐碎太多

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) 
    	{
            if(l1 == null || l2 == null) return l1==null? l2:l2==null?l1:null;
            
            ListNode res = new ListNode(0);
            ListNode temp = res;
            ListNode temp1 = l1;
            ListNode temp2 = l2;
            int v = 0;
            int rem = 0;
            while(temp1!=null && temp2!=null)
            {
                if(rem == 0) v = 0;
                else v = 1;
                //System.out.print(v);
                int sum = temp1.val + temp2.val + v;
                //System.out.println(sum);
                if(sum>=10)
                {
                    rem = 1;
                    sum%=10;
                }
                else rem = 0;
                
    
                temp.next = new ListNode(sum);
                temp = temp.next;
                temp1 = temp1.next;
                temp2 = temp2.next;
                //System.out.print(rem);
                
            }
            if(temp1 == null && temp2 == null)
            {
                //System.out.print(rem);
                
                if(rem == 1) temp.next = new ListNode(1);
            
                return res.next;
            }
            ListNode cur = temp1!=null? temp1:temp2;
            
            while(cur!=null)
            {
                if(rem == 1) v = 1;
                else v = 0;
                
                v+=cur.val;
                if(v>=10)
                {
                    rem = 1;
                    v = 0;
                }
                else rem = 0;
                
                temp.next = new ListNode(v);
                temp = temp.next;
                cur = cur.next;
                
            }
            
            if(rem == 1) temp.next = new ListNode(1);
            
            return res.next;
    
        }
    



    二刷。

    感觉没什么巧办法,就是楞做,楞做也不难。

    要点有2个:
    处理进位
    处理NULL

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) 
        {
            if(l1 == null || l2 == null) return l1 == null? l2:l1;
            
            ListNode dummy = new ListNode(-1);
            
            ListNode temp1 = l1;
            ListNode temp2 = l2;
            ListNode temp = dummy;
            int carry = 0;
            
            while(temp1 != null && temp2 != null)
            {
                int tempVal = temp1.val + temp2.val + carry;
                if(tempVal >= 10) carry = 1;
                else carry = 0;
                
                tempVal = tempVal%10;
                temp.next = new ListNode(tempVal);
                temp = temp.next;
                
                temp1 = temp1.next;
                temp2 = temp2.next;
                
            }
            
            if(temp1 == null) temp1 = temp2;
            
            while(temp1 != null)
            {
                int tempVal = temp1.val + carry;
                if(tempVal >= 10) carry=1;
                else carry = 0;
                
                tempVal = tempVal%10;
                temp.next = new ListNode(tempVal);
                temp = temp.next;
                
                temp1 = temp1.next;
            }
            
            if(carry == 1) temp.next = new ListNode(1);
            
            return dummy.next;
            
            
        }
    }
    



    三刷就是在打二刷的脸。

    一个while loop解决。

    public class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if (l1 == null || l2 == null) return l1 == null? l2:l1;
            ListNode dummy = new ListNode(0);
            ListNode l0 = dummy;
            int carry = 0;
            while (l1 != null || l2 != null || carry > 0) {
                int a = l1 == null ? 0 : l1.val;
                int b = l2 == null ? 0 : l2.val;
                l0.next = new ListNode((a+b+carry)%10);
                l0 = l0.next;
                carry = (a + b + carry) / 10;
                if (l1 != null) l1 = l1.next;
                if (l2 != null) l2 = l2.next;
            }
            return dummy.next;
        }
    }
    
  • 相关阅读:
    Kubernetes 1.5部署sonarqube
    Kubernetes 1.5集成heapster
    Kubernetes 1.5 配置dashboard
    SQL SERVER中的逻辑读取,物理读取,以及预读的理解
    JS控制显示/隐藏二级菜单
    Css下拉菜单设置
    div包裹页面后多余部分没有显示,也没滚动条 overflow 属性设置
    Sql Ado.net 学习笔记之连接字符串
    Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
    SQL中的字母的大小写转换
  • 原文地址:https://www.cnblogs.com/reboot329/p/5874195.html
Copyright © 2011-2022 走看看