DeBug Technique for Algorithm Problems
First things first, make sure you fully understand the problem! instead of looking somewhat similar or read and stop thinking about: oh, I’ve met this problem before, so I don’t have to continue read and starting coding right away. DON’T EVEN THINK ABOUT IT!
when we read the problem, what should we extract: the input/output, procedure instruction(in steps) to convert input to output.
Some bugs are often encountered:
Can’t compile:
- out of index range:
compile correctly but wrong answer in all:
- equals and ==
- the result stays the same as we initialize it.(which means our core function does not work at all)
- many method only returns void or other class instead of things we really want: like map.get(s.charAt(i), list.add(i)) is not right, s.charAt(i) returns char, so this part is right. However, list.add(i) did not return the updated list as we expected.
- check the minor mistake before checking logic.
- when checking logic, don’t just iterate the process using the same idea. write down the input and output of each function. get a clear meaning of each variable.
- after checking the logic without mistake been found. try to think in a whole new way, do not waste too much time on current not working way.
compile correctly, some case can pass, but not all
- TLE or Stack overflow, in this case, improve the code using memo, or just using a smarter algorithm

浙公网安备 33010602011771号