acmug
acmug
Go to bed early, get up early, knock code
展开
-
leetcode417
题目考查逆向思维,水可以向低处流,但是遍历需要重复遍历各个节点,换个角度,水可以向高处流,可以采用DFS或者BFS一次遍历即可暴力水往低处流(剪枝优化才能AC) class Solution { static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int[][] heights; int m, n; boolean[][] pacific; boolean[]原创 2022-04-28 09:56:25 · 251 阅读 · 0 评论 -
leetcode883
题目数组在遍历的时候,grid[i][j]为横坐标,grid[j][i]就是纵坐标class Solution { public int projectionArea(int[][] grid) { int n = grid.length; int xyArea = 0, yzArea = 0, zxArea = 0; for (int i = 0; i < n; i++) { int yzHeight = 0, zx原创 2022-04-26 22:04:35 · 120 阅读 · 0 评论 -
leetcode398
考查数学概率题目方法一:class Solution { private Map<Integer, List<Integer>> records = new HashMap<>(); private Random random; public Solution(int[] nums) { for (int i = 0; i < nums.length; i++) {原创 2022-04-25 16:49:51 · 68 阅读 · 0 评论 -
leetcode868
题目考查二进制&运算,因为&运算,能够从最低位判断该位等于1还是0public int binaryGap(int n) { int ans = 0; String str = toBinary(n); int left = -1; int index = 0; while (index < str.length()) { if (str.charAt(index) == '1'原创 2022-04-24 17:45:56 · 381 阅读 · 0 评论 -
leetcode868
题目考查二进制&运算,因为&运算,能够从最低位判断该位等于1还是0public int binaryGap(int n) { int ans = 0; String str = toBinary(n); int left = -1; int index = 0; while (index < str.length()) { if (str.charAt(index) == '1'原创 2022-04-24 11:18:45 · 211 阅读 · 0 评论 -
leetcode396
题目暴力Time Out//两次遍历会Time Out public static int maxRotateFunction(int[] nums) { //模拟循环链表 int length = nums.length; int max = Integer.MIN_VALUE; for (int i = length; i > 0; i--) { int index = i;原创 2022-04-22 22:41:48 · 275 阅读 · 0 评论 -
leetcode824
题目public String toGoatLatin(String sentence) { Set<Character> chars = new HashSet<Character>() {{ add('a'); add('e'); add('i'); add('o'); add('u'); add('A');原创 2022-04-21 11:45:17 · 65 阅读 · 0 评论 -
leetcode388
codeTop题目考查如何保存上一次的结果方法一:递归,方法二:数组,或者栈//我刚开始的思想是直接对其进行'\t'分割,递归之后对于'\t\t'分割, // 但是发现,第一次递归对于'\t'分割,已经不能行的通了。 //既然递归行不通,可以借助栈,采取手动解析的方式。 //如何判断下一个节点是栈顶元素的下一个目录节点?采用深度来判断。每个'\t'代表一个深度。 //其实这道题就是考查,如何记录已经遍历过的节点,方法一:递归,方法二:数组,或者栈 public原创 2022-04-20 18:23:26 · 910 阅读 · 0 评论 -
leetcode821
题目链接我的思路//将s中==c的记录下来到数组arr中,然后遍历s,对于arr进行二分查找,找到第一个小于该下标的位置, public int[] shortestToChar(String s, char c) { int length = s.length(); int[] ans = new int[length]; List<Integer> res = new ArrayList<>(); for原创 2022-04-20 09:29:50 · 211 阅读 · 0 评论 -
leetcode206反转链表
codeTop题目链接//方法一: public ListNode reverseList(ListNode head) { ListNode pre = null; while (head != null) { ListNode nex = head.next; head.next = pre; pre = head; head = nex; }原创 2022-04-20 09:27:31 · 342 阅读 · 0 评论 -
leetcode215数组中的第K个最大元素
codeTop 微软刷题题目链接这道题考查最基本的排序功能,看能不能写出来快速排序private final Random random = new Random(); public int findKthLargest(int[] nums, int k) { return randomPartition(nums, 0, nums.length - 1, nums.length - k); } //平均情况下快速排序的时间复杂度是Θ(𝑛\lgn),最坏原创 2022-04-19 14:18:50 · 81 阅读 · 0 评论 -
leetcode396
从今天开始,每天至少一道题题目链接leetcode386标准的递归回溯public List<Integer> lexicalOrder(int n) { List<Integer> ans = new ArrayList<>(); for (int i = 1; i < 10; i++) { dfs(ans, String.valueOf(i), n); } retur原创 2022-04-18 21:38:35 · 271 阅读 · 0 评论