摘要:感想剛開(kāi)始看到這道題,覺(jué)得很簡(jiǎn)單,跟歸并的過(guò)程比較像,算法復(fù)雜度。可以半小時(shí)之內(nèi)完全可以搞定。可是隨著一次次提交出問(wèn)題,發(fā)現(xiàn)似乎沒(méi)有我想的那么簡(jiǎn)單問(wèn)題其實(shí)就出現(xiàn)在各種沒(méi)有處理好,劍指多次提到的寫(xiě)代碼要注意健壯性深刻體會(huì)到了。
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
感想: 剛開(kāi)始看到這道題,覺(jué)得很簡(jiǎn)單,跟歸并merge的過(guò)程比較像,算法復(fù)雜度O(n)。可以半小時(shí)之內(nèi)完全可以搞定。可是隨著一次次提交出問(wèn)題,發(fā)現(xiàn)似乎沒(méi)有我想的那么簡(jiǎn)單...問(wèn)題其實(shí)就出現(xiàn)在各種corner cases沒(méi)有處理好,which is also 劍指offer多次提到的寫(xiě)代碼要注意健壯性....深刻體會(huì)到了。
思路: 處理好進(jìn)位,要自己手動(dòng)寫(xiě)各種情況的測(cè)試用例,確保程序的健壯性...
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { private int getSum(int x, int y, int digit){ int value = x+y+digit; if(value>=10) return value-10; else return value; } public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode p = l1; ListNode q = l2; // new head node for the resultlist ListNode newHead = new ListNode(getSum(p.val,q.val,0)); ListNode pHead = newHead; int digit;//處理進(jìn)位 if(p.val+q.val-10 >= 0){ digit = 1; } else digit = 0; p = p.next; q = q.next; while(p!=null || q != null){ // 兩個(gè)鏈表長(zhǎng)度相等時(shí)候的處理 if(p!=null &&q!=null){ ListNode temp = new ListNode(getSum(p.val, q.val,digit)); pHead.next = temp; pHead = pHead.next; if(p.val+q.val+digit-10 >= 0){ digit = 1; } else digit = 0; p = p.next; q = q.next; } // 兩個(gè)鏈表長(zhǎng)度不等時(shí)候的處理 else if(p==null){ ListNode temp = new ListNode(getSum(0, q.val,digit)); pHead.next = temp; pHead = pHead.next; if(0+q.val-10+digit >= 0){ digit = 1; } else{ digit = 0; } q = q.next; } // 兩個(gè)鏈表長(zhǎng)度不等時(shí)候的處理 else if(q==null){ ListNode temp = new ListNode(getSum(p.val, 0,digit)); pHead.next = temp; pHead = pHead.next; if(0+p.val-10+digit >= 0){ digit = 1; } else{ digit= 0; } p = p.next; } } // 鏈表1: 5 鏈表2: 5 這個(gè)時(shí)候的處理 if(digit ==1){ ListNode temp = new ListNode(getSum(0, 0,digit)); pHead.next = temp; } return newHead; } }
更優(yōu)美一點(diǎn)的代碼
javapublic class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry =0; ListNode newHead = new ListNode(0); ListNode p1 = l1, p2 = l2, p3=newHead; while(p1 != null || p2 != null){ if(p1 != null){ carry += p1.val; p1 = p1.next; } if(p2 != null){ carry += p2.val; p2 = p2.next; } p3.next = new ListNode(carry%10); p3 = p3.next; carry /= 10; } if(carry==1) p3.next=new ListNode(1); return newHead.next; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/64315.html
摘要:難度題目給定兩個(gè)非空且元素非負(fù)的鏈表。鏈表中的數(shù)字以逆序排列且每個(gè)結(jié)點(diǎn)只含一個(gè)一位數(shù)。使兩個(gè)數(shù)相加并反回其結(jié)果。思路設(shè)置頭結(jié)點(diǎn)簡(jiǎn)化操作。從前向后遍歷相加。 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse or...
摘要:這題是說(shuō)給出兩個(gè)鏈表每個(gè)鏈表代表一個(gè)多位整數(shù)個(gè)位在前比如代表著求這兩個(gè)鏈表代表的整數(shù)之和同樣以倒序的鏈表表示難度這個(gè)題目就是模擬人手算加法的過(guò)程需要記錄進(jìn)位每次把對(duì)應(yīng)位置兩個(gè)節(jié)點(diǎn)如果一個(gè)走到頭了就只算其中一個(gè)的值加上進(jìn)位值 Add Two Numbers You are given two linked lists representing two non-negative num...
摘要:描述中文解釋給定兩個(gè)非空的鏈表里面分別包含不等數(shù)量的正整數(shù),每一個(gè)節(jié)點(diǎn)都包含一個(gè)正整數(shù),肯能是,但是不會(huì)是這種情況。我們需要按照倒序計(jì)算他們的和然后再次倒序輸出。 描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in rev...
Problem You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and ...
摘要:題目要求對(duì)以鏈表形式的兩個(gè)整數(shù)進(jìn)行累加計(jì)算。思路一鏈表轉(zhuǎn)置鏈表形式跟非鏈表形式的最大區(qū)別在于我們無(wú)法根據(jù)下標(biāo)來(lái)訪問(wèn)對(duì)應(yīng)下標(biāo)的元素。因此這里通過(guò)先將鏈表轉(zhuǎn)置,再?gòu)淖笸覍?duì)每一位求和來(lái)進(jìn)行累加。通過(guò)棧可以實(shí)現(xiàn)先進(jìn)后出,即讀取順序的轉(zhuǎn)置。 題目要求 You are given two non-empty linked lists representing two non-negative i...
閱讀 1909·2021-11-24 11:16
閱讀 3262·2021-09-10 10:51
閱讀 3209·2021-08-03 14:03
閱讀 1268·2019-08-29 17:03
閱讀 3249·2019-08-29 12:36
閱讀 2237·2019-08-26 14:06
閱讀 500·2019-08-23 16:32
閱讀 2688·2019-08-23 13:42