Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6b3ef91
Leetcode
zhuj05 May 16, 2025
0ba0e7d
Delete 108316121-10.html
zhuj05 May 16, 2025
2610cb9
Delete 108316121-11.html
zhuj05 May 16, 2025
b4330a4
Delete 108316121-12-改.html
zhuj05 May 16, 2025
497c30e
Delete 108316121-13.html
zhuj05 May 16, 2025
d3c7213
Delete 108316121-14.html
zhuj05 May 16, 2025
59c7c4f
Delete 108316121-15.html
zhuj05 May 16, 2025
a6f340b
Delete 108316121-6 CSS樣式.html
zhuj05 May 16, 2025
48a1edc
Delete 108316121-8.htm
zhuj05 May 16, 2025
e708484
Delete 20200311第二次作業 表格.htm
zhuj05 May 16, 2025
c3d7fec
Delete 20200318第三次作業 表單.html
zhuj05 May 16, 2025
5c9ccb6
Delete Midterm_108316121.html
zhuj05 May 16, 2025
ce2c4f2
Delete 20200414 CSS圓角邊框練習.html
zhuj05 May 16, 2025
70873e7
Delete 108316121-9.html
zhuj05 May 16, 2025
8e4ec46
Delete beau_PTT_Stock.py
zhuj05 May 16, 2025
0431dae
Add files via upload
zhuj05 May 16, 2025
2f70c62
Update README.md
zhuj05 May 16, 2025
caf448a
Update README.md
zhuj05 May 16, 2025
4c8cbc3
Update and rename 206 Reverse_Linked_LIst.py to 206 Reverse_Linked_Li…
zhuj05 May 16, 2025
107fe16
Add files via upload
zhuj05 May 17, 2025
e3cc278
Add files via upload
zhuj05 May 17, 2025
2b8a142
Add files via upload
zhuj05 May 17, 2025
b6444f5
Update 19 Remove_Nth_Node_From_End_of_List.cpp
zhuj05 May 17, 2025
24a9995
Add files via upload
zhuj05 May 18, 2025
5590c6a
Update 242 Valid Anagram.py
zhuj05 May 19, 2025
a8137fb
Update 242 Valid Anagram.py
zhuj05 May 19, 2025
768d6f7
Add files via upload
zhuj05 May 19, 2025
a6a8a72
Update 349 Intersection_of_Two_Arrays.py
zhuj05 May 19, 2025
a130e43
Update 349 Intersection_of_Two_Arrays.py
zhuj05 May 23, 2025
d1a22a2
Add files via upload
zhuj05 Jun 4, 2025
2a5b178
Update 001 Two Sum.java
zhuj05 Jun 4, 2025
46a7142
Add files via upload
zhuj05 Jun 5, 2025
0895bb0
Create test.txt
zhuj05 Jun 22, 2025
4749931
Add files via upload
zhuj05 Jun 22, 2025
60c0e08
Delete Leetcode_practice/leetcode_sql/test.txt
zhuj05 Jun 22, 2025
c8685f6
Update README.md
zhuj05 Jul 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Leetcode_practice/001 Two Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Map;

class Solution {
public int[] twoSum(int[] nums,int target){
Map<Integer,Integer> indexMap=new HashMap<>();

for(int i=0;i<nums.length;i++){
int s=target-nums[i];//紀錄要查詢的值,尋找s
if(indexMap.containsKey(s)){//如果在Map中出現過
return new int[]{i,indexMap.get(s)};//如果有找到,return目標值
}
else{
indexMap.put(nums[i],i);//如果沒有,把遍歷過的元素和下標(index)加入map中
}
}
return null;
}
}
26 changes: 26 additions & 0 deletions Leetcode_practice/142 Linked_List_Cycle_II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def detectCycle(self,head):
slow=head
fast=head

while fast and fast.next:
slow = slow.next #slow向後1步
fast=fast.next.next #fast向後2步

#如果有一個cycle,slow和fast pointer會相遇
if slow==fast:
#移動其中一個pointers回到list的開頭
slow=head
while slow!=fast:
slow = slow.next
fast=fast.next
return slow

#當沒有cycle,return None
return None
28 changes: 28 additions & 0 deletions Leetcode_practice/19 Remove_Nth_Node_From_End_of_List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//1.fast先移動n步(n+1)
//2.slow和fast同時move
//3.直到fast指向NULL
//4.slow指向要刪除的node(前一個)
//5.要先把slow指向要刪除的前一個node
//6.最後slow指向的node為NULL
//7.return頭節點
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy=new ListNode(0);//宣告虛擬頭節點
dummy->next=head;//設定虛擬頭節點位置
ListNode* slow=dummy;
ListNode* fast=dummy;
while(n-- && fast!=NULL){ //快指針向後移動
fast=fast->next;
}

