2019-02-21-算法-进化
生活随笔
收集整理的這篇文章主要介紹了
2019-02-21-算法-进化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
論代碼能力:
給出兩個 非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式存儲的,并且它們的每個節點只能存儲 一位 數字。
如果,我們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。
您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。
示例:
輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 輸出:7 -> 0 -> 8 原因:342 + 465 = 807主方法為:addTwoNumbers
自己最初的解題代碼:
public class Test {public static void main(String[] args) throws InterruptedException { // 輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4) // 輸出:7 -> 0 -> 8 // 原因:342 + 465 = 807 // [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] // [5,6,4]ListNode aNode = initNode(new int[]{5});ListNode bNode = initNode(new int[]{5});ListNode cNode = addTwoNumbers(aNode, bNode);System.out.println(JSONObject.toJSONString(cNode));}public static ListNode initNode(int [] arr) {ListNode head = new ListNode(arr[0]);ListNode node = head;for(int i=1;i<arr.length;i++) {ListNode tNode = new ListNode(arr[i]);node.next = tNode;node = node.next;}return head;}public static class ListNode {int val;ListNode next;public ListNode(int val) {this.val = val;}}public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {List<Integer> arr1 = new ArrayList<Integer>();List<Integer> arr2 = new ArrayList<Integer>();ListNode t1 = l1;ListNode t2 = l2;while (t1!=null) {arr1.add(t1.val);t1 = t1.next;}while (t2!=null) {arr2.add(t2.val);t2 = t2.next;}List<Integer> arr3 = null;arr3 = getResultList(arr1.size() > arr2.size()?arr1:arr2, arr1.size() > arr2.size()?arr2:arr1);ListNode head = new ListNode(arr3.get(0));ListNode node = head;for(int i=1;i<arr3.size();i++) {ListNode tNode = new ListNode(arr3.get(i));node.next = tNode;node = node.next;}return head;}public static List<Integer> getResultList(List<Integer> longList, List<Integer> shortList) {List<Integer> arr3 = new ArrayList<Integer>();int jinJi = 0;for(int i=0;i<longList.size();i++) {int a = longList.get(i);int b=0;if(i<shortList.size()) {b=shortList.get(i);}int count = a+b + jinJi;if(count>9) {jinJi = 1;}else {jinJi = 0;}arr3.add(count % 10);}if(jinJi != 0) {arr3.add(jinJi);}return arr3;}}參照標準答案重寫的代碼:
public class Test {public static void main(String[] args) throws InterruptedException { // 輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4) // 輸出:7 -> 0 -> 8 // 原因:342 + 465 = 807 // [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] // [5,6,4]ListNode aNode = initNode(new int[]{5});ListNode bNode = initNode(new int[]{5});ListNode cNode = addTwoNumbers(aNode, bNode);System.out.println(JSONObject.toJSONString(cNode));}public static ListNode initNode(int [] arr) {ListNode head = new ListNode(arr[0]);ListNode node = head;for(int i=1;i<arr.length;i++) {ListNode tNode = new ListNode(arr[i]);node.next = tNode;node = node.next;}return head;}public static class ListNode {int val;ListNode next;public ListNode(int val) {this.val = val;}}public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {//啞節點ListNode yaNode = new ListNode(0);ListNode head = yaNode;int jinJi = 0;int benJi =0;while (l1!=null || l2!=null) {int temp = (l1 == null?0:l1.val) + (l2==null?0:l2.val) + jinJi;jinJi = temp>9?1:0;benJi = temp % 10;ListNode node = new ListNode(benJi);head.next = node;head = head.next;if(l1 != null) {l1 = l1.next;}if(l2 != null) {l2 = l2.next;}}if(jinJi != 0) {ListNode node = new ListNode(jinJi);head.next = node;}return yaNode.next;}部分思路(按照人類解題,加法超過9進級的思路)是類似的,但是局部進行了優化(不用借助ArrayList,直接使用鏈表遍歷即可)
減少了內存占用,并且代碼更加的簡潔易懂,不易出錯。繼續努力吧!
總結
以上是生活随笔為你收集整理的2019-02-21-算法-进化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java工程师修炼之路
- 下一篇: Java修炼之路——基础篇——平台无关性