Skip to content

Commit c38bef6

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 410335f commit c38bef6

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed

leetcode/125. Valid Palindrome.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 125. Valid Palindrome
2+
3+
### Question:
4+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
5+
6+
Note: For the purpose of this problem, we define empty string as valid palindrome.
7+
8+
```
9+
Example 1:
10+
11+
Input: "A man, a plan, a canal: Panama"
12+
Output: true
13+
14+
Example 2:
15+
16+
Input: "race a car"
17+
Output: false
18+
```
19+
20+
### 二刷
21+
```Java
22+
class Solution {
23+
public boolean isPalindrome(String s) {
24+
if(s == null) return false;
25+
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
26+
int len = s.length();
27+
if(len == 0) return true;
28+
int head = 0, end = s.length() - 1;
29+
char[] arr = s.toCharArray();
30+
while(head < end){
31+
while(!isAlphanumeric(arr[head])){
32+
if(head + 1 >= len) return true;
33+
head++;
34+
}
35+
while(!isAlphanumeric(arr[end])){
36+
if(end - 1 < 0) return true;
37+
end--;
38+
}
39+
if(head > end) return true;
40+
if(arr[head++] != arr[end--]) return false;
41+
}
42+
return true;
43+
}
44+
public boolean isAlphanumeric(char c) {
45+
if ((c >=48 && c <=57) || (c >= 97 && c <= 122)) return true;
46+
return false;
47+
}
48+
}
49+
```