fast=fast->next;//快指針再提前走一步,要讓慢指針指向刪除節點的前一個節點
while(fast!=NULL){ //fast和slow指針,同時向右移動
fast=fast->next; //fast指向NULL時,slow指向要刪除節點的前一個node
slow=slow->next;
}
slow->next=slow->next->next;//完成刪除倒數第n個節點

return dummy->next; //回傳頭節點
}
};
29 changes: 29 additions & 0 deletions Leetcode_practice/203 Remove_Linked_List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* ans=new ListNode(0,head);
ListNode* dummy=ans;

while(dummy!=nullptr)
{
while(dummy->next!=nullptr && dummy->next->val==val){
dummy->next=dummy->next->next;
}
dummy=dummy->next;
}
ListNode* result=ans->next;
delete ans;

return result;
}
};
17 changes: 17 additions & 0 deletions Leetcode_practice/206 Reverse_Linked_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev=None
while head:
next=head.next #head指向自己->指向下一個head
head.next=prev#head.next指向前一個(prev)
prev=head#prev:指向前一個->指向自己(head)
head=next
return prev

#「指向下一個 head.next」改成指向「前一個節點 prev」:
#head.next=prev
22 changes: 22 additions & 0 deletions Leetcode_practice/209 Minium_Size_Subarray_Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <vector>
#include <cstdint>
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int result = INT32_MAX;
int sum = 0; // 滑動窗口數字之和
int i = 0; // 滑動窗口起始位置
int subLength = 0; // 滑動窗口的長度
for (int j = 0; j < nums.size(); j++) {
sum += nums[j];
// 注意這裡使用while,每次更新 i(起始位置),並不斷比較序列是否符合條件
while (sum >= s) {
subLength = (j - i + 1); // 取子序列的長度
result = result < subLength ? result : subLength;
sum -= nums[i++]; // 這裡出現滑動窗口的精隨之處,不斷變更i(子序列的起始位置)
}
}
// 如果result沒有被賦值的話,就返回0,說明沒有符合條件的子序列
return result == INT32_MAX ? 0 : result;
}
};
23 changes: 23 additions & 0 deletions Leetcode_practice/24 Swap_Nodes_in_Pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution{
public:
ListNode* swapPairs(ListNode* head)
{
ListNode* dummy=new ListNode(0);//虛擬頭節點
dummy->next=head;
ListNode* cur=dummy;
while(cur->next!=nullptr && cur->next->next!=nullptr)
{
ListNode* tmp1 = cur->next; //節點1
ListNode* tmp2 = cur->next->next->next; //節點3

cur->next=cur->next->next; //step1,dummy->2
cur ->next-> next=tmp1; //step2,2->1
cur->next->next->next=tmp2;//step3,3->1

cur=cur->next->next; //cur向後移兩位,跳到while開頭,下一輪交換
}
ListNode* result=dummy->next;
delete dummy;
return result; //回傳頭節點
}
};
13 changes: 13 additions & 0 deletions Leetcode_practice/242 Valid Anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def isAnagram(self,s:str,t:str):
record=[0]*26
for i in s:
#不需記字母a的ASCII,只要求出一個相對數值就可以
record[ord(i)-ord("a")]+=1
for i in t:
record[ord(i)-ord("a")]-=1
for i in range(26):
if record[i]!=0:
#record若有些element!=0,說明string s和t,一定有多字符or少字符。
return False#不是有效的
return True#是有效的
32 changes: 32 additions & 0 deletions Leetcode_practice/349 Intersection_of_Two_Arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Two array的Intersection
#Use List and Set

class Solution(object):
def intersection(nums1,nums2):
#Use Hash Table save一個array中的所有element
table={}
for num in nums1:
table[num]=table.get(num,0)+1
#Use set to save result
result=set()
for num in nums2:
if num in table:
result.add(num) #新增有intersection的element到result list
del table[num]
return list(result) #return intersection的結果


