leetcode 36 有效數獨 用到的Map.getOrDefault()方法 以及縮小計算時間牛x方法


先來看看Map.getOrDefault()方法:

default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }

這是源碼,意思就是當Map集合中有這個key時,就使用這個key值,如果沒有就使用默認值defaultValue

下面就具體的例子,再說明一下:

public class Demo13 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("name", "lxj");
        map.put("age", "24");
        map.put("sex", "女");
        String name = map.getOrDefault("name", "test");
        System.out.println(name);// lxj,map中存在name,獲得name對應的value
        String address = map.getOrDefault("address", "北京");
        System.out.println(address);// 北京,map中不存在address,使用默認值“北京”
    }
}
轉載自:https://blog.csdn.net/lxj_1993/article/details/79798963

————————————————

在leetcode36題:有效的數獨中使用到了map.getOrDefault()

官方答案:

class Solution {
  public boolean isValidSudoku(char[][] board) {
    // init data
    HashMap<Integer, Integer> [] rows = new HashMap[9];
    HashMap<Integer, Integer> [] columns = new HashMap[9];
    HashMap<Integer, Integer> [] boxes = new HashMap[9];
    for (int i = 0; i < 9; i++) {
      rows[i] = new HashMap<Integer, Integer>();
      columns[i] = new HashMap<Integer, Integer>();
      boxes[i] = new HashMap<Integer, Integer>();
    }

    // validate a board
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        char num = board[i][j];
        if (num != '.') {
          int n = (int)num;
          int box_index = (i / 3 ) * 3 + j / 3;

          // keep the current cell value
          rows[i].put(n, rows[i].getOrDefault(n, 0) + 1);
          columns[j].put(n, columns[j].getOrDefault(n, 0) + 1);
          boxes[box_index].put(n, boxes[box_index].getOrDefault(n, 0) + 1);

          // check if this value has been already seen before
          if (rows[i].get(n) > 1 || columns[j].get(n) > 1 || boxes[box_index].get(n) > 1)
            return false;
        }
      }
    }

    return true;
  }
}

作者:LeetCode
鏈接:https://leetcode-cn.com/problems/valid-sudoku/solution/you-xiao-de-shu-du-by-leetcode/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

哇評論用的高分方法太變態了

public boolean isValidSudoku(char[][] board) {
    int[] rowCnt = new int[9];
    int[] colCnt = new int[9];
    int[] boxCnt = new int[9];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if ('.' == board[i][j]) {
                continue;
            }
            int num = board[i][j] - 48;
            // 處理行
            if ((rowCnt[i] >> num) % 2 == 1) {
                return false;
            } else {
                rowCnt[i] += 1 << num;
            }
            // 處理列
            if ((colCnt[j] >> num) % 2 == 1) {
                return false;
            } else {
                colCnt[j] += 1 << num;
            }
            // 處理框
            int boxNum = i / 3 * 3 + j / 3;
            if ((boxCnt[boxNum] >> num) % 2 == 1) {
                return false;
            } else {
                boxCnt[boxNum] += 1 << num;
            }
        }
    }
    return true;
}

作者:zhaomin6666
鏈接:https://leetcode-cn.com/problems/valid-sudoku/solution/36you-xiao-de-shu-du-ti-jie-java-3ms-by-zhaomin666/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

最高分給的這個:偶像!!!!這個思想太強了 我太菜了

class Solution {
    public boolean isValidSudoku(char[][] board) {
        short[] rows = new short[9];
        short[] cols = new short[9];
        short[] boxes = new short[9];
        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                char num = board[row][col];
                if (num == '.') continue;
                num = (char) (1 << (num - '1'));

                if ((rows[row] & num) != 0) return false;
                if ((cols[col] & num) != 0) return false;
                int boxIndex = (row / 3) * 3 + col / 3;
                if ((boxes[boxIndex] & num) != 0) return false;

//                rows[row] += num;
//                cols[col] += num;
//                boxes[boxIndex] += num;
                rows[row] |= num;
                cols[col] |= num;
                boxes[boxIndex] |= num;
            }
        }
        return true;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM