先来看看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; } }