Skip to content

Commit 686248a

Browse files
Zanger67/leetcodeZanger67/leetcode
authored andcommitted
Updated markdown files
1 parent 17b3b2f commit 686248a

8 files changed

+557
-0
lines changed

markdowns/_1. Two Sum.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1. [Two Sum](<https://leetcode.com/problems/two-sum>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Array](<by_topic/Array.md>), [Hash Table](<by_topic/Hash Table.md>)**
16+
>
17+
> **Acceptance Rate** : **53.036 %**
18+
19+
------
20+
21+
> test test test again iasdfdsaf
22+
23+
------
24+
25+
## Solutions
26+
27+
- [e1 - brute force.java](<../my-submissions/e1 - brute force.java>)
28+
- [e1.java](<../my-submissions/e1.java>)
29+
- [e1.py](<../my-submissions/e1.py>)
30+
### Java
31+
#### [e1 - brute force.java](<../my-submissions/e1 - brute force.java>)
32+
```Java
33+
// Slow and O(n^2)
34+
// See alternative OTHER for hashmap solution
35+
36+
class Solution {
37+
public int[] twoSum(int[] nums, int target) {
38+
for (int i = 0; i < nums.length - 1; i++) {
39+
for (int j = i+1; j < nums.length; j++) {
40+
if (nums[i] + nums[j] == target) {
41+
return new int[]{i, j};
42+
}
43+
}
44+
}
45+
46+
return new int[]{};
47+
}
48+
}
49+
```
50+
51+
#### [e1.java](<../my-submissions/e1.java>)
52+
```Java
53+
class Solution {
54+
public int[] twoSum(int[] nums, int target) {
55+
if (nums.length == 2) {
56+
return new int[]{0, 1};
57+
}
58+
59+
HashMap<Integer, Integer> ref = new HashMap<>();
60+
61+
for (int i = 0; i < nums.length; i++) {
62+
if (ref.containsKey(nums[i])) {
63+
return new int[]{ref.get(nums[i]), i};
64+
}
65+
ref.put((target - nums[i]), i);
66+
}
67+
68+
// No solution (shouldn't occur given parameters provided)
69+
return new int[]{0, 0};
70+
}
71+
}
72+
```
73+
74+
### Python
75+
#### [e1.py](<../my-submissions/e1.py>)
76+
```Python
77+
class Solution:
78+
def twoSum(self, nums: List[int], target: int) -> List[int]:
79+
if len(nums) == 2 :
80+
return [0, 1]
81+
82+
differences = {}
83+
for i in range(len(nums)) :
84+
if nums[i] in differences.keys() :
85+
return [i, differences.get(nums[i])]
86+
differences[target - nums[i]] = i
87+
return [0, 0]
88+
89+
```
90+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 14. [Longest Common Prefix](<https://leetcode.com/problems/longest-common-prefix>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[String](<by_topic/String.md>), [Trie](<by_topic/Trie.md>)**
16+
>
17+
> **Acceptance Rate** : **43.195 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [e14.java](<../my-submissions/e14.java>)
24+
### Java
25+
#### [e14.java](<../my-submissions/e14.java>)
26+
```Java
27+
class Solution {
28+
public String longestCommonPrefix(String[] strs) {
29+
if (strs.length == 1) {
30+
return strs[0];
31+
}
32+
33+
int shortestLen = strs[0].length();
34+
for (int i = 0; i < strs.length; i++) {
35+
shortestLen = Math.min(strs[i].length(), shortestLen);
36+
}
37+
38+
for (int i = 0; i < shortestLen; i++) {
39+
for (int j = 1; j < strs.length; j++) {
40+
if (strs[j].charAt(i) != strs[j - 1].charAt(i)) {
41+
return strs[0].substring(0,i);
42+
}
43+
}
44+
}
45+
46+
return strs[0].substring(0, shortestLen);
47+
}
48+
}
49+
```
50+

markdowns/_2. Add Two Numbers.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 2. [Add Two Numbers](<https://leetcode.com/problems/add-two-numbers>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Linked List](<by_topic/Linked List.md>), [Math](<by_topic/Math.md>), [Recursion](<by_topic/Recursion.md>)**
16+
>
17+
> **Acceptance Rate** : **43.366 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [m2.java](<../my-submissions/m2.java>)
24+
### Java
25+
#### [m2.java](<../my-submissions/m2.java>)
26+
```Java
27+
/**
28+
* Definition for singly-linked list.
29+
* public class ListNode {
30+
* int val;
31+
* ListNode next;
32+
* ListNode() {}
33+
* ListNode(int val) { this.val = val; }
34+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
35+
* }
36+
*/
37+
class Solution {
38+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
39+
if (l1 == null || l2 == null) {
40+
return null;
41+
}
42+
43+
ListNode output = new ListNode();
44+
ListNode current = output;
45+
int carry = 0;
46+
while (l1 != null && l2 != null) {
47+
current.val = l1.val + l2.val + carry;
48+
carry = 0;
49+
if (current.val >= 10) {
50+
current.val -= 10;
51+
carry = 1;
52+
}
53+
54+
55+
if (l1.next == null || l2.next == null) {
56+
break;
57+
}
58+
59+
current.next = new ListNode();
60+
current = current.next;
61+
l1 = l1.next;
62+
l2 = l2.next;
63+
}
64+
65+
66+
while (l1.next != null) {
67+
current.next = new ListNode();
68+
current = current.next;
69+
l1 = l1.next;
70+
71+
72+
current.val = l1.val + carry;
73+
carry = 0;
74+
if (current.val >= 10) {
75+
current.val -= 10;
76+
carry = 1;
77+
}
78+
79+
}
80+
81+
while (l2.next != null) {
82+
current.next = new ListNode();
83+
current = current.next;
84+
l2 = l2.next;
85+
86+
current.val = l2.val + carry;
87+
carry = 0;
88+
if (current.val >= 10) {
89+
current.val -= 10;
90+
carry = 1;
91+
}
92+
93+
}
94+
95+
if (carry == 1) {
96+
current.next = new ListNode(1);
97+
}
98+
99+
100+
return output;
101+
}
102+
}
103+
```
104+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 3. [Longest Substring Without Repeating Characters](<https://leetcode.com/problems/longest-substring-without-repeating-characters>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Hash Table](<by_topic/Hash Table.md>), [String](<by_topic/String.md>), [Sliding Window](<by_topic/Sliding Window.md>)**
16+
>
17+
> **Acceptance Rate** : **35.039 %**
18+
19+
------
20+
21+
## Solutions
22+
23+
- [m3.java](<../my-submissions/m3.java>)
24+
### Java
25+
#### [m3.java](<../my-submissions/m3.java>)
26+
```Java
27+
class Solution {
28+
public int lengthOfLongestSubstring(String s) {
29+
int maxSoFar = 0;
30+
for (int i = 0; i < s.length() && s.length() - i > maxSoFar; i++) {
31+
int counter = 0;
32+
HashSet<Character> temp = new HashSet<>();
33+
for (int j = i; j < s.length(); j++) {
34+
if (temp.contains(s.charAt(j))) {
35+
break;
36+
} else {
37+
temp.add(s.charAt(j));
38+
}
39+
40+
counter++;
41+
}
42+
43+
if (maxSoFar < counter) {
44+
maxSoFar = counter;
45+
}
46+
}
47+
48+
return maxSoFar;
49+
}
50+
}
51+
```
52+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 5. [Longest Palindromic Substring](<https://leetcode.com/problems/longest-palindromic-substring>)
2+
3+
*All prompts are owned by LeetCode. To view the prompt, click the title link above.*
4+
5+
*[Back to top](<../README.md>)*
6+
7+
------
8+
9+
> *First completed : July 10, 2024*
10+
>
11+
> *Last updated : July 10, 2024*
12+
13+
------
14+
15+
> **Related Topics** : **[Two Pointers](<by_topic/Two Pointers.md>), [String](<by_topic/String.md>), [Dynamic Programming](<by_topic/Dynamic Programming.md>)**
16+
>
17+
> **Acceptance Rate** : **34.08 %**
18+
19+
------
20+
21+
> ***Version 1*** is severly inefficient since it checks each pair of indicies, reasulting in at
22+
> least $O(n^2)$ runtime. On top of that, the inside of the nested loop has another for loop
23+
> to check whether the substring is a palindrome. This would make the runtime increase further
24+
> to a worst case leaning towards $O(n^3)$. While it does avoid redundant cases (i.e.) substrings
25+
> of a length that wouldn't improve the current record, it's still a major negative.
26+
>
27+
> </br>
28+
>
29+
> ***Version 2*** on the other hand avoids this by starting from the centre of each potential palindrome.
30+
> It iterates through the string twice, once where it assumes the centre is a single character (and thus at
31+
> least a palindrome of length $n$) and again where the indices $i$ and $i+1$ are used as the centre. In effect,
32+
> it's checking odd and even length palindromes. This ends up being comfortably $O(n^2)$; a much greater improvement
33+
> as compared to *Version 1*.
34+
35+
------
36+
37+
## Solutions
38+
39+
- [m5 v1 inefficient.py](<../my-submissions/m5 v1 inefficient.py>)
40+
- [m5 v2 much better.py](<../my-submissions/m5 v2 much better.py>)
41+
### Python
42+
#### [m5 v1 inefficient.py](<../my-submissions/m5 v1 inefficient.py>)
43+
```Python
44+
class Solution:
45+
def longestPalindrome(self, s: str) -> str:
46+
longest = 0
47+
longsetS = ''
48+
for i in range(len(s)) :
49+
for j in range(len(s) - 1, i - 1, -1) :
50+
if j - i + 1 <= longest :
51+
continue
52+
isPal = True
53+
for k in range((j - i + 1) // 2) :
54+
if s[i + k] != s[j - k] :
55+
isPal = False
56+
break
57+
if isPal :
58+
longest = j - i + 1
59+
longsetS = s[i: j+1]
60+
61+
return longsetS
62+
```
63+
64+
#### [m5 v2 much better.py](<../my-submissions/m5 v2 much better.py>)
65+
```Python
66+
class Solution:
67+
def longestPalindrome(self, s: str) -> str:
68+
longest = 0
69+
longsetS = ''
70+
for i in range(len(s)) :
71+
currOffset = 1
72+
while 0 <= i - currOffset and \
73+
i + currOffset < len(s) and \
74+
s[i - currOffset] == s[i + currOffset] :
75+
currOffset += 1
76+
currOffset -= 1
77+
78+
if longest < currOffset * 2 + 1 :
79+
longest = currOffset * 2 + 1
80+
longsetS = s[i - currOffset : i + currOffset + 1]
81+
82+
for i in range(0, len(s) - 1) :
83+
currOffset = 0
84+
while 0 <= i - currOffset and \
85+
i + currOffset + 1 < len(s) and \
86+
s[i - currOffset] == s[i + currOffset + 1] :
87+
currOffset += 1
88+
89+
if longest < currOffset * 2 :
90+
longest = currOffset * 2
91+
longsetS = s[i - currOffset + 1 : i + currOffset + 1]
92+
93+
return longsetS
94+
```
95+

0 commit comments

Comments
 (0)