HashTable

几种用法

  1. 给定一堆二维点 int[][] obstacles,需要判断是否存在一个点。这时,可以将二维点以 String的形式存在 Set 里,比如 obstacles[i][0] + "," + obstalces[i][1],这样将来可以将点转换成该形式进行判断。

经典题目

874 Walking Robot Simulationarrow-up-right

class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int x = 0, y = 0, angle = 0;
        Set<String> set = new HashSet<>();
        for (int[] o : obstacles) {
            set.add(o[0] + "," + o[1]);
        }
        int max = 0;
        for (int command: commands) {
            if (command == -1) {
                angle += 1;
                angle = angle == 4 ? 0 : angle;
            } else if (command == -2) {
                angle -= 1;
                angle = angle == -1 ? 3 : angle;
            } else {
                while (command-- > 0 && (!set.contains((x + dirs[angle][0]) + "," + (y + dirs[angle][1])))) {
                    x += dirs[angle][0];
                    y += dirs[angle][1];
                }
            }
            max = Math.max(max, x * x + y * y);
        }
        return max;
    }
}

711 Number of Distinct Islands IIarrow-up-right

Last updated