leetcode/468. Validate IP Address.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## 468. Validate IP Address
2+
3+
### Question
4+
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
5+
6+
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
7+
8+
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
9+
10+
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
11+
12+
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
13+
14+
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
15+
16+
Note: You may assume there is no extra space or special characters in the input string.
17+
18+
```
19+
Example 1:
20+
21+
Input: "172.16.254.1"
22+
23+
Output: "IPv4"
24+
25+
Explanation: This is a valid IPv4 address, return "IPv4".
26+
27+
Example 2:
28+
29+
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
30+
31+
Output: "IPv6"
32+
33+
Explanation: This is a valid IPv6 address, return "IPv6".
34+
35+
Example 3:
36+
37+
Input: "256.256.256.256"
38+
39+
Output: "Neither"
40+
41+
Explanation: This is neither a IPv4 address nor a IPv6 address.
42+
```
43+
44+
### Solutions
45+
* Method 1: Condition checking
46+
```Java
47+
class Solution {
48+
public String validIPAddress(String IP) {
49+
if(IP.indexOf(".") != -1){
50+
return isIpv4(IP) ? "IPv4": "Neither";
51+
}else{
52+
return isIpv6(IP) ? "IPv6": "Neither";
53+
}
54+
}
55+
private boolean isIpv4(String s){
56+
if(s.startsWith(".") || s.endsWith(".")) return false;
57+
String[] arr = s.split("\\.");
58+
if(arr.length != 4 || s.indexOf(":") != -1) return false;
59+
for(String token : arr){
60+
int len = token.length();
61+
if(len == 0 || len > 3 || (!token.equals("0") && token.startsWith("0")) || !isNumber(token)) return false;
62+
int num = Integer.parseInt(token);
63+
if(num > 255) return false;
64+
}
65+
return true;
66+
}
67+
private boolean isNumber(String s){
68+
for(int i = 0; i < s.length(); i++){
69+
if(!Character.isDigit(s.charAt(i)))
70+
return false;
71+
}
72+
return true;
73+
}
74+
private boolean isIpv6(String s){
75+
if(s.startsWith(":") || s.endsWith(":")) return false;
76+
String[] arr = s.split(":");
77+
if(arr.length != 8 || s.indexOf(".") != -1) return false;
78+
for(String token : arr){
79+
if(!isIpv6Token(token)) return false;
80+
int len = token.length();
81+
if(len == 0 || len > 4) return false;
82+
}
83+
return true;
84+
}
85+
private boolean isIpv6Token(String s){
86+
for(char c : s.toCharArray()){
87+
if((c >= '0' && c <= '9')) continue;
88+
else if(c >= 'a' && c <= 'f') continue;
89+
else if(c >= 'A' && c <= 'F') continue;
90+
return false;
91+
}
92+
return true;
93+
}
94+
}
95+
```
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
## 529. Minesweeper
2+
3+
### Question
4+
Let's play the minesweeper game (Wikipedia, online game)!
5+
6+
You are given a 2D char matrix representing the game board. 'M' represents an unrevealed mine, 'E' represents an unrevealed empty square, 'B' represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit ('1' to '8') represents how many mines are adjacent to this revealed square, and finally 'X' represents a revealed mine.
7+
8+
Now given the next click position (row and column indices) among all the unrevealed squares ('M' or 'E'), return the board after revealing this position according to the following rules:
9+
10+
* If a mine ('M') is revealed, then the game is over - change it to 'X'.
11+
* If an empty square ('E') with no adjacent mines is revealed, then change it to revealed blank ('B') and all of its adjacent unrevealed squares should be revealed recursively.
12+
* If an empty square ('E') with at least one adjacent mine is revealed, then change it to a digit ('1' to '8') representing the number of adjacent mines.
13+
* Return the board when no more squares will be revealed.
14+
15+
16+
```
17+
Example 1:
18+
19+
Input:
20+
21+
[['E', 'E', 'E', 'E', 'E'],
22+
['E', 'E', 'M', 'E', 'E'],
23+
['E', 'E', 'E', 'E', 'E'],
24+
['E', 'E', 'E', 'E', 'E']]
25+
26+
Click : [3,0]
27+
28+
Output:
29+
30+
[['B', '1', 'E', '1', 'B'],
31+
['B', '1', 'M', '1', 'B'],
32+
['B', '1', '1', '1', 'B'],
33+
['B', 'B', 'B', 'B', 'B']]
34+
35+
Explanation:
36+
37+
Example 2:
38+
39+
Input:
40+
41+
[['B', '1', 'E', '1', 'B'],
42+
['B', '1', 'M', '1', 'B'],
43+
['B', '1', '1', '1', 'B'],
44+
['B', 'B', 'B', 'B', 'B']]
45+
46+
Click : [1,2]
47+
48+
Output:
49+
50+
[['B', '1', 'E', '1', 'B'],
51+
['B', '1', 'X', '1', 'B'],
52+
['B', '1', '1', '1', 'B'],
53+
['B', 'B', 'B', 'B', 'B']]
54+
```
55+
56+
57+
Note:
58+
1. The range of the input matrix's height and width is [1,50].
59+
2. The click position will only be an unrevealed square ('M' or 'E'), which also means the input board contains at least one clickable square.
60+
3. The input board won't be a stage when game is over (some mines have been revealed).
61+
4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don't need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.
62+
63+
64+
### Solutions
65+
* Method 1: Recursion => Actually is dfs
66+
```Java
67+
class Solution {
68+
private static final int[][] dir = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
69+
private char[][] board;
70+
private int height, width;
71+
private Set<Integer> visited;
72+
private int[] click;
73+
public char[][] updateBoard(char[][] board, int[] click) {
74+
this.board = board;
75+
this.height = board.length;
76+
this.width = board[0].length;
77+
visited = new HashSet<>();
78+
this.click = click;
79+
click(click[0], click[1]);
80+
return this.board;
81+
}
82+
private void click(int x, int y){
83+
visited.add(x * width + y);
84+
if(board[x][y] == 'M'){
85+
if(x == click[0] && y == click[1])
86+
board[x][y] = 'X';
87+
return;
88+
}else{
89+
int mimeNum = checkMimeNum(x, y);
90+
board[x][y] = mimeNum > 0 ? (char)('0' + mimeNum): 'B';
91+
if(mimeNum == 0){
92+
int tx = 0, ty = 0;
93+
for(int d = 0; d < 8; d++){
94+
tx = x + dir[d][0];
95+
ty = y + dir[d][1];
96+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && !visited.contains(tx * width + ty))
97+
click(tx, ty);
98+
}
99+
}
100+
}
101+
}
102+
private int checkMimeNum(int x, int y){
103+
int tx = 0, ty = 0;
104+
int count = 0;
105+
for(int d = 0; d < 8; d++){
106+
tx = x + dir[d][0];
107+
ty = y + dir[d][1];
108+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && board[tx][ty] == 'M')
109+
count++;
110+
}
111+
return count;
112+
}
113+
}
114+
```

0 commit comments

Comments
 (0)