面试题 16.15. 珠玑妙算

先算猜中数,再算伪猜中数,伪猜中数=两个序列相同的字符数-猜中数
两个序列相同的字符数 = map中相同的key对应的较小的那个value

299. 猜数字游戏

跟上面一样

36. 有效的数独

  1. class Solution {
  2. public boolean isValidSudoku(char[][] board) {
  3. int[][] rows = new int[9][9];
  4. int[][] columns = new int[9][9];
  5. int[][][] subboxes = new int[3][3][9];
  6. for (int i = 0; i < 9; i++) {
  7. for (int j = 0; j < 9; j++) {
  8. char c = board[i][j];
  9. if (c != '.') {
  10. //注意这里,想用字符的数字当下标一定得减'0',减1是为了从0开始
  11. int index = c - '0' - 1;
  12. rows[i][index]++;
  13. columns[j][index]++;
  14. subboxes[i / 3][j / 3][index]++;
  15. if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
  16. return false;
  17. }
  18. }
  19. }
  20. }
  21. return true;
  22. }
  23. }

575. 分糖果

剑指 Offer 61. 扑克牌中的顺子

image.png

202. 快乐数

  1. class Solution {
  2. public boolean isHappy(int n) {
  3. Set<Integer> record = new HashSet<>();
  4. while (n != 1 && !record.contains(n)) { //这里判断思路比我更好一点
  5. record.add(n);
  6. n = getNextNumber(n);
  7. }
  8. return n == 1;
  9. }
  10. private int getNextNumber(int n) {
  11. int res = 0;
  12. while (n > 0) {
  13. int temp = n % 10;
  14. res += temp * temp;
  15. n = n / 10;
  16. }
  17. return res;
  18. }
  19. }