【leetcode】2. Add Two Numbers
来源:互联网 发布:微软sql server 编辑:程序博客网 时间:2024/05/20 20:48
不能只刷简单题了。从头开始刷,也算加大强度吧。
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
测试用例, 1 999是最好的。
这个考进位,不同的是序列。
自己写的这个分成两部分,一部分是l1,l2一起的同时的时候,其次是处理其中一个不存在的时候,其实是可以把不存在当成0来处理,这样就可以了么。
Your runtime beats 23.66% of javascriptsubmissions.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */var addTwoNumbers = function(l1, l2) { var head = new ListNode(-1); var node = head; var singleAdd=function(ll,added,node){ if(added === 0){ node.next = ll; }else{ var val ; if(ll !== null){ val = ll.val; var num= val+added; var temp_flag = 0; if(num >= 10){ temp_flag = 1; num -= 10; } node.next = new ListNode(num); node = node.next; singleAdd(ll.next,temp_flag,node); }else{ node.next = new ListNode(1); } } }; var c_flag = 0; while(l1!== null){ if(l2!==null){ var added_num = l1.val+l2.val+c_flag; c_flag = 0; if(added_num>=10){ c_flag = 1; added_num = added_num -10; } var temp = new ListNode(added_num); node.next = temp; node = node.next; l1 = l1.next; l2 = l2.next; }else{ singleAdd(l1,c_flag,node); return head.next; } } singleAdd(l2,c_flag,node); return head.next;};
逻辑清楚了很多,只是没有
Your runtime beats 32.06% of javascriptsubmissions.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */var addTwoNumbers = function(l1, l2) { var head = new ListNode(-1); var node = head; var c_flag=0; while(l1!== null||l2!==null||c_flag>0){ var val1 = l1===null?0:l1.val; var val2 = l2 === null ?0:l2.val; var added_num = val1+val2+c_flag; c_flag = 0; if(added_num>=10){ c_flag = 1; added_num = added_num -10; } var temp = new ListNode(added_num); node.next = temp; node = node.next; if(l1!==null){ l1 = l1.next; } if(l2!==null){ l2 = l2.next; } } return head.next;};
0 0
- [LeetCode]2.Add Two Numbers
- LeetCode 2.Add Two Numbers
- LeetCode --- 2. Add Two Numbers
- [Leetcode] 2. Add Two Numbers
- 【leetcode】2. Add Two Numbers
- leetcode 2. Add Two Numbers
- LeetCode-2.Add Two Numbers
- [leetcode] 2. Add Two Numbers
- 【LeetCode】2. Add Two Numbers
- LeetCode - 2. Add Two Numbers
- LeetCode 2.Add Two Numbers
- LeetCode 2. Add Two Numbers
- Leetcode 2. Add Two Numbers
- 2. Add Two Numbers LeetCode
- leetcode 2. Add Two Numbers
- LeetCode-2. Add Two Numbers
- LeetCode 2. Add Two Numbers
- 2. Add Two Numbers----LeetCode
- 第十六周上机实践项目1(3):阅读程序
- 在vs中建立web项目和web网站的区别
- leetcode--Roman to Integer
- Pig 学习之 日志处理
- 53. Maximum Subarray
- 【leetcode】2. Add Two Numbers
- android中生成随机点
- 第15 章 输入/输出
- 注册表写入读取和启动项删除
- Python爬虫实践(五):Cookie
- 第十六周上机实践项目2(1):阅读程序
- 一句话概括面向对象基本原则
- 新的博客,新的记录
- python 正则表达式