Skip to content

Commit 4daf90e

Browse files
committed
new soln
1 parent 52097be commit 4daf90e

19 files changed

+417
-54
lines changed

1047.RemoveDuplicates.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 1047. Remove All Adjacent Duplicates In String
2+
// You are given a string s consisting of lowercase English letters.
3+
// A duplicate removal consists of choosing two adjacent and equal letters and removing them.
4+
// We repeatedly make duplicate removals on s until we no longer can.
5+
// Return the final string after all such duplicate removals have been made.
6+
// It can be proven that the answer is unique.
7+
8+
// Example 1:
9+
// Input: s = "abbaca"
10+
// Output: "ca"
11+
// Explanation:
12+
// For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
13+
// Example 2:
14+
// Input: s = "azxxzy"
15+
// Output: "ay"
16+
17+
18+
// Constraints:
19+
// 1 <= s.length <= 105
20+
// s consists of lowercase English letters.
21+
22+
public class Solution {
23+
//Intuition: Use stack to keep track of the characters.
24+
//If the current character is same as the top of the stack, pop the top of the stack.
25+
//Else, push the current character to the stack.
26+
public string RemoveDuplicates(string s) {
27+
Stack<char> st = new();
28+
for(int i = 0; i < s.Length; i++){
29+
if(st.Count() > 0 && st.Peek() == s[i]){
30+
st.Pop();
31+
}
32+
else{
33+
st.Push(s[i]);
34+
}
35+
}
36+
return new string(st.ToArray().Reverse().ToArray());
37+
}
38+
}

11.MaxArea.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 11. Container With Most Water
22

3-
// You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
3+
// You are given an integer array height of length n.
4+
// There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
45

56
// Find two lines that together with the x-axis form a container, such that the container contains the most water.
67

@@ -18,6 +19,13 @@
1819
// Output: 1
1920

