BF (my solution)
1 | class Solution { |
Remarks:
- TC: $O(81)=O(1)$, SC: $O(1)$
- Be careful about converting from
chattoint:num - '1'(actually should be0be 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?000rows[i] |= maskmeansrows[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 byte1 byte 8 –128 to 127 0short2 bytes 16 –32,768 to 32,767 ($-2^{15}$ to $2^{15} - 1$) 0int4 bytes 32 –2,147,483,648 to 2,147,483,647 ($-2^{31}$ to $2^{31} - 1$) 0long8 bytes 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ($-2^{63}$ to $2^{63} - 1$) 0Lfloat4 bytes 32 Approx. $±3.40282347×10^{38}$ (7 decimal digits precision) 0.0fdouble8 bytes 64 Approx. $±1.79769313×10^{308}$ (15 decimal digits precision) 0.0dchar2 bytes 16 0 to 65,535 (Unicode characters) '\u0000'booleanJVM-dependent N/A trueorfalse(internally often 1 byte or more)false