Skip to content

Commit 162b5ae

Browse files
author
FreeTymeKiyan
committed
add 30 and update other solutions
1 parent df71192 commit 162b5ae

File tree

8 files changed

+378
-314
lines changed

8 files changed

+378
-314
lines changed

src/main/java/com/freetymekiyan/algorithms/level/easy/CountAndSay.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@
1717
* Tags: String
1818
* Similar Problems: (M) Encode and Decode Strings
1919
*/
20-
2120
class CountAndSay {
2221

23-
/**
24-
* String. Simulate the process.
25-
* Generate the next sequence.
26-
* Repeat n-1 times.
27-
*/
28-
public String countAndSay(int n) {
29-
String res = "1";
30-
while (n > 1) {
31-
StringBuilder s = new StringBuilder();
32-
for (int i = 0, j = i; i < res.length(); i = j) {
33-
while (j < res.length() && res.charAt(i) == res.charAt(j)) {
34-
j++;
35-
}
36-
s.append(j - i).append(res.charAt(i));
37-
}
38-
res = s.toString();
39-
n--;
40-
}
41-
return res;
22+
/**
23+
* String. Simulate the process.
24+
* Generate the next sequence.
25+
* Repeat n-1 times.
26+
*/
27+
public String countAndSay(int n) {
28+
String res = "1";
29+
while (n > 1) {
30+
StringBuilder s = new StringBuilder();
31+
for (int i = 0, j = i; i < res.length(); i = j) {
32+
while (j < res.length() && res.charAt(i) == res.charAt(j)) {
33+
j++;
34+
}
35+
s.append(j - i).append(res.charAt(i));
4236
}
37+
res = s.toString();
38+
n--;
39+
}
40+
return res;
41+
}
4342
}

src/main/java/com/freetymekiyan/algorithms/level/easy/ValidSudoku.java

Lines changed: 108 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,131 @@
55

66
/**
77
* Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
8-
*
8+
* <p>
99
* The Sudoku board could be partially filled, where empty cells are filled
1010
* with the character '.'.
11-
*
11+
* <p>
1212
* Note:
1313
* A valid Sudoku board (partially filled) is not necessarily solvable. Only
1414
* the filled cells need to be validated.
15-
*
15+
* <p>
1616
* Tags: Hash table
1717
*/
1818
class ValidSudoku {
19-
public static void main(String[] args) {
20-
ValidSudoku v = new ValidSudoku();
21-
22-
char[][] board = new char[9][9];
23-
for (int i = 0; i < board.length; i++) {
24-
for (int j = 0; j < board[i].length; j++) {
25-
if (i == 0 && j == 0) {
26-
board[i][j] = '.';
27-
} else if (i == 0 && j != 0) {
28-
board[i][j] = (char)('0' + j + 1);
29-
} else {
30-
board[i][j] = '.';
31-
}
32-
}
19+
public static void main(String[] args) {
20+
ValidSudoku v = new ValidSudoku();
21+
22+
char[][] board = new char[9][9];
23+
for (int i = 0; i < board.length; i++) {
24+
for (int j = 0; j < board[i].length; j++) {
25+
if (i == 0 && j == 0) {
26+
board[i][j] = '.';
27+
} else if (i == 0 && j != 0) {
28+
board[i][j] = (char) ('0' + j + 1);
29+
} else {
30+
board[i][j] = '.';
3331
}
34-
v.printBoard(board);
35-
System.out.println(v.isValidSudoku(board));
32+
}
3633
}
37-
38-
public boolean isValidSudoku(char[][] board) {
39-
boolean[][] row = new boolean[9][9];
40-
boolean[][] col = new boolean[9][9];
41-
boolean[][] box = new boolean[9][9];
42-
43-
for (int i = 0; i < 9; i++) {
44-
for (int j = 0; j < 9; j++) {
45-
if (board[i][j] != '.') {
46-
int num = board[i][j] - '0' - 1;
47-
int k = i / 3 * 3 + j / 3;
48-
if (row[i][num] || col[j][num] || box[k][num]) {
49-
return false;
50-
}
51-
row[i][num] = col[j][num] = box[k][num] = true;
52-
}
53-
}
34+
v.printBoard(board);
35+
System.out.println(v.isValidSudoku(board));
36+
}
37+
38+
public boolean isValidSudoku(char[][] board) {
39+
boolean[][] row = new boolean[9][9];
40+
boolean[][] col = new boolean[9][9];
41+
boolean[][] box = new boolean[9][9];
42+
43+
for (int i = 0; i < 9; i++) {
44+
for (int j = 0; j < 9; j++) {
45+
if (board[i][j] != '.') {
46+
int num = board[i][j] - '0' - 1;
47+
int k = i / 3 * 3 + j / 3;
48+
if (row[i][num] || col[j][num] || box[k][num]) {
49+
return false;
50+
}
51+
row[i][num] = col[j][num] = box[k][num] = true;
5452
}
55-
return true;
53+
}
5654
}
55+
return true;
56+
}
57+
58+
/**
59+
* Use three arrays of integers to do masking
60+
*/
61+
public boolean isValidSudoku2(char[][] board) {
62+
int[] row = new int[9];
63+
int[] col = new int[9];
64+
int[] sqr = new int[9];
65+
66+
for (int i = 0; i < 9; i++) {
67+
for (int j = 0; j < 9; j++) {
68+
if (board[i][j] != '.') {
69+
int num = board[i][j] - '0';
70+
if ((row[i] & 1 << num) > 0) return false; // already in row
71+
else row[i] |= 1 << num;
72+
73+
if ((col[j] & 1 << num) > 0) return false;// already in col
74+
else col[j] |= 1 << num;
75+
76+
int sqrIdx = (i - i % 3) + j / 3; // note the square idx
77+
if ((sqr[sqrIdx] & 1 << num) > 0) return false; // already
78+
else sqr[sqrIdx] |= 1 << num;
5779

58-
/**
59-
* Use three arrays of integers to do masking
60-
*/
61-
public boolean isValidSudoku2(char[][] board) {
62-
int[] row = new int[9];
63-
int[] col = new int[9];
64-
int[] sqr = new int[9];
65-
66-
for (int i = 0; i < 9; i++){
67-
for (int j = 0; j < 9; j++) {
68-
if (board[i][j] != '.') {
69-
int num = board[i][j] - '0';
70-
if ((row[i] & 1 << num) > 0) return false; // already in row
71-
else row[i] |= 1 << num;
72-
73-
if ((col[j] & 1 << num) > 0) return false;// already in col
74-
else col[j] |= 1 << num;
75-
76-
int sqrIdx = (i - i % 3) + j / 3; // note the square idx
77-
if ((sqr[sqrIdx] & 1 << num) > 0) return false; // already
78-
else sqr[sqrIdx] |= 1 << num;
79-
80-
}
81-
}
8280
}
83-
return true;
84-
81+
}
8582
}
86-
87-
/**
88-
* hashtable, index as key, mask as value
89-
*/
90-
public boolean isValidSudoku3(char[][] board) {
91-
Map<Integer, Integer> row = new HashMap<Integer, Integer>();
92-
Map<Integer, Integer> col = new HashMap<Integer, Integer>();
93-
Map<Integer, Integer> sqr = new HashMap<Integer, Integer>();
94-
95-
for (int i = 0; i < board.length; i++) {
96-
for (int j = 0; j < board[i].length; j++) {
97-
if (board[i][j] != '.') {
98-
int num = board[i][j] - '0';
99-
int rowMask = row.containsKey(i) ? row.get(i) : 0;
100-
if ((rowMask & 1 << num) > 0) {
101-
return false;
102-
} else {
103-
row.put(i, rowMask | 1 << num);
104-
}
105-
106-
int colMask = col.containsKey(j) ? col.get(j) : 0;
107-
if ((colMask & 1 << num) > 0) {
108-
return false;
109-
} else {
110-
col.put(j, colMask | 1 << num);
111-
}
112-
113-
int sqrIdx = (i - i % 3) + j / 3;
114-
int sqrMask = sqr.containsKey(sqrIdx) ? sqr.get(sqrIdx) : 0;
115-
if ((sqrMask & 1 << num) > 0) {
116-
return false;
117-
} else {
118-
sqr.put(sqrIdx, sqrMask | 1 << num);
119-
}
120-
}
121-
}
83+
return true;
84+
}
85+
86+
/**
87+
* hashtable, index as key, mask as value
88+
*/
89+
public boolean isValidSudoku3(char[][] board) {
90+
Map<Integer, Integer> row = new HashMap<>();
91+
Map<Integer, Integer> col = new HashMap<>();
92+
Map<Integer, Integer> sqr = new HashMap<>();
93+
94+
for (int i = 0; i < board.length; i++) {
95+
for (int j = 0; j < board[i].length; j++) {
96+
if (board[i][j] != '.') {
97+
int num = board[i][j] - '0';
98+
int rowMask = row.containsKey(i) ? row.get(i) : 0;
99+
if ((rowMask & 1 << num) > 0) {
100+
return false;
101+
} else {
102+
row.put(i, rowMask | 1 << num);
103+
}
104+
105+
int colMask = col.containsKey(j) ? col.get(j) : 0;
106+
if ((colMask & 1 << num) > 0) {
107+
return false;
108+
} else {
109+
col.put(j, colMask | 1 << num);
110+
}
111+
112+
int sqrIdx = (i - i % 3) + j / 3;
113+
int sqrMask = sqr.containsKey(sqrIdx) ? sqr.get(sqrIdx) : 0;
114+
if ((sqrMask & 1 << num) > 0) {
115+
return false;
116+
} else {
117+
sqr.put(sqrIdx, sqrMask | 1 << num);
118+
}
122119
}
123-
124-
return true;
120+
}
125121
}
126122

127-
private void printBoard(char[][] board) {
128-
for (int i = 0; i < board.length; i++) {
129-
for (int j = 0; j < board[i].length; j++) {
130-
System.out.print(board[i][j] + " ");
131-
}
132-
System.out.println();
133-
}
134-
System.out.println("-----------------");
123+
return true;
124+
}
125+
126+
private void printBoard(char[][] board) {
127+
for (int i = 0; i < board.length; i++) {
128+
for (int j = 0; j < board[i].length; j++) {
129+
System.out.print(board[i][j] + " ");
130+
}
131+
System.out.println();
135132
}
133+
System.out.println("-----------------");
134+
}
136135
}

0 commit comments

Comments
 (0)