Skip to content

Commit 246eceb

Browse files
authored
Added Go
1 parent a6a465e commit 246eceb

File tree

133 files changed

+4910
-307
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+4910
-307
lines changed

README.md

Lines changed: 307 additions & 307 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package s0001_two_sum
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
4+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
5+
// #2024_01_28_Time_3_ms_(93.85%)_Space_4.2_MB_(58.64%)
6+
7+
func twoSum(nums []int, target int) []int {
8+
indexMap := make(map[int]int)
9+
for i, num := range nums {
10+
requiredNum := target - num
11+
if j, ok := indexMap[requiredNum]; ok {
12+
return []int{j, i}
13+
}
14+
indexMap[num] = i
15+
}
16+
return []int{-1, -1}
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package s0002_add_two_numbers
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
4+
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_03_05_Time_4_ms_(84.60%)_Space_4.4_MB_(47.97%)
6+
7+
/*
8+
* Definition for singly-linked list.
9+
* type ListNode struct {
10+
* Val int
11+
* Next *ListNode
12+
* }
13+
*/
14+
15+
type ListNode struct {
16+
Val int
17+
Next *ListNode
18+
}
19+
20+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
21+
dummyHead := &ListNode{}
22+
p, q, curr := l1, l2, dummyHead
23+
carry := 0
24+
25+
for p != nil || q != nil {
26+
x, y := 0, 0
27+
if p != nil {
28+
x = p.Val
29+
p = p.Next
30+
}
31+
if q != nil {
32+
y = q.Val
33+
q = q.Next
34+
}
35+
36+
sum := carry + x + y
37+
carry = sum / 10
38+
curr.Next = &ListNode{Val: sum % 10}
39+
curr = curr.Next
40+
}
41+
42+
if carry > 0 {
43+
curr.Next = &ListNode{Val: carry}
44+
}
45+
46+
return dummyHead.Next
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest** **substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
30+
31+
**Constraints:**
32+
33+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
34+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package s0003_longest_substring_without_repeating_characters
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
4+
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
5+
// #Big_O_Time_O(n)_Space_O(1) #2024_03_05_Time_0_ms_(100.00%)_Space_2.5_MB_(98.66%)
6+
7+
func lengthOfLongestSubstring(s string) int {
8+
lastIndices := make([]int, 256)
9+
for i := 0; i < 256; i++ {
10+
lastIndices[i] = -1
11+
}
12+
maxLen := 0
13+
curLen := 0
14+
start := 0
15+
16+
for i := 0; i < len(s); i++ {
17+
cur := s[i]
18+
if lastIndices[cur] < start {
19+
lastIndices[cur] = i
20+
curLen++
21+
} else {
22+
lastIndex := lastIndices[cur]
23+
start = lastIndex + 1
24+
curLen = i - start + 1
25+
lastIndices[cur] = i
26+
}
27+
if curLen > maxLen {
28+
maxLen = curLen
29+
}
30+
}
31+
return maxLen
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Constraints:**
26+
27+
* `nums1.length == m`
28+
* `nums2.length == n`
29+
* `0 <= m <= 1000`
30+
* `0 <= n <= 1000`
31+
* `1 <= m + n <= 2000`
32+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package s0004_median_of_two_sorted_arrays
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2024_03_05_Time_9_ms_(72.04%)_Space_4.8_MB_(67.69%)
5+
6+
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
7+
if len(nums1) > len(nums2) {
8+
nums1, nums2 = nums2, nums1
9+
}
10+
11+
x, y := len(nums1), len(nums2)
12+
low, high := 0, x
13+
14+
for low <= high {
15+
partitionX := (low + high) / 2
16+
partitionY := (x+y+1)/2 - partitionX
17+
18+
maxLeftX := getIntValue(nums1, partitionX-1)
19+
minRightX := getIntValue(nums1, partitionX)
20+
21+
maxLeftY := getIntValue(nums2, partitionY-1)
22+
minRightY := getIntValue(nums2, partitionY)
23+
24+
if maxLeftX <= minRightY && maxLeftY <= minRightX {
25+
if (x+y)%2 == 0 {
26+
return float64(max(maxLeftX, maxLeftY)+min(minRightX, minRightY)) / 2
27+
} else {
28+
return float64(max(maxLeftX, maxLeftY))
29+
}
30+
} else if maxLeftX > minRightY {
31+
high = partitionX - 1
32+
} else {
33+
low = partitionX + 1
34+
}
35+
}
36+
37+
return 0.0
38+
}
39+
40+
func getIntValue(nums []int, index int) int {
41+
if index < 0 {
42+
return -1000000
43+
}
44+
if index >= len(nums) {
45+
return 1000000
46+
}
47+
return nums[index]
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
5\. Longest Palindromic Substring
2+
3+
Medium
4+
5+
Given a string `s`, return _the longest_ _palindromic_ _substring_ in `s`.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "babad"
10+
11+
**Output:** "bab"
12+
13+
**Explanation:** "aba" is also a valid answer.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "cbbd"
18+
19+
**Output:** "bb"
20+
21+
**Constraints:**
22+
23+
* `1 <= s.length <= 1000`
24+
* `s` consist of only digits and English letters.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package s0005_longest_palindromic_substring
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
6+
// #2024_03_05_Time_0_ms_(100.00%)_Space_3.8_MB_(32.56%)
7+
8+
func longestPalindrome(s string) string {
9+
newStr := make([]byte, len(s)*2+1)
10+
newStr[0] = '#'
11+
for i := 0; i < len(s); i++ {
12+
newStr[2*i+1] = s[i]
13+
newStr[2*i+2] = '#'
14+
}
15+
dp := make([]int, len(newStr))
16+
friendCenter, friendRadius := 0, 0
17+
lpsCenter, lpsRadius := 0, 0
18+
19+
for i := 0; i < len(newStr); i++ {
20+
if friendCenter+friendRadius > i {
21+
dp[i] = min(dp[2*friendCenter-i], (friendCenter+friendRadius)-i)
22+
} else {
23+
dp[i] = 1
24+
}
25+
for i+dp[i] < len(newStr) && i-dp[i] >= 0 && newStr[i+dp[i]] == newStr[i-dp[i]] {
26+
dp[i]++
27+
}
28+
if friendCenter+friendRadius < i+dp[i] {
29+
friendCenter, friendRadius = i, dp[i]
30+
}
31+
if lpsRadius < dp[i] {
32+
lpsCenter, lpsRadius = i, dp[i]
33+
}
34+
}
35+
return s[(lpsCenter-lpsRadius+1)/2 : (lpsCenter+lpsRadius-1)/2]
36+
}
37+
38+
func min(a, b int) int {
39+
if a < b {
40+
return a
41+
}
42+
return b
43+
}

0 commit comments

Comments
 (0)