-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0709
171 lines (155 loc) · 4.85 KB
/
AlgoPlan0709
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#rotate the stones
#key idea:
-consider stones moving right rather than moving down
-look for consecutive empty spots until we hit a non-empty spot.
class Solution {
public char[][] rotateTheBox(char[][] box) {
int m = box.length;
int n = box[0].length;
System.out.println(m);
System.out.println(n);
char[][] newbox =new char[n][m];
int empty = n-1;
for( int row = 0; row < m;row++){
for (int col = n-1;col>=0;col--){
if (box[row][col]=='#'){
empty= col+1;
//keeps moving right to find the rightest empty spot
while(empty <n && box[row][empty] =='.'){
empty++;
}
//hit a non-empty stop;
if(empty-1 < n && box[row][empty-1] =='.'){
box[row][empty-1] = '#';
box[row][col] = '.';
}
}
}
}
for(int i = 0; i < m;i++){
for (int j =0; j < n;j++){
newbox[j][m-1-i] = box[i][j];
}
}
return newbox;
}
}
#valid sudoku
#key idea:
-go through the board according to the three rules.
class Solution {
public boolean isValidSudoku(char[][] board) {
boolean[] checked = new boolean[10];
char temp ='.';
//go through rows
for (int i = 0;i < 9;i++){
for (int k = 0; k< 9;k++){
if(board[i][k] !='.' ){
temp = board[i][k];
if (!checked[(temp-'0')-1]){
checked[(temp-'0')-1] = true;
}else {
System.out.println(i + " "+ k);
return false;
}
}
}
checked = new boolean[10];
}
checked = new boolean[10];
//go through columns
for (int i = 0; i < 9; i++) {
for (int k = 0; k < 9; k++) {
if (board[k][i] != '.') {
temp = board[k][i];
if (!checked[(temp - '0')]) {
checked[(temp - '0')] = true;
} else {
System.out.println(i + " "+ k);
return false;
}
}
}
checked = new boolean[10];
}
checked = new boolean[10];
int box_int = 0;
//go through boxes
while (box_int<9){
checked = new boolean[10];
System.out.println(box_int);
for (int k = 0; k < 3;k++){
for (int m = 0;m < 3;m++){
if (board[box_int+k][m]!='.'){
temp = board[box_int+k][m] ;
if (!checked[(temp-'0')]){
checked[(temp-'0')] = true;
}else {
System.out.println(k + " "+ m);
return false;
}
}
}
}
checked = new boolean[10];
for (int k = 0; k < 3;k++){
for (int m = 3;m < 6;m++){
if (board[box_int+k][m]!='.'){
temp = board[box_int+k][m] ;
if (!checked[(temp-'0')]){
checked[(temp-'0')] = true;
}else {
System.out.println(k + " "+ m);
return false;
}
}
}
}
checked = new boolean[10];
for (int k = 0; k < 3;k++){
for (int m = 6;m < 9;m++){
if (board[box_int+k][m]!='.'){
temp = board[box_int+k][m] ;
if (!checked[(temp-'0')]){
checked[(temp-'0')] = true;
}else {
System.out.println(k + " "+ m);
return false;
}
}
}
}
box_int = box_int+3;
}
return true;
}
}
#contain all integers from 1 to n each row and column
class Solution {
public boolean checkValid(int[][] matrix) {
int n = matrix.length;
//check rows
boolean[] checked = new boolean[n+1];
for (int i = 0; i < n;i++){
for(int j = 0; j < n;j++){
if (!checked[matrix[i][j]]){
checked[matrix[i][j]] = true;
}else{
return false;
}
}
checked = new boolean[n+1];
}
for (int i = 0; i < n;i++){
for(int j = 0; j < n;j++){
if (!checked[matrix[j][i]]){
checked[matrix[j][i]] = true;
}else{
return false;
}
}
checked = new boolean[n+1];
}
return true;
}
}