Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Runtime: 1 ms, faster than 98.97% of Java online submissions for Queens That Can Attack the King.
- Memory Usage: 36.3 MB, less than 100.00% of Java online submissions for Queens That Can Attack the King.
- */
- class Solution {
- int[][] dirs = new int[][] {
- {1,1},
- {0,1},
- {1,0},
- {1,-1},
- {0,-1},
- {-1,1},
- {-1,0},
- {-1,-1}
- };
- public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
- Set<Short> qSet = new HashSet<>();
- List<List<Integer>> result = new LinkedList<>();
- for (int[] q : queens) {
- qSet.add((short)(q[0] << 4 | q[1]));
- }
- for (int[] dir : dirs) {
- int i = king[0];
- int j = king[1];
- while (i >= 0 && j >= 0 &&
- i < 8 && j < 8) {
- if (qSet.contains((short)(i << 4 | j))) {
- result.add(Arrays.asList(i, j));
- break;
- }
- i += dir[0];
- j += dir[1];
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement