Skip to main content
List item continuation paragraphs
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Great job, it’s clean and straightforward. Two quick tweaks will make it more resilient and streamlined.

  1. Replace repeated sums with a simple bitmask check. I didn’t see anyone mention bitmasking yet. Instead of summing and relying on magic numbers, use a bitmask per row/column/box:
    int bit = 1 << value;
    if ((mask[r] & bit) != 0) return false;
    mask[r] |= bit;
    
    You catch duplicates directly — it avoids false positives like “all 5’s” which add up to 45 but don’t make a valid set. This approach gives clear intent, simpler logic, and it runs in a fixed number of checks.
int bit = 1 << value;
if ((mask[r] & bit) != 0) return false;
mask[r] |= bit;

You catch duplicates directly — it avoids false positives like “all 5’s” which add up to 45 but don’t make a valid set. This approach gives clear intent, simpler logic, and it runs in a fixed number of checks.

  1. Move from static shared state to local variables. There are multiple static fields (rSumrSum, cSumcSum, arraysarrays) that clutter mainmain and act as global state. Instead, use method-local variables and return results or throw when invalid. That keeps state contained, makes your code modular, and avoids unintended side-effects.

Great job, it’s clean and straightforward. Two quick tweaks will make it more resilient and streamlined.

  1. Replace repeated sums with a simple bitmask check I didn’t see anyone mention bitmasking yet. Instead of summing and relying on magic numbers, use a bitmask per row/column/box:
int bit = 1 << value;
if ((mask[r] & bit) != 0) return false;
mask[r] |= bit;

You catch duplicates directly — it avoids false positives like “all 5’s” which add up to 45 but don’t make a valid set. This approach gives clear intent, simpler logic, and it runs in a fixed number of checks.

  1. Move from static shared state to local variables There are multiple static fields (rSum, cSum, arrays) that clutter main and act as global state. Instead, use method-local variables and return results or throw when invalid. That keeps state contained, makes your code modular, and avoids unintended side-effects.

Great job, it’s clean and straightforward. Two quick tweaks will make it more resilient and streamlined.

  1. Replace repeated sums with a simple bitmask check. I didn’t see anyone mention bitmasking yet. Instead of summing and relying on magic numbers, use a bitmask per row/column/box:
    int bit = 1 << value;
    if ((mask[r] & bit) != 0) return false;
    mask[r] |= bit;
    
    You catch duplicates directly — it avoids false positives like “all 5’s” which add up to 45 but don’t make a valid set. This approach gives clear intent, simpler logic, and it runs in a fixed number of checks.
  1. Move from static shared state to local variables. There are multiple static fields (rSum, cSum, arrays) that clutter main and act as global state. Instead, use method-local variables and return results or throw when invalid. That keeps state contained, makes your code modular, and avoids unintended side-effects.
Source Link

Great job, it’s clean and straightforward. Two quick tweaks will make it more resilient and streamlined.

  1. Replace repeated sums with a simple bitmask check I didn’t see anyone mention bitmasking yet. Instead of summing and relying on magic numbers, use a bitmask per row/column/box:
int bit = 1 << value;
if ((mask[r] & bit) != 0) return false;
mask[r] |= bit;

You catch duplicates directly — it avoids false positives like “all 5’s” which add up to 45 but don’t make a valid set. This approach gives clear intent, simpler logic, and it runs in a fixed number of checks.

  1. Move from static shared state to local variables There are multiple static fields (rSum, cSum, arrays) that clutter main and act as global state. Instead, use method-local variables and return results or throw when invalid. That keeps state contained, makes your code modular, and avoids unintended side-effects.