#Use Dictionary
class Solution:
def intersection(self,nums1,nums2):
count1=[0]*1001
count2=[0]*1001 #題目要求length不超過1000
result=[] #存intersection result
for i in range(len(nums1)):
count1[nums1[i]]+=1
for j in range(len(nums2)):
count2[nums2[j]]+=1
for k in range(1001):
if count1[k]*count2[k]>0:
result.append(k) #若發現有intersection,新增element到result的dictionary
return result #return intersection的結果
29 changes: 29 additions & 0 deletions Leetcode_practice/409. Longest Palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*此問題的解決方法:
初始化兩個變量,oddCount 用於儲存出現次數為奇數的字元數,無序映射 ump 用於儲存字串中每個字元的計數。
迭代字串,對於每個字元 ch,增加無序映射中該字元的計數。
如果目前字元 ch 的計數為奇數,則增加 oddCount。 如果計數為偶數,則減少 oddCount。
如果 oddCount 大於 1,則傳回 s.length() - oddCount + 1,這是使用除一個出現次數為奇數的字元以外的所有字元形成的回文的最大長度。
如果 oddCount 不大於 1,則傳回 s.length(),也就是原始字串的長度,因為所有字元都可以用來形成回文。*/

#include <string>

class Solution {
public:
int longestPalindrome(string s) {
int oddCount=0;//用於儲存出現次數為奇數,目前ch的計數為奇數,則增加oddCount
unordered_map<char, int> ump;
for(char ch : s){
ump[ch]++;
if(ump[ch]%2==1){
oddCount++;
}
else{
oddCount--;
}
}
if(oddCount > 1){
return s.length()-oddCount+1;
}
return s.length();
}
};
25 changes: 25 additions & 0 deletions Leetcode_practice/454 4Sum II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution(object):
def fourSumCount(self, nums1, nums2, nums3, nums4):
"""
:type nums1: List[int]
:type nums2: List[int]
:type nums3: List[int]
:type nums4: List[int]
:rtype: int
"""
#用dictionary存nums1,nums2的元素,和
hashmap=dict()
for n1 in nums1:
for n2 in nums2:
if n1+n2 in hashmap:
hashmap[n1+n2]+=1#統計n1+n2的出現次數
else:
hashmap[n1+n2]=1
#if nums3和nums4,存在-(n1+n2),存到count
count=0
for n3 in nums3:
for n4 in nums4:
key = -n3-n4
if key in hashmap:#找到hashmap中key對應的數值
count+=hashmap[key]
return count #count統計四元組
19 changes: 19 additions & 0 deletions Leetcode_practice/53 Maximum Subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maxSubArray(int[] nums) {
int maxSum=Integer.MIN_VALUE;
int currentSum=0;

for(int i=0;i<nums.length;i++)
{
currentSum+=nums[i];

if(currentSum>maxSum){
maxSum=currentSum;
}
if(currentSum<0){
currentSum=0;
}
}
return maxSum;
}
}
47 changes: 47 additions & 0 deletions Leetcode_practice/59 Spiral_Matrix_II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> res(n, vector<int>(n, 0)); // 使用vector定義一個二維數組
int startx = 0, starty = 0; // 定義每循環一個圈的起始位置
int loop = n / 2; // 每個圈循環幾次,例如n為奇數3,那麼loop = 1 只是循環一圈,矩陣中間的值需要單獨處理
int mid = n / 2; // 矩陣中間的位置,例如:n為3, 中間的位置就是(1,1),n為5,中間位置為(2, 2)
int count = 1; // 用來給矩陣中每一個空格賦值
int offset = 1; // 需要控制每一條邊遍歷的長度,每次循環右邊收縮一個長度
int i,j;
while (loop --) {
i = startx;
j = starty;

// 下面開始的四個for就是模擬轉了一圈
// 模擬填充上行從左到右(左閉右開)
for (j; j < n - offset; j++) {
res[i][j] = count++;
}
// 模擬填充右列從上到下(左閉右開)
for (i; i < n - offset; i++) {
res[i][j] = count++;
}
// 模擬填充下行從右到左(左閉右開)
for (; j > starty; j--) {
res[i][j] = count++;
}
// 模擬填充左列從下到上(左閉右開)
for (; i > startx; i--) {
res[i][j] = count++;
}

// 第二圈開始的時候,起始位置要各自加1, 例如:第一圈起始位置是(0, 0),第二圈起始位置是(1, 1)
startx++;
starty++;

// offset 控制每一圈裡每一條邊遍歷的長度
offset += 1;
}

// 如果n為奇數的話,需要單獨給矩陣最中間的位置賦值
if (n % 2) {
res[mid][mid] = count;
}
return res;
}
};
21 changes: 21 additions & 0 deletions Leetcode_practice/704 Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] == target) {//相等
return mid;
} else if (nums[mid] < target) {//在右側
left = mid + 1;
} else {//在左側
right = mid - 1;
}
}

return -1;//沒找到
}
};
Loading
Loading