日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode-2-两数相加

發布時間:2024/9/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode-2-两数相加 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

問題:

?

?

package com.nxz.blog.otherTest;public class TestLeetCode {public static void main(String[] args) {TestLeetCode t = new TestLeetCode();ListNode l1 = new ListNode(2);ListNode l2 = new ListNode(4);ListNode l3 = new ListNode(3);ListNode r1 = new ListNode(5);ListNode r2 = new ListNode(6);ListNode r3 = new ListNode(4);l1.next = l2;l2.next = l3;r1.next = r2;r2.next = r3;ListNode listNode = t.addTwoNumbers(l1, r1);do {System.out.println(listNode.val);} while ((listNode = listNode.next )!= null);}/*** 循環 l1 和 l2 兩個listnode,當l1 或 l2 不為null的時候,將兩個數相加,大于10時進行處理(保存進一位),否則視為0和另一個值相加** @param l1* @param l2* @return*/public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
     //啞結點,最終返回的節點就是該節點的下一個節點ListNode dummyHead
= new ListNode(-1);//暫存變量ListNode p = l1, q = l2, curr = dummyHead;//大于10時的暫存值int carry = 0;//當p或q不為null是,將值相加,此時需要處理,其中一個為null的情況while (p != null || q != null) {//只要p或q為nul時,將val值默認為0,和另一個值相加int x = p != null ? p.val : 0;int y = q != null ? q.val : 0;int sum = carry + x + y;//重新設置進一變量carry = sum / 10;curr.next = new ListNode(sum % 10);//重新設置當前節點,p,q節點,以便再一次循環curr = curr.next;if (p != null) {p = p.next;}if (q != null) {q = q.next;}}//最后處理最高為的carry的值if (carry > 0) {curr.next = new ListNode(carry);}return dummyHead.next;}public static class ListNode {int val;ListNode next;ListNode(int x) {val = x;}} }

?

?

?進階----->>>>

兩數相加:節點為正序的情況

package com.nxz.blog.otherTest;import java.util.Stack;public class TestLeetCode {public static void main(String[] args) {TestLeetCode t = new TestLeetCode();ListNode l1 = new ListNode(2);ListNode l2 = new ListNode(4);ListNode l3 = new ListNode(3);ListNode r1 = new ListNode(5);ListNode r2 = new ListNode(6);ListNode r3 = new ListNode(4);l1.next = l2;l2.next = l3;r1.next = r2;r2.next = r3;ListNode listNode = t.addTwoNumbers(l1, r1);do {System.out.println(listNode.val);} while ((listNode = listNode.next) != null);}/*** 利用棧這種數據結構(先進后出),這樣就可以將正序的節點轉換為倒敘的節點了,這樣依次從兩個棧中取出值(順序就是個、十、百。。。)* 這樣就轉換為之前的那種方式了** @param l1* @param l2* @return*/public ListNode addTwoNumbers(ListNode l1, ListNode l2) {Stack<ListNode> stack1 = new Stack<>();Stack<ListNode> stack2 = new Stack<>();ListNode dummyHead = new ListNode(-1);ListNode p = l1, q = l2, head = dummyHead;while (p != null) {stack1.add(p);p = p.next;}while (q != null) {stack2.add(q);q = q.next;}p = stack1.pop();q = stack2.pop();int carry = 0;while (p != null || q != null) {int x = p != null ? p.val : 0;int y = q != null ? q.val : 0;int sum = carry + x + y;carry = sum / 10;ListNode last = head.next;head.next = new ListNode(sum % 10);head.next.next = last;p = stack1.isEmpty() ? null : stack1.pop();q = stack2.isEmpty() ? null : stack2.pop();}if (carry > 0) {ListNode last = head.next;head.next = new ListNode(carry);head.next.next = last;}return dummyHead.next;}public static class ListNode {int val;ListNode next;ListNode(int x) {val = x;}} }

?

總結

以上是生活随笔為你收集整理的leetcode-2-两数相加的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。