LeetCode解题记录(2)
2015-04-10 17:57
429 查看
LeetCode解题记录(2)
目录
LeetCode解题记录2目录
前言
正文
题目
解法一
前言
我将慢慢开始做LeetCode上的题,并做解题记录发布在这里。我每题会给出一到多个解法,记录思考过程。我算法巨烂,是想通过这种方式稍微补补,基本功和我一样差的小伙伴可以和我一起共勉,有大神路过可以指点一二,我感激不尽。解题的最底要求是能通过LeetCode的检测,我不会丧病的为了各种提高效率在一个题上纠缠不休,所以最终解法可能也不怎么样,大家看看就好~编程语言一律采用C#。题解代码会按编号发布到我的GitHub上(仅保留最终解法)。正文
题目
problem002: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
解法一
只想到一个解法,在visual studio中验证后提交就通过了,所以就没纠结了:/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode result = null; ListNode temp = null; int duoyu = 0; while(l1 != null || l2 != null || duoyu == 1) { int value = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + duoyu; if (value >= 10) { value -= 10; duoyu = 1; } else { duoyu = 0; } if (temp == null){ temp = new ListNode(value); result = temp; } else { temp.next = new ListNode(value); temp = temp.next; } l1 = l1 == null ? null : l1.next; l2 = l2 == null ? null : l2.next; } temp.next = null; return result; } }
106ms。
相关文章推荐
- [leetcode] database解题记录
- 2015.03.28 leetcode LRU cache 解题记录
- leetcode小白解题记录——第六题
- leetcode小白解题记录——第十一题
- leetcode小白解题记录——第十题
- [LeetCode]Valid Sudoku解题记录
- Leetcode Database 18道题解题记录
- leetcode小白解题记录——第五题
- Leetcode解题记录
- LeetCode 318. Maximum Product of Word Lengths解题记录
- 2015.03.30 LeetCode Merge Intervals 解题记录
- LeetCode解题记录(6)
- LeetCode解题记录(5)
- LeetCode 31 Next Permutation 解题记录
- leetcode小白解题记录——第二题
- [leetcode解题记录]Jump Game和Jump Game II
- LeetCode解题记录(4)
- LeetCode解题记录(8)
- leetcode小白解题记录——第九题
- [LeetCode]Valid Sudoku解题记录