亚洲精品92内射,午夜福利院在线观看免费 ,亚洲av中文无码乱人伦在线视色,亚洲国产欧美国产综合在线,亚洲国产精品综合久久2007

?
Java知識分享網(wǎng) - 輕松學習從此開始!????

Java知識分享網(wǎng)

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus權(quán)限系統(tǒng)實戰(zhàn)課程 震撼發(fā)布        

最新Java全棧就業(yè)實戰(zhàn)課程(免費)

AI人工智能學習大禮包

IDEA永久激活

66套java實戰(zhàn)課程無套路領(lǐng)取

鋒哥開始收Java學員啦!

Python學習路線圖

鋒哥開始收Java學員啦!

基于Java實現(xiàn)20道LeetCode題 PDF 下載


分享到:
時間:2024-08-22 11:15來源:http://sh6999.cn 作者:轉(zhuǎn)載  侵權(quán)舉報
基于Java實現(xiàn)20道LeetCode題
失效鏈接處理
基于Java實現(xiàn)20道LeetCode題 PDF 下載

 
 
相關(guān)截圖:
 


主要內(nèi)容:


1. 兩數(shù)之和 (Two Sum)
解題思路
使用哈希表存儲每個數(shù)字的索引,如果某個數(shù)字的補數(shù)存在于哈希表中,則返回對應(yīng)的索引。
實現(xiàn)代碼
 
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}

 

2. 添加兩數(shù) (Add Two Numbers)
解題思路
使用鏈表表示兩個數(shù),從最低位開始逐位相加,注意處理進位。
實現(xiàn)代碼
 
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, current = dummyHead;
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;
current.next = new ListNode(sum % 10);
current = current.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
current.next = new ListNode(carry);
}
return dummyHead.next;
}
}

 



 


------分隔線----------------------------
?
鋒哥公眾號


鋒哥微信


關(guān)注公眾號
【Java資料站】
回復(fù) 666
獲取 
66套java
從菜雞到大神
項目實戰(zhàn)課程

鋒哥推薦