Skip to content

Commit 0722a3b

Browse files
committed
clean
1 parent 42378ba commit 0722a3b

31 files changed

+105
-106
lines changed

134.CanCompleteCircuit.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// 134. Gas Station
22
// There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
3-
// You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
4-
// Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique
3+
// You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next
4+
// (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
5+
// Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit
6+
// once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique
57
// Example 1:
68
// Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
79
// Output: 3

138.CopyRandomList.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// 138. Copy List with Random Pointer
22

3-
// A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
4-
// Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
5-
// For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
3+
// A linked list of length n is given such that each node contains an additional random pointer, which could point to any node
4+
// in the list, or null.
5+
// Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its
6+
// value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point
7+
// to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state.
8+
// None of the pointers in the new list should point to nodes in the original list.
9+
// For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes
10+
// x and y in the copied list, x.random --> y.
611
// Return the head of the copied linked list.
7-
// The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
12+
// The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of
13+
// [val, random_index] where:
814
// val: an integer representing Node.val
915
// random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.
1016
// Your code will only be given the head of the original linked list.

142.DetectCycle.cs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
// 142. Linked List Cycle II
2-
32
// Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
4-
5-
// There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
6-
3+
// There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following
4+
// the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to
5+
// (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
76
// Do not modify the linked list.
87

9-
10-
118
// Example 1:
12-
13-
149
// Input: head = [3,2,0,-4], pos = 1
1510
// Output: tail connects to node index 1
1611
// Explanation: There is a cycle in the linked list, where tail connects to the second node.
1712
// Example 2:
18-
19-
2013
// Input: head = [1,2], pos = 0
2114
// Output: tail connects to node index 0
2215
// Explanation: There is a cycle in the linked list, where tail connects to the first node.
2316
// Example 3:
24-
25-
2617
// Input: head = [1], pos = -1
2718
// Output: no cycle
2819
// Explanation: There is no cycle in the linked list.

146.LRUCache.cs

-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
// void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
1010
// The functions get and put must each run in O(1) average time complexity.
1111

12-
13-
1412
// Example 1:
15-
1613
// Input
1714
// ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
1815
// [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
1916
// Output
2017
// [null, null, null, 1, null, -1, null, -1, 3, 4]
21-
2218
// Explanation
2319
// LRUCache lRUCache = new LRUCache(2);
2420
// lRUCache.put(1, 1); // cache is {1=1}

149.MaxPoints.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// 149. Max Points on a Line
2-
// Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
2+
// Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of
3+
// points that lie on the same straight line.
34
// Example 1:
45
// Input: points = [[1,1],[2,2],[3,3]]
56
// Output: 3

150.EvalRPN.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Evaluate the value of an arithmetic expression in Reverse Polish Notation.
33
// Valid operators are +, -, *, and /. Each operand may be an integer or another expression.
44
// Note that division between two integers should truncate toward zero.
5-
// It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
5+
// It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result,
6+
// and there will not be any division by zero operation.
67
// Example 1:
78
// Input: tokens = ["2","1","+","3","*"]
89
// Output: 9

153.FindMin.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// 153. Find Minimum in Rotated Sorted Array
2-
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
2+
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array
3+
// nums = [0,1,2,4,5,6,7] might become:
34
// [4,5,6,7,0,1,2] if it was rotated 4 times.
45
// [0,1,2,4,5,6,7] if it was rotated 7 times.
5-
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
6+
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array
7+
// [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
68
// Given the sorted rotated array nums of unique elements, return the minimum element of this array.
79
// You must write an algorithm that runs in O(log n) time.
810
// Example 1:

154.FindMin.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// 154. Find Minimum in Rotated Sorted Array II
2-
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become:
2+
// Suppose an array of length n sorted in ascending order is rotated between 1 and n times.
3+
// For example, the array nums = [0,1,4,4,5,6,7] might become:
34
// [4,5,6,7,0,1,4] if it was rotated 4 times.
45
// [0,1,4,4,5,6,7] if it was rotated 7 times.
5-
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
6+
// Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array
7+
// [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
68
// Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array.
79
// You must decrease the overall operation steps as much as possible.
810
// Example 1:

155.MinStack.cs

-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
// int getMin() retrieves the minimum element in the stack.
1212
// You must implement a solution with O(1) time complexity for each function.
1313

14-
15-
1614
// Example 1:
17-
1815
// Input
1916
// ["MinStack","push","push","push","getMin","pop","top","getMin"]
2017
// [[],[-2],[0],[-3],[],[],[],[]]

160.GetIntersectionNode.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 160. Intersection of Two Linked Lists
22

3-
// Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
3+
// Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect.
4+
// If the two linked lists have no intersection at all, return null.
45
// For example, the following two linked lists begin to intersect at node c1:
56

67
// The test cases are generated such that there are no cycles anywhere in the entire linked structure.

164.MaximumGap.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// 164. Maximum Gap
22

3-
// Given an integer array nums, return the maximum difference between two successive elements in its sorted form. If the array contains less than two elements, return 0.
3+
// Given an integer array nums, return the maximum difference between two successive elements in its sorted form.
4+
// If the array contains less than two elements, return 0.
45

56
// You must write an algorithm that runs in linear time and uses linear extra space.
67

78
// Example 1:
8-
99
// Input: nums = [3,6,9,1]
1010
// Output: 3
1111
// Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
1212
// Example 2:
13-
1413
// Input: nums = [10]
1514
// Output: 0
1615
// Explanation: The array contains less than 2 elements, therefore return 0.

169.MajorityElement.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//169. Majority Element
22
// Given an array nums of size n, return the majority element.
33

4-
// The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
4+
// The majority element is the element that appears more than ⌊n / 2⌋ times.
5+
// You may assume that the majority element always exists in the array.
56

