-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c59cae0
commit 5a12b24
Showing
23 changed files
with
712 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...y-Number-in-a-Linked-List-to-Integer/Convert-Binary-Number-in-a-Linked-List-to-Integer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
namespace _1290_Convert_Binary_Number_in_a_Linked_List_to_Integer | ||
{ | ||
public class Solution | ||
{ | ||
public int GetDecimalValue(ListNode head) | ||
{ | ||
//Solution 1 | ||
var res = 0; | ||
while (head != null) | ||
{ | ||
res = res * 2 + head.val; | ||
head = head.next; | ||
} | ||
return res; | ||
|
||
//Solution 2: Stack, from end to beginning | ||
/* | ||
var stack = new Stack<int>(); | ||
while(head != null){ | ||
stack.Push(head.val); | ||
head = head.next; | ||
} | ||
var res = 0; | ||
var lvl = 1; | ||
res += stack.Pop(); | ||
while(stack.Count > 0){ | ||
lvl *= 2; | ||
res += lvl * stack.Pop(); | ||
} | ||
return res; | ||
*/ | ||
} | ||
} | ||
|
||
public class ListNode | ||
{ | ||
public int val; | ||
public ListNode next; | ||
public ListNode(int val = 0, ListNode next = null) | ||
{ | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
November-LeetCoding-Challenge/02-Insertion-Sort-List/Insertion-Sort-List.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace _147_Insertion_Sort_List | ||
{ | ||
public class Solution | ||
{ | ||
public ListNode InsertionSortList(ListNode head) | ||
{ | ||
ListNode dummy = new ListNode(0); | ||
ListNode prev = dummy; | ||
while (head != null) | ||
{ | ||
ListNode temp = head.next; | ||
|
||
/* Before insert, the prev is at the last node of the sorted list. | ||
Only the last node's value is larger than the current inserting node | ||
should we move the temp back to the head*/ | ||
if (prev.val >= head.val) prev = dummy; | ||
|
||
while (prev.next != null && prev.next.val < head.val) | ||
{ | ||
prev = prev.next; | ||
} | ||
|
||
head.next = prev.next; | ||
prev.next = head; | ||
// prev = dummy; // Don't set prev to the head of the list after insert | ||
head = temp; | ||
} | ||
return dummy.next; | ||
} | ||
} | ||
public class ListNode | ||
{ | ||
public int val; | ||
public ListNode next; | ||
public ListNode(int val = 0, ListNode next = null) | ||
{ | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
November-LeetCoding-Challenge/03-Consecutive-Characters/Consecutive-Characters.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace _1446_Consecutive_Characters | ||
{ | ||
public class Solution | ||
{ | ||
public int MaxPower(string s) | ||
{ | ||
int res = 1, cnt = 1; | ||
for (int i = 1; i < s.Length; i++) | ||
{ | ||
if (s[i] == s[i - 1]) | ||
{ | ||
cnt++; | ||
res = Math.Max(res, cnt); | ||
} | ||
else | ||
{ | ||
cnt = 1; | ||
} | ||
} | ||
return res; | ||
} | ||
} | ||
} |
Empty file.
14 changes: 14 additions & 0 deletions
14
...ost-to-Move-Chips-to-The-Same-Position/Minimum-Cost-to-Move-Chips-to-The-Same-Position.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Solution { | ||
public int MinCostToMoveChips(int[] position) { | ||
int even = 0, odd = 0; | ||
for(int i=0; i<position.Length; i++){ | ||
if(position[i] % 2 == 0){ | ||
even++; | ||
}else{ | ||
odd++; | ||
} | ||
} | ||
|
||
return Math.Min(even, odd); | ||
} | ||
} |
Empty file.
50 changes: 50 additions & 0 deletions
50
November-LeetCoding-Challenge/07-Add-Two-Numbers-II/Add-Two-Numbers-II.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace _445_Add_Two_Numbers_II | ||
{ | ||
public class Solution | ||
{ | ||
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) | ||
{ | ||
Stack<int> s1 = new Stack<int>(), s2 = new Stack<int>(); | ||
while (l1 != null) | ||
{ | ||
s1.Push(l1.val); | ||
l1 = l1.next; | ||
} | ||
while (l2 != null) | ||
{ | ||
s2.Push(l2.val); | ||
l2 = l2.next; | ||
} | ||
|
||
var sum = 0; | ||
var node = new ListNode(0); | ||
while (s1.Count > 0 || s2.Count > 0) | ||
{ | ||
if (s1.Count > 0) sum += s1.Pop(); | ||
if (s2.Count > 0) sum += s2.Pop(); | ||
node.val = sum % 10; | ||
|
||
var head = new ListNode(sum / 10); | ||
head.next = node; | ||
node = head; | ||
|
||
sum /= 10; | ||
} | ||
|
||
return node.val == 0 ? node.next : node; | ||
} | ||
} | ||
|
||
public class ListNode | ||
{ | ||
public int val; | ||
public ListNode next; | ||
public ListNode(int val = 0, ListNode next = null) | ||
{ | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
November-LeetCoding-Challenge/08-Binary-Tree-Tilt/Binary-Tree-Tilt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
|
||
namespace _563_Binary_Tree_Tilt | ||
{ | ||
public class Solution | ||
{ | ||
private int diff = 0; | ||
|
||
public int FindTilt(TreeNode root) { | ||
PostOrder(root); | ||
return diff; | ||
} | ||
|
||
private int PostOrder(TreeNode node){ | ||
if(node == null) return 0; | ||
|
||
var l = PostOrder(node.left); | ||
var r = PostOrder(node.right); | ||
|
||
diff += Math.Abs(l - r); | ||
|
||
return l + r + node.val; | ||
} | ||
} | ||
|
||
public class TreeNode { | ||
public int val; | ||
public TreeNode left; | ||
public TreeNode right; | ||
public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...imum-Difference-Between-Node-and-Ancestor/Maximum-Difference-Between-Node-and-Ancestor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
|
||
namespace _1026_Maximum_Difference_Between_Node_and_Ancestor | ||
{ | ||
public class Solution | ||
{ | ||
public int MaxAncestorDiff(TreeNode root) | ||
{ | ||
return Dfs(root, root.val, root.val); | ||
} | ||
|
||
public int Dfs(TreeNode node, int min, int max) | ||
{ | ||
if (node == null) return max - min; | ||
|
||
min = Math.Min(min, node.val); | ||
max = Math.Max(max, node.val); | ||
|
||
return Math.Max(Dfs(node.left, min, max), Dfs(node.right, min, max)); | ||
} | ||
} | ||
|
||
public class TreeNode | ||
{ | ||
public int val; | ||
public TreeNode left; | ||
public TreeNode right; | ||
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) | ||
{ | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
November-LeetCoding-Challenge/10-Flipping-an-Image/Flipping-an-Image.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace _832_Flipping_an_Image | ||
{ | ||
public class Solution | ||
{ | ||
public int[][] FlipAndInvertImage(int[][] A) { | ||
if(A == null || A.Length == 0) return new int[0][]; | ||
var levelLength = A[0].Length-1; | ||
int b =0; | ||
int e = levelLength; | ||
for(int i=0; i<=A.Length-1; i++){ | ||
b = 0; | ||
e = levelLength; | ||
while(b < e){ | ||
int temp = A[i][b]; | ||
A[i][b] = A[i][e]; | ||
A[i][e] = temp; | ||
b++; | ||
e--; | ||
} | ||
for(int j=0; j<=levelLength; j++){ | ||
A[i][j] = 1 - A[i][j]; | ||
} | ||
} | ||
return A; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
November-LeetCoding-Challenge/11-Valid-Square/Valid-Square.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace _593_Valid_Square | ||
{ | ||
public class Solution | ||
{ | ||
public bool ValidSquare(int[] p1, int[] p2, int[] p3, int[] p4) | ||
{ | ||
var arr = | ||
new int[] { | ||
Cal(p1, p2), | ||
Cal(p1, p3), | ||
Cal(p1, p4), | ||
Cal(p2, p3), | ||
Cal(p2, p4), | ||
Cal(p3, p4) | ||
}; | ||
HashSet<int> hs = new HashSet<int>(arr); | ||
return !hs.Contains(0) && hs.Count == 2; | ||
} | ||
|
||
private int Cal(int[] a, int[] b) | ||
{ | ||
return (a[0] - b[0]) * (a[0] - b[0]) + | ||
(a[1] - b[1]) * (a[1] - b[1]); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
November-LeetCoding-Challenge/12-Permutations-II/Permutations-II.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace _47_Permutations_II | ||
{ | ||
public class Solution | ||
{ | ||
public IList<IList<int>> PermuteUnique(int[] nums) { | ||
var res = new List<IList<int>>(); | ||
if(nums == null || nums.Length == 0) return res; | ||
|
||
Recursion(nums, 0, res); | ||
return res; | ||
} | ||
|
||
private void Recursion(int[] nums, int idx, List<IList<int>> li){ | ||
if(idx == nums.Length){ | ||
li.Add(nums.ToList()); | ||
return; | ||
} | ||
|
||
var hs = new HashSet<int>(); | ||
for(int i=idx; i<nums.Length; i++){ | ||
if(hs.Add(nums[i])){ | ||
Swap(nums, i, idx); | ||
Recursion(nums, idx+1, li); | ||
Swap(nums, i, idx); | ||
} | ||
} | ||
} | ||
|
||
private void Swap(int[] nums, int x, int y){ | ||
var tmp = nums[x]; | ||
nums[x] = nums[y]; | ||
nums[y] = tmp; | ||
} | ||
} | ||
} |
Oops, something went wrong.