2021
public class Solution {
22+
//intution: We can solve this problem by using two pointers.
23+
//We can use two pointers to iterate through the array and find the maximum area.
24+
//We can use the two pointers to find the maximum area by finding the minimum of the two heights
25+
//and the distance between the two pointers.
26+
//We can return the maximum area.
27+
//Time complexity: O(n)
28+
//Space complexity: O(1)
2129
public int MaxArea(int[] height) {
2230
int left = 0,
2331
right = height.Length -1;

14.LongestCommonPrefix.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public string LongestCommonPrefix(string[] strs)
1111
return "";
1212
if (size == 1)
1313
return strs[0];
14-
14+
//Sort the array of strings
15+
//The first and last strings will be the smallest and largest strings
16+
//We can compare the first and last strings to find the longest common prefix
1517
Array.Sort(strs);
1618
int end = Math.Min(strs[0].Length, strs[size - 1].Length);
1719
int i = 0;

15.ThreeSum.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// 15. 3Sum
22

3-
// Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
3+
// Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]]
4+
// such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
45

56
// Notice that the solution set must not contain duplicate triplets.
67

78
// Example 1:
8-
99
// Input: nums = [-1,0,1,2,-1,-4]
1010
// Output: [[-1,-1,2],[-1,0,1]]
1111
// Explanation:
@@ -15,17 +15,21 @@
1515
// The distinct triplets are [-1,0,1] and [-1,-1,2].
1616
// Notice that the order of the output and the order of the triplets does not matter.
1717
// Example 2:
18-
1918
// Input: nums = [0,1,1]
2019
// Output: []
2120
// Explanation: The only possible triplet does not sum up to 0.
2221
// Example 3:
23-
2422
// Input: nums = [0,0,0]
2523
// Output: [[0,0,0]]
2624
// Explanation: The only possible triplet sums up to 0.
2725

2826
public class Solution {
27+
//intuition: We can solve this problem by using two pointers.
28+
//We can sort the array and then iterate through the array.
29+
//For each element, we can use two pointers to find the other two elements that sum up to 0.
30+
//We can skip duplicate elements to avoid duplicate triplets.
31+
//Time complexity: O(n^2)
32+
//Space complexity: O(1)
2933
public IList<IList<int>> ThreeSum(int[] nums) {
3034
Array.Sort(nums);
3135

1669.MergeInBetween.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// 1669. Merge In Between Linked Lists
2+
// You are given two linked lists: list1 and list2 of sizes n and m respectively.
3+
// Remove list1's nodes from the ath node to the bth node, and put list2 in their place.
4+
// The blue edges and nodes in the following figure indicate the result:
5+
// Build the result list and return its head.
6+
// Example 1:
7+
// Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
8+
// Output: [10,1,13,1000000,1000001,1000002,5]
9+
// Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
10+
// Example 2:
11+
// Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
12+
// Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
13+
// Explanation: The blue edges and nodes in the above figure indicate the result.
14+
15+
// Constraints:
16+
// 3 <= list1.length <= 104
17+
// 1 <= a <= b < list1.length - 1
18+
// 1 <= list2.length <= 104
19+
20+
/**
21+
* Definition for singly-linked list.
22+
* public class ListNode {
23+
* public int val;
24+
* public ListNode next;
25+
* public ListNode(int val=0, ListNode next=null) {
26+
* this.val = val;
27+
* this.next = next;
28+
* }
29+
* }
30+
*/
31+
public class Solution {
32+
public ListNode MergeInBetween(ListNode list1, int a, int b, ListNode list2) {
33+
int i = 0;
34+
ListNode Start, Curr, End;
35+
Start = Curr = list1;
36+
//iterate to the ath node
37+
while(i < a-1){
38+
Curr = Curr.next;
39+
i++;
40+
}
41+
//save the ath node as Start of the list to be removed
42+
Start = Curr;
43+
//iterate to the bth node to save it as End of the list to be removed
44+
while(i <= b){
45+
Curr = Curr.next;
46+
i++;
47+
}
48+
//save the bth node as End of the list to be removed
49+
End = Curr;
50+
//connect the list1 to list2
51+
Start.next = list2;
52+
//iterate to the end of list2
53+
while(Start.next != null){
54+
Start = Start.next;
55+
}
56+
//connect the end of list2 to the list1
57+
Start.next = End;
58+
return list1;
59+
}
60+
}

17.LetterCombinations.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// 17. Letter Combinations of a Phone Number
2-
// Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
3-
// A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
2+
// Given a string containing digits from 2-9 inclusive, return all possible letter combinations
3+
// that the number could represent. Return the answer in any order.
4+
// A mapping of digits to letters (just like on the telephone buttons) is given below.
5+
// Note that 1 does not map to any letters.
46
// Example 1:
57
// Input: digits = "23"
68
// Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
@@ -12,6 +14,8 @@
1214
// Output: ["a","b","c"]
1315

1416
public class Solution {
17+
//Time complexity: O(3^n * 4^m) where n is the number of digits that have 3 letters and m is the number of digits that have 4 letters.
18+
//Space complexity: O(3^n * 4^m) where n is the number of digits that have 3 letters and m is the number of digits that have 4 letters.
1519
string[] dialPad = new string[]
1620
{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
1721
public IList<string> LetterCombinations(string digits) {
@@ -22,11 +26,14 @@ public IList<string> LetterCombinations(string digits) {
2226
return result;
2327
}
2428
public void AllCombination(IList<string> result,string digits,string s, int idx) {
29+
//if last index is checked, add the string to the result list.
2530
if(idx == digits.Length){
2631
result.Add(new string(s));
2732
return;
2833
}
34+
//Get the number from the string by converting string to int.
2935
int num = digits[idx] - '0';
36+
//Add current character to string s and then call the function recursively for the next digit.
3037
foreach(char c in dialPad[num]){
3138
AllCombination(result,digits,s + c, idx+1);
3239
}

2.AddTwoNumbers.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@
1515
*/
1616
public class Solution
1717
{
18+
//Intuition: We can solve this problem using two pointers.
19+
//We can iterate through both the lists and add the values of the nodes.
20+
//We can keep track of the carry and add it to the next node.
21+
//If the sum is greater than 9, we can take the remainder and set the carry to 1.
22+
//If the sum is less than 9, we can set the carry to 0.
23+
//If we reach the end of the list and there is still a carry, we can add a new node with the value 1.
24+
//Finally, we can return the head of the list.
25+
//Time complexity: O(n)
26+
//Space complexity: O(1)
1827
public ListNode AddTwoNumbers(ListNode list1, ListNode list2)
1928
{
29+
2030
ListNode head, curr, prev;
2131
if (list1 == null && list2 == null)
2232
return null;

20.IsValid.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,27 @@
33
//An input string is valid if:
44
//Open brackets must be closed by the same type of brackets.
55
//Open brackets must be closed in the correct order.
6+
// Every close bracket has a corresponding open bracket of the same type.
7+
// Example 1:
8+
// Input: s = "()"
9+
// Output: true
10+
// Example 2:
11+
// Input: s = "()[]{}"
12+
// Output: true
13+
// Example 3:
14+
// Input: s = "(]"
15+
// Output: false
16+
17+
// Constraints:
18+
// 1 <= s.length <= 104
19+
// s consists of parentheses only '()[]{}'.
620

721
public class Solution
822
{
23+
//intution: Use stack to store the opening brackets and pop the stack when a closing bracket is found.
24+
//If the stack is empty and a closing bracket is found, return false.
25+
//Time complexity: O(n) where n is the length of the string.
26+
//Space complexity: O(n) where n is the length of the string.
927
public bool IsValid(string s)
1028
{
1129
Stack<char> stack = new Stack<char>();
@@ -17,10 +35,11 @@ public bool IsValid(string s)
1735
if ((s[i] == '{') || (s[i] == '[') || (s[i] == '('))
1836
{
1937
stack.Push(s[i]);
38+
continue;
2039
}
21-
else if (stack.Count == 0)
40+
if (stack.Count == 0)
2241
return false;
23-
else if ((s[i] == '}') && (stack.Peek() == '{'))
42+
if ((s[i] == '}') && (stack.Peek() == '{'))
2443
{
2544
stack.Pop();
2645
}

23.MergeKLists.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// 23. Merge k Sorted Lists
22

33
// You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
4-
54
// Merge all the linked-lists into one sorted linked-list and return it.
65

7-
8-
96
// Example 1:
10-
117
// Input: lists = [[1,4,5],[1,3,4],[2,6]]
128
// Output: [1,1,2,3,4,4,5,6]
139
// Explanation: The linked-lists are:
@@ -19,16 +15,16 @@
1915
// merging them into one sorted list:
2016
// 1->1->2->3->4->4->5->6
2117
// Example 2:
22-
2318
// Input: lists = []
2419
// Output: []
2520
// Example 3:
26-
2721
// Input: lists = [[]]
2822
// Output: []
29-
3023
//Approch Divide & Conqure -> Merge Sort
3124
public class Solution {
25+
//intution: Use Merge Sort to merge the lists.
26+
//Time complexity: O(NLogK) where N is the total number of elements in all the lists and K is the number of lists.
27+
//Space complexity: O(1)
3228
public ListNode MergeKLists(ListNode[] lists) {
3329
if(lists == null || lists.Length == 0)
3430
return null;

24.SwapPairs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
public class Solution
1616
{
17+
//intuio
1718
public ListNode SwapPairs(ListNode head)
1819
{
1920
ListNode prev, curr;
@@ -25,6 +26,7 @@ public ListNode SwapPairs(ListNode head)
2526
head = head.next;
2627
while (curr != null && curr.next != null)
2728
{
29+
//swap
2830
prev.next = curr.next;
2931
prev = curr;
3032
curr = curr.next;

26.RemoveDuplicates.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,44 @@
1-
//26. Remove Duplicates from Sorted Array
2-
//Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
3-
// Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
4-
// Return k after placing the final result in the first k slots of nums.
5-
// Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
1+
// 26. Remove Duplicates from Sorted Array
2+
3+
// Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.
4+
// The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
5+
6+
// Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
7+
8+
// Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially.
9+
// The remaining elements of nums are not important as well as the size of nums.
10+
// Return k.
11+
// Custom Judge:
12+
13+
// The judge will test your solution with the following code:
14+
15+
// int[] nums = [...]; // Input array
16+
// int[] expectedNums = [...]; // The expected answer with correct length
17+
18+
// int k = removeDuplicates(nums); // Calls your implementation
19+
20+
// assert k == expectedNums.length;
21+
// for (int i = 0; i < k; i++) {
22+
// assert nums[i] == expectedNums[i];
23+
// }
24+
// If all assertions pass, then your solution will be accepted.
25+
26+
// Example 1:
27+
// Input: nums = [1,1,2]
28+
// Output: 2, nums = [1,2,_]
29+
// Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
30+
// It does not matter what you leave beyond the returned k (hence they are underscores).
31+
// Example 2:
32+
// Input: nums = [0,0,1,1,1,2,2,3,3,4]
33+
// Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
34+
// Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
35+
// It does not matter what you leave beyond the returned k (hence they are underscores).
36+
37+
// Constraints:
38+
// 1 <= nums.length <= 3 * 104
39+
// -100 <= nums[i] <= 100
40+
// nums is sorted in non-decreasing order.
41+
642
public class Solution {
743
public int RemoveDuplicates(int[] nums) {
844
int j=0;

0 commit comments

Comments
 (0)