67
public class Solution {
78
public int MajorityElement(int[] nums) {

191.HammingWeight.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
// Note:
66

7-
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
8-
// In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
9-
10-
7+
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as
8+
// a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same,
9+
// whether it is signed or unsigned.
10+
// In Java, the compiler represents the signed integers using 2's complement notation.
11+
// Therefore, in Example 3, the input represents the signed integer. -3.
1112
// Example 1:
12-
1313
// Input: n = 00000000000000000000000000001011
1414
// Output: 3
1515
// Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.

198.Rob.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// 198. House Robber
22

3-
// You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
3+
// You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed,
4+
// the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and
5+
// it will automatically contact the police if two adjacent houses were broken into on the same night.
46

5-
// Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
7+
// Given an integer array nums representing the amount of money of each house, return the maximum amount of money you
8+
// can rob tonight without alerting the police.
69

710
// Example 1:
811

200.NumIslands.cs

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
44

5-
// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
6-
7-
8-
5+
// An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
6+
// You may assume all four edges of the grid are all surrounded by water.
97
// Example 1:
10-
118
// Input: grid = [
129
// ["1","1","1","1","0"],
1310
// ["1","1","0","1","0"],
@@ -16,7 +13,6 @@
1613
// ]
1714
// Output: 1
1815
// Example 2:
19-
2016
// Input: grid = [
2117
// ["1","1","0","0","0"],
2218
// ["1","1","0","0","0"],
@@ -28,8 +24,7 @@
2824
public class Solution {
2925
bool[,] visited = new bool[300,300];
3026
public int NumIslands(char[][] grid)
31-
{
32-
27+
{
3328
int count=0;
3429
for(int i = 0; i < grid.Length; i++)
3530
{

203.RemoveElements.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//203. Remove Linked List Elements
2-
//Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
2+
//Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and
3+
// return the new head.
34
//definition for singly-linked list.
45
public class listnode
56
{

204.CountPrimes.cs

+19
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,23 @@ public int CountPrimes(int n) {
3030
}
3131
return count;
3232
}
33+
}
34+
35+
public class Solution {
36+
public int CountPrimes(int n) {
37+
if (n < 3) return n-1;
38+
int[] isNotPrime = new int[n];
39+
int count = 1; // count 2 as a prime
40+
for (int i = 3; i < n; i += 2) {
41+
if (isNotPrime[i] == 0) {
42+
count++;
43+
if ((long)i * i < n) {
44+
for (int j = i * i; j < n; j += 2 * i) {
45+
isNotPrime[j] = 1;
46+
}
47+
}
48+
}
49+
}
50+
return count;
51+
}
3352
}

206.ReverseList.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@
22

33
// Given the head of a singly linked list, reverse the list, and return the reversed list.
44

5-
6-
75
// Example 1:
8-
9-
106
// Input: head = [1,2,3,4,5]
117
// Output: [5,4,3,2,1]
128
// Example 2:
13-
14-
159
// Input: head = [1,2]
1610
// Output: [2,1]
1711
// Example 3:
18-
1912
// Input: head = []
2013
// Output: []
2114

22-
2315
/**
2416
* Definition for singly-linked list.
2517
* public class ListNode {
@@ -43,4 +35,16 @@ public ListNode ReverseList(ListNode head) {
4335
}
4436
return prev;
4537
}
38+
}
39+
40+
public class Solution {
41+
public ListNode ReverseList(ListNode head) {
42+
if (head == null || head.next == null) {
43+
return head;
44+
}
45+
ListNode newHead = ReverseList(head.next);
46+
head.next.next = head;
47+
head.next = null;
48+
return newHead;
49+
}
4650
}

207.CanFinish.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 207. Course Schedule
2-
// There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
3-
2+
// There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
3+
// You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first
4+
// if you want to take course ai.
45
// For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
56
// Return true if you can finish all courses. Otherwise, return false.
67

@@ -13,7 +14,8 @@
1314
// Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
1415
// Output: false
1516
// Explanation: There are a total of 2 courses to take.
16-
// To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
17+
// To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1.
18+
// So it is impossible.
1719

1820

1921
//Detect Cycle | Deadlock
@@ -26,11 +28,12 @@ public class Solution {
2628
public bool CanFinish(int numCourses, int[][] prerequisites)
2729
{
2830
List<int>[] path = new List<int>[numCourses];
29-
31+
//initialise List
3032
for(int i = 0; i < numCourses; i++)
3133
{
3234
path[i] = new List<int>();
3335
}
36+
//Add nbr
3437
foreach(var item in prerequisites)
3538
{
3639
path[item[0]].Add(item[1]);

210.FindOrder.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
// 210. Course Schedule II
2-
// M
3-
// There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
2+
3+
// There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1.
4+
// You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if
5+
// you want to take course ai.
46

57
// For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
6-
// Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
8+
// Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them.
9+
// If it is impossible to finish all courses, return an empty array.
710

811
// Example 1:
9-
1012
// Input: numCourses = 2, prerequisites = [[1,0]]
1113
// Output: [0,1]
1214
// Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
1315
// Example 2:
14-
1516
// Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
1617
// Output: [0,2,1,3]
1718
// Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
1819
// So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
1920
// Example 3:
20-
2121
// Input: numCourses = 1, prerequisites = []
2222
// Output: [0]
2323

212.FindWords.cs

-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
// Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
55

66
// Example 1:
7-
8-
97
// Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
108
// Output: ["eat","oath"]
119
// Example 2:
12-
13-
1410
// Input: board = [["a","b"],["c","d"]], words = ["abcb"]
1511
// Output: []
1612

0 commit comments

Comments
 (0)