Skip to content

Commit a050ff5

Browse files
committed
upload
1 parent 5b0c039 commit a050ff5

File tree

34 files changed

+1001
-0
lines changed

34 files changed

+1001
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Design Circular Queue
2+
3+
Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
4+
5+
One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
6+
7+
Your implementation should support following operations:
8+
9+
- MyCircularQueue(k): Constructor, set the size of the queue to be k.
10+
- Front: Get the front item from the queue. If the queue is empty, return -1.
11+
- Rear: Get the last item from the queue. If the queue is empty, return -1.
12+
- enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
13+
- deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
14+
- isEmpty(): Checks whether the circular queue is empty or not.
15+
- isFull(): Checks whether the circular queue is full or not.
16+
17+
18+
#### Example:
19+
```text
20+
MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
21+
circularQueue.enQueue(1); // return true
22+
circularQueue.enQueue(2); // return true
23+
circularQueue.enQueue(3); // return true
24+
circularQueue.enQueue(4); // return false, the queue is full
25+
circularQueue.Rear(); // return 3
26+
circularQueue.isFull(); // return true
27+
circularQueue.deQueue(); // return true
28+
circularQueue.enQueue(4); // return true
29+
circularQueue.Rear(); // return 4
30+
```
31+
32+
33+
### Note:
34+
35+
- All values will be in the range of [0, 1000].
36+
- The number of operations will be in the range of [1, 1000].
37+
- Please do not use the built-in Queue library.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class MyCircularQueue {
2+
3+
private Integer[] nums;
4+
private int head;
5+
private int tail;
6+
private int size;
7+
8+
/** Initialize your data structure here. Set the size of the queue to be k. */
9+
public MyCircularQueue(int k) {
10+
this.nums = new Integer[k];
11+
this.head = -1;
12+
this.tail = -1;
13+
this.size = 0;
14+
}
15+
16+
/** Insert an element into the circular queue. Return true if the operation is successful. */
17+
public boolean enQueue(int value) {
18+
if (isFull()) {
19+
return false;
20+
} else if(this.head == this.tail && this.tail == -1){
21+
this.head++;
22+
this.tail++;
23+
nums[this.tail] = value;
24+
} else {
25+
this.tail = (this.tail + 1) % nums.length;
26+
this.nums[this.tail] = value;
27+
}
28+
this.size++;
29+
return true;
30+
}
31+
32+
/** Delete an element from the circular queue. Return true if the operation is successful. */
33+
public boolean deQueue() {
34+
if (isEmpty()) {
35+
return false;
36+
} else if (this.head == this.tail) {
37+
this.head = -1;
38+
this.tail = -1;
39+
} else {
40+
this.head = (this.head + 1) % this.nums.length;
41+
}
42+
this.size--;
43+
return true;
44+
}
45+
46+
/** Get the front item from the queue. */
47+
public int Front() {
48+
if (isEmpty()) {
49+
return -1;
50+
} else {
51+
return this.nums[this.head];
52+
}
53+
}
54+
55+
/** Get the last item from the queue. */
56+
public int Rear() {
57+
if (isEmpty()) {
58+
return -1;
59+
} else {
60+
return this.nums[this.tail];
61+
}
62+
}
63+
64+
/** Checks whether the circular queue is empty or not. */
65+
public boolean isEmpty() {
66+
if (this.size == 0) {
67+
return true;
68+
} else {
69+
return false;
70+
}
71+
}
72+
73+
/** Checks whether the circular queue is full or not. */
74+
public boolean isFull() {
75+
if (this.size == this.nums.length) {
76+
return true;
77+
} else {
78+
return false;
79+
}
80+
}
81+
}
82+
83+
/**
84+
* Your MyCircularQueue object will be instantiated and called as such:
85+
* MyCircularQueue obj = new MyCircularQueue(k);
86+
* boolean param_1 = obj.enQueue(value);
87+
* boolean param_2 = obj.deQueue();
88+
* int param_3 = obj.Front();
89+
* int param_4 = obj.Rear();
90+
* boolean param_5 = obj.isEmpty();
91+
* boolean param_6 = obj.isFull();
92+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Count Binary Substrings
2+
3+
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.
4+
5+
Substrings that occur multiple times are counted the number of times they occur.
6+
7+
## Example 1:
8+
```text
9+
Input: "00110011"
10+
Output: 6
11+
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".
12+
13+
Notice that some of these substrings repeat and are counted the number of times they occur.
14+
15+
Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
16+
```
17+
18+
## Example 2:
19+
```text
20+
Input: "10101"
21+
Output: 4
22+
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
23+
```
24+
25+
## Note:
26+
27+
- s.length will be between 1 and 50,000.
28+
- s will only consist of "0" or "1" characters
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int countBinarySubstrings(String s) {
3+
int[] group = new int[s.length()];
4+
int k = 0;
5+
Arrays.fill(group , 0);
6+
group[0] = 1;
7+
for(int i = 1;i < s.length();i++) {
8+
if(s.charAt(i) == s.charAt(i-1))
9+
group[k]++;
10+
else
11+
group[++k] = 1;
12+
}
13+
int ans = 0;
14+
for(int i = 1;i < s.length() && group[i] != 0;i++) {
15+
ans += Math.min(group[i-1], group[i]);
16+
}
17+
return ans;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Search in a Binary Search Tree
2+
3+
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
4+
5+
For example,
6+
```text
7+
Given the tree:
8+
4
9+
/ \
10+
2 7
11+
/ \
12+
1 3
13+
14+
And the value to search: 2
15+
```
16+
17+
You should return this subtree:
18+
```text
19+
2
20+
/ \
21+
1 3
22+
```
23+
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
24+
25+
Note that an empty tree is repres
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode searchBST(TreeNode root, int val) {
12+
if (root == null) return null;
13+
if (root.val == val) return root;
14+
if (root.val < val) return searchBST(root.right, val);
15+
else return searchBST(root.left, val);
16+
}
17+
}

solution/0752.Open the Lock/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Open the Lock
2+
3+
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.
4+
5+
The lock initially starts at '0000', a string representing the state of the 4 wheels.
6+
7+
You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
8+
9+
Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
10+
11+
## Example 1:
12+
```text
13+
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
14+
Output: 6
15+
Explanation:
16+
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
17+
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
18+
because the wheels of the lock become stuck after the display becomes the dead end "0102".
19+
```
20+
21+
## Example 2:
22+
```text
23+
Input: deadends = ["8888"], target = "0009"
24+
Output: 1
25+
Explanation:
26+
We can turn the last wheel in reverse to move from "0000" -> "0009".
27+
```
28+
29+
## Example 3:
30+
```text
31+
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
32+
Output: -1
33+
Explanation:
34+
We can't reach the target without getting stuck.
35+
```
36+
37+
## Example 4:
38+
```text
39+
Input: deadends = ["0000"], target = "8888"
40+
Output: -1
41+
```
42+
43+
## Note:
44+
- The length of deadends will be in the range [1, 500].
45+
- target will not be in the list deadends.
46+
- Every string in deadends and the string target will be a string of 4 digits from the 10,000 possibilities '0000' to '9999'.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int openLock(String[] deadends, String target) {
3+
Set<String> begins = new HashSet<>();
4+
Set<String> deads = new HashSet<>(Arrays.asList(deadends));
5+
int step = 0;
6+
7+
begins.add("0000");
8+
if (begins.contains(target)) {
9+
return step;
10+
}
11+
12+
while (!begins.isEmpty()) {
13+
if (begins.contains(target)) {
14+
return step;
15+
}
16+
17+
Set<String> temp = new HashSet<>();
18+
19+
for (String cur : begins) {
20+
if (deads.contains(cur)) {
21+
continue;
22+
}
23+
deads.add(cur);
24+
StringBuffer s = new StringBuffer(cur);
25+
for (int i = 0; i < 4; i++) {
26+
char c = s.charAt(i);
27+
String s1 = s.substring(0, i) + (char)(c == '9' ? '0' : c + 1) + s.substring(i + 1, 4);
28+
String s2 = s.substring(0, i) + (char)(c == '0' ? '9' : c - 1) + s.substring(i + 1, 4);
29+
if (!deads.contains(s1)) {
30+
temp.add(s1);
31+
}
32+
if (!deads.contains(s2)) {
33+
temp.add(s2);
34+
}
35+
}
36+
}
37+
step ++;
38+
begins = temp;
39+
}
40+
41+
return -1;
42+
}
43+
}

solution/0912.Sort an Array/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sort an Array
2+
3+
Given an array of integers nums, sort the array in ascending order.
4+
5+
6+
7+
## Example 1:
8+
```text
9+
Input: [5,2,3,1]
10+
Output: [1,2,3,5]
11+
```
12+
13+
## Example 2:
14+
```text
15+
Input: [5,1,1,2,0,0]
16+
Output: [0,0,1,1,2,5]
17+
```
18+
19+
## Note:
20+
21+
- 1 <= A.length <= 10000
22+
- -50000 <= A[i] <= 50000
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
3+
void createHeap(int[] data, int n, int h) {
4+
int i = h;
5+
int j = 2 * i + 1;
6+
int temp = data[i];
7+
8+
while (j < n) {
9+
if (j + 1 < n && data[j] < data[j + 1]) j++;
10+
if (temp > data[j]) {
11+
break;
12+
} else {
13+
data[i] = data[j];
14+
i = j;
15+
j = 2 * i + 1;
16+
}
17+
}
18+
data[i] = temp;
19+
}
20+
21+
void initHeap(int[] data, int n) {
22+
for (int i = (n - 2) / 2; i > -1; i--) {
23+
createHeap(data, n, i);
24+
}
25+
}
26+
27+
void heapSort(int[] data, int n) {
28+
initHeap(data, n);
29+
30+
for (int i = n - 1;i > -1;i--) {
31+
int temp = data[i];
32+
data[i] = data[0];
33+
data[0] = temp;
34+
createHeap(data, i, 0);
35+
}
36+
}
37+
38+
public int[] sortArray(int[] nums) {
39+
heapSort(nums, nums.length);
40+
return nums;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Complement of Base 10 Integer
2+
Every non-negative integer N has a binary representation. For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation.
3+
4+
The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of "101" in binary is "010" in binary.
5+
6+
For a given number N in base-10, return the complement of it's binary representation as a base-10 integer.
7+
8+
#### Example1
9+
```text
10+
Input: 5
11+
Output: 2
12+
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
13+
```
14+
15+
#### Node
16+
0 <= N < 10^9

0 commit comments

Comments
 (0)