Skip to content

Commit 5a12b24

Browse files
committed
Add solutions
1 parent c59cae0 commit 5a12b24

File tree

23 files changed

+712
-0
lines changed

23 files changed

+712
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace _1290_Convert_Binary_Number_in_a_Linked_List_to_Integer
2+
{
3+
public class Solution
4+
{
5+
public int GetDecimalValue(ListNode head)
6+
{
7+
//Solution 1
8+
var res = 0;
9+
while (head != null)
10+
{
11+
res = res * 2 + head.val;
12+
head = head.next;
13+
}
14+
return res;
15+
16+
//Solution 2: Stack, from end to beginning
17+
/*
18+
var stack = new Stack<int>();
19+
while(head != null){
20+
stack.Push(head.val);
21+
head = head.next;
22+
}
23+
24+
var res = 0;
25+
var lvl = 1;
26+
res += stack.Pop();
27+
28+
while(stack.Count > 0){
29+
lvl *= 2;
30+
res += lvl * stack.Pop();
31+
}
32+
33+
return res;
34+
*/
35+
}
36+
}
37+
38+
public class ListNode
39+
{
40+
public int val;
41+
public ListNode next;
42+
public ListNode(int val = 0, ListNode next = null)
43+
{
44+
this.val = val;
45+
this.next = next;
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace _147_Insertion_Sort_List
2+
{
3+
public class Solution
4+
{
5+
public ListNode InsertionSortList(ListNode head)
6+
{
7+
ListNode dummy = new ListNode(0);
8+
ListNode prev = dummy;
9+
while (head != null)
10+
{
11+
ListNode temp = head.next;
12+
13+
/* Before insert, the prev is at the last node of the sorted list.
14+
Only the last node's value is larger than the current inserting node
15+
should we move the temp back to the head*/
16+
if (prev.val >= head.val) prev = dummy;
17+
18+
while (prev.next != null && prev.next.val < head.val)
19+
{
20+
prev = prev.next;
21+
}
22+
23+
head.next = prev.next;
24+
prev.next = head;
25+
// prev = dummy; // Don't set prev to the head of the list after insert
26+
head = temp;
27+
}
28+
return dummy.next;
29+
}
30+
}
31+
public class ListNode
32+
{
33+
public int val;
34+
public ListNode next;
35+
public ListNode(int val = 0, ListNode next = null)
36+
{
37+
this.val = val;
38+
this.next = next;
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace _1446_Consecutive_Characters
4+
{
5+
public class Solution
6+
{
7+
public int MaxPower(string s)
8+
{
9+
int res = 1, cnt = 1;
10+
for (int i = 1; i < s.Length; i++)
11+
{
12+
if (s[i] == s[i - 1])
13+
{
14+
cnt++;
15+
res = Math.Max(res, cnt);
16+
}
17+
else
18+
{
19+
cnt = 1;
20+
}
21+
}
22+
return res;
23+
}
24+
}
25+
}

November-LeetCoding-Challenge/04-Minimum-Height-Trees/Minimum-Height-Trees.cs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int MinCostToMoveChips(int[] position) {
3+
int even = 0, odd = 0;
4+
for(int i=0; i<position.Length; i++){
5+
if(position[i] % 2 == 0){
6+
even++;
7+
}else{
8+
odd++;
9+
}
10+
}
11+
12+
return Math.Min(even, odd);
13+
}
14+
}

November-LeetCoding-Challenge/06-Find-the-Smallest-Divisor-Given-a-Threshold/Find-the-Smallest-Divisor-Given-a-Threshold.cs

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
3+
namespace _445_Add_Two_Numbers_II
4+
{
5+
public class Solution
6+
{
7+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
8+
{
9+
Stack<int> s1 = new Stack<int>(), s2 = new Stack<int>();
10+
while (l1 != null)
11+
{
12+
s1.Push(l1.val);
13+
l1 = l1.next;
14+
}
15+
while (l2 != null)
16+
{
17+
s2.Push(l2.val);
18+
l2 = l2.next;
19+
}
20+
21+
var sum = 0;
22+
var node = new ListNode(0);
23+
while (s1.Count > 0 || s2.Count > 0)
24+
{
25+
if (s1.Count > 0) sum += s1.Pop();
26+
if (s2.Count > 0) sum += s2.Pop();
27+
node.val = sum % 10;
28+
29+
var head = new ListNode(sum / 10);
30+
head.next = node;
31+
node = head;
32+
33+
sum /= 10;
34+
}
35+
36+
return node.val == 0 ? node.next : node;
37+
}
38+
}
39+
40+
public class ListNode
41+
{
42+
public int val;
43+
public ListNode next;
44+
public ListNode(int val = 0, ListNode next = null)
45+
{
46+
this.val = val;
47+
this.next = next;
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace _563_Binary_Tree_Tilt
4+
{
5+
public class Solution
6+
{
7+
private int diff = 0;
8+
9+
public int FindTilt(TreeNode root) {
10+
PostOrder(root);
11+
return diff;
12+
}
13+
14+
private int PostOrder(TreeNode node){
15+
if(node == null) return 0;
16+
17+
var l = PostOrder(node.left);
18+
var r = PostOrder(node.right);
19+
20+
diff += Math.Abs(l - r);
21+
22+
return l + r + node.val;
23+
}
24+
}
25+
26+
public class TreeNode {
27+
public int val;
28+
public TreeNode left;
29+
public TreeNode right;
30+
public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
31+
this.val = val;
32+
this.left = left;
33+
this.right = right;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace _1026_Maximum_Difference_Between_Node_and_Ancestor
4+
{
5+
public class Solution
6+
{
7+
public int MaxAncestorDiff(TreeNode root)
8+
{
9+
return Dfs(root, root.val, root.val);
10+
}
11+
12+
public int Dfs(TreeNode node, int min, int max)
13+
{
14+
if (node == null) return max - min;
15+
16+
min = Math.Min(min, node.val);
17+
max = Math.Max(max, node.val);
18+
19+
return Math.Max(Dfs(node.left, min, max), Dfs(node.right, min, max));
20+
}
21+
}
22+
23+
public class TreeNode
24+
{
25+
public int val;
26+
public TreeNode left;
27+
public TreeNode right;
28+
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
29+
{
30+
this.val = val;
31+
this.left = left;
32+
this.right = right;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace _832_Flipping_an_Image
2+
{
3+
public class Solution
4+
{
5+
public int[][] FlipAndInvertImage(int[][] A) {
6+
if(A == null || A.Length == 0) return new int[0][];
7+
var levelLength = A[0].Length-1;
8+
int b =0;
9+
int e = levelLength;
10+
for(int i=0; i<=A.Length-1; i++){
11+
b = 0;
12+
e = levelLength;
13+
while(b < e){
14+
int temp = A[i][b];
15+
A[i][b] = A[i][e];
16+
A[i][e] = temp;
17+
b++;
18+
e--;
19+
}
20+
for(int j=0; j<=levelLength; j++){
21+
A[i][j] = 1 - A[i][j];
22+
}
23+
}
24+
return A;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
3+
namespace _593_Valid_Square
4+
{
5+
public class Solution
6+
{
7+
public bool ValidSquare(int[] p1, int[] p2, int[] p3, int[] p4)
8+
{
9+
var arr =
10+
new int[] {
11+
Cal(p1, p2),
12+
Cal(p1, p3),
13+
Cal(p1, p4),
14+
Cal(p2, p3),
15+
Cal(p2, p4),
16+
Cal(p3, p4)
17+
};
18+
HashSet<int> hs = new HashSet<int>(arr);
19+
return !hs.Contains(0) && hs.Count == 2;
20+
}
21+
22+
private int Cal(int[] a, int[] b)
23+
{
24+
return (a[0] - b[0]) * (a[0] - b[0]) +
25+
(a[1] - b[1]) * (a[1] - b[1]);
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace _47_Permutations_II
5+
{
6+
public class Solution
7+
{
8+
public IList<IList<int>> PermuteUnique(int[] nums) {
9+
var res = new List<IList<int>>();
10+
if(nums == null || nums.Length == 0) return res;
11+
12+
Recursion(nums, 0, res);
13+
return res;
14+
}
15+
16+
private void Recursion(int[] nums, int idx, List<IList<int>> li){
17+
if(idx == nums.Length){
18+
li.Add(nums.ToList());
19+
return;
20+
}
21+
22+
var hs = new HashSet<int>();
23+
for(int i=idx; i<nums.Length; i++){
24+
if(hs.Add(nums[i])){
25+
Swap(nums, i, idx);
26+
Recursion(nums, idx+1, li);
27+
Swap(nums, i, idx);
28+
}
29+
}
30+
}
31+
32+
private void Swap(int[] nums, int x, int y){
33+
var tmp = nums[x];
34+
nums[x] = nums[y];
35+
nums[y] = tmp;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)