zoukankan      html  css  js  c++  java
  • [leetcode]2-Add Two Numbers

    2. Add Two Numbers

    1)题目

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
    
    You may assume the two numbers do not contain any leading zero, except the number 0 itself.
    
    Example:
    
    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.
    

    2)思路

    其实就是简单的加法。
    第一位就是各位,往上累加即可。

    3) 代码

    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
                    ListNode result = new ListNode(-1);
            ListNode p = result;
            ListNode a = l1;
            ListNode b = l2;
            int flag = 0;
            while (a!=null || b!= null) {
                int add1 = a == null ? 0 : a.val;
                int add2 = b == null ? 0 : b.val;
                int sum = add1 + add2 + flag;
                flag = sum >= 10? 1:0;
                int val = sum%10 ;
                ListNode temp = new ListNode(val);
                p.next = temp;
                p = temp;
                if (a != null) {
                    a = a.next;
                }
                if (b != null) {
                    b = b.next;
                }
            }
            if (flag ==1) {
                p.next = new ListNode(1);
            }
            return result.next;
    
        }
    }
    

    4) 结果

    时间复杂度:O(n)
    空间复杂度:O(1)
    耗时:52 ms

    5) 调优

    看了下时间少的算法,基本逻辑是一致的。 我运行了一遍 耗时和这差不多。
    官网可能由于啥偶然因素导致他的代码毕竟快吧。

  • 相关阅读:
    InitializingBean
    执行jar的记事本
    vue中$forceUpdate()事件
    帆软时间检索限制90天
    vue清楚子组件v-model绑定的值
    Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
    Java将CST的时间字符串转换成需要的日期格式字符串
    axios发送命令如何实现同步
    利用tomcat启动web前端
    vue生命周期
  • 原文地址:https://www.cnblogs.com/novaCN/p/9872114.html
Copyright © 2011-2022 走看看