BF (my solution)
1 | class Solution { |
Remarks:
- TC: $O(81)=O(1)$, SC: $O(1)$
- Be careful about converting from
chat
toint
:num - '1'
(actually should be0
be we want to use index from0
)
Bit Manipulation
1 | class Solution { |
Remarks:
Use bit to record and check seen.
val = 3
->mask = 000001000
->rows[i] & mask = 00000?000
rows[i] |= mask
meansrows[i] = rows[i] || mask
. which helps to record seen.more powerful for bigger sudokus (e.g. 16*16):
boolean[16]
takes 16 bytes, butint (32bit)
only takes 4 bytes.Data Type Size (Bytes) Bits Value Range (Decimal) Default Value byte
1 byte 8 –128 to 127 0
short
2 bytes 16 –32,768 to 32,767 ($-2^{15}$ to $2^{15} - 1$) 0
int
4 bytes 32 –2,147,483,648 to 2,147,483,647 ($-2^{31}$ to $2^{31} - 1$) 0
long
8 bytes 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ($-2^{63}$ to $2^{63} - 1$) 0L
float
4 bytes 32 Approx. $±3.40282347×10^{38}$ (7 decimal digits precision) 0.0f
double
8 bytes 64 Approx. $±1.79769313×10^{308}$ (15 decimal digits precision) 0.0d
char
2 bytes 16 0 to 65,535 (Unicode characters) '\u0000'
boolean
JVM-dependent N/A true
orfalse
(internally often 1 byte or more)false