diff --git a/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s1_two_sum/Solution.java b/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s1_two_sum/Solution.java index 21fb392f..b541dff1 100644 --- a/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s1_two_sum/Solution.java +++ b/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s1_two_sum/Solution.java @@ -3,15 +3,15 @@ import java.util.ArrayList; /** - * 1. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 + * 1. 缁欏畾涓涓暣鏁版暟缁勫拰涓涓洰鏍囧硷紝鎵惧嚭鏁扮粍涓拰涓虹洰鏍囧肩殑涓や釜鏁般 * - * 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 + * 浣犲彲浠ュ亣璁炬瘡涓緭鍏ュ彧瀵瑰簲涓绉嶇瓟妗堬紝涓斿悓鏍风殑鍏冪礌涓嶈兘琚噸澶嶅埄鐢ㄣ * - * 示例: + * 绀轰緥: * - * 给定 nums = [2, 7, 11, 15], target = 9 + * 缁欏畾 nums = [2, 7, 11, 15], target = 9 * - * 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] + * 鍥犱负 nums[0] + nums[1] = 2 + 7 = 9 鎵浠ヨ繑鍥 [0, 1] * * @author GinRyan * diff --git a/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s2_add_two_numbers/Solution.java b/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s2_add_two_numbers/Solution.java index 9419a730..d39f2911 100644 --- a/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s2_add_two_numbers/Solution.java +++ b/Codes/GinRyan/LeetCode/Java 8 Solves/LeeCodesSolves/srcs/org/ginryan/github/leetcodes/s2_add_two_numbers/Solution.java @@ -1,74 +1,74 @@ -package org.ginryan.github.leetcodes.s2_add_two_numbers; - -import java.util.ArrayList; - -/** - * - * 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 - * - * 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 - * - * 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 - * - * 示例: - * - * 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) - * - * 输出:7 -> 0 -> 8 - * - * 原因:342 + 465 = 807 - * - * @author GinRyan - */ -class Solution { - - public ListNode addTwoNumbers(ListNode l1, ListNode l2) { - ListNode retListNode = new ListNode(0); - ListNode latestRetListNode = retListNode; - ListNode currentNode1 = l1; - ListNode currentNode2 = l2; - // 满位进制准备值 - int carry = 0; - - while (currentNode1 != null || currentNode2 != null) { - // 一旦遇到单个空节点,则说明高位为0,则填充高位为0 - currentNode1 = currentNode1 == null ? new ListNode(0) : currentNode1; - currentNode2 = currentNode2 == null ? new ListNode(0) : currentNode2; - // 新建一个节点,并且填充两数字之和,并且加上上一节点进位值。如无进位这个值为0 - int cSum = currentNode1.val + currentNode2.val + carry; - // 取余做填充数值 - int cMod = cSum % 10; - // 取整除值做进位值写入到to_upper_pos变量中,用于下次将进位值加和 - carry = cSum / 10; - - ListNode createMode = new ListNode(cMod); - //挂接到结果链表节点下 - latestRetListNode.next = createMode; - //挂接到结果链表节点下之后还要向前推latestRetListNode到下一个节点 - latestRetListNode = latestRetListNode.next; - //次级节点递送 - currentNode1 = currentNode1.next; - currentNode2 = currentNode2.next; - - } - //最高位如果升位,需要增加一位节点,放1 - if (carry > 0) { - ListNode createMode = new ListNode(carry); - latestRetListNode.next = createMode; - } - return retListNode.next; - } - - public static void main(String[] args) { - - } -} - -class ListNode { - int val; - ListNode next; - - ListNode(int x) { - val = x; - } +package org.ginryan.github.leetcodes.s2_add_two_numbers; + +import java.util.ArrayList; + +/** + * + * 缁欏嚭涓や釜 闈炵┖ 鐨勯摼琛ㄧ敤鏉ヨ〃绀轰袱涓潪璐熺殑鏁存暟銆傚叾涓紝瀹冧滑鍚勮嚜鐨勪綅鏁版槸鎸夌収 閫嗗簭 鐨勬柟寮忓瓨鍌ㄧ殑锛屽苟涓斿畠浠殑姣忎釜鑺傜偣鍙兘瀛樺偍 涓浣 鏁板瓧銆 + * + * 濡傛灉锛屾垜浠皢杩欎袱涓暟鐩稿姞璧锋潵锛屽垯浼氳繑鍥炰竴涓柊鐨勯摼琛ㄦ潵琛ㄧず瀹冧滑鐨勫拰銆 + * + * 鎮ㄥ彲浠ュ亣璁鹃櫎浜嗘暟瀛 0 涔嬪锛岃繖涓や釜鏁伴兘涓嶄細浠 0 寮澶淬 + * + * 绀轰緥锛 + * + * 杈撳叆锛(2 -> 4 -> 3) + (5 -> 6 -> 4) + * + * 杈撳嚭锛7 -> 0 -> 8 + * + * 鍘熷洜锛342 + 465 = 807 + * + * @author GinRyan + */ +class Solution { + + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode retListNode = new ListNode(0); + ListNode latestRetListNode = retListNode; + ListNode currentNode1 = l1; + ListNode currentNode2 = l2; + // 婊′綅杩涘埗鍑嗗鍊 + int carry = 0; + + while (currentNode1 != null || currentNode2 != null) { + // 涓鏃﹂亣鍒板崟涓┖鑺傜偣锛屽垯璇存槑楂樹綅涓0锛屽垯濉厖楂樹綅涓0 + currentNode1 = currentNode1 == null ? new ListNode(0) : currentNode1; + currentNode2 = currentNode2 == null ? new ListNode(0) : currentNode2; + // 鏂板缓涓涓妭鐐癸紝骞朵笖濉厖涓ゆ暟瀛椾箣鍜岋紝骞朵笖鍔犱笂涓婁竴鑺傜偣杩涗綅鍊笺傚鏃犺繘浣嶈繖涓间负0 + int cSum = currentNode1.val + currentNode2.val + carry; + // 鍙栦綑鍋氬~鍏呮暟鍊 + int cMod = cSum % 10; + // 鍙栨暣闄ゅ煎仛杩涗綅鍊煎啓鍏ュ埌to_upper_pos鍙橀噺涓紝鐢ㄤ簬涓嬫灏嗚繘浣嶅煎姞鍜 + carry = cSum / 10; + + ListNode createMode = new ListNode(cMod); + //鎸傛帴鍒扮粨鏋滈摼琛ㄨ妭鐐逛笅 + latestRetListNode.next = createMode; + //鎸傛帴鍒扮粨鏋滈摼琛ㄨ妭鐐逛笅涔嬪悗杩樿鍚戝墠鎺╨atestRetListNode鍒颁笅涓涓妭鐐 + latestRetListNode = latestRetListNode.next; + //娆$骇鑺傜偣閫掗 + currentNode1 = currentNode1.next; + currentNode2 = currentNode2.next; + + } + //鏈楂樹綅濡傛灉鍗囦綅锛岄渶瑕佸鍔犱竴浣嶈妭鐐癸紝鏀1 + if (carry > 0) { + ListNode createMode = new ListNode(carry); + latestRetListNode.next = createMode; + } + return retListNode.next; + } + + public static void main(String[] args) { + + } +} + +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } } \ No newline at end of file