随笔分类 -  leetcode

摘要:Binary Gap 思路一: 记录 bit 为 1 的位置,保留区间最大的值 public int binaryGap(int n) { int idx = -1; int result = 0; int i = 0; while (n > 0) { if ((n & 1) == 1) { if 阅读全文
posted @ 2022-10-19 21:17 iyiluo 阅读(22) 评论(0) 推荐(0)
摘要:Buddy Strings 思路一: 两个字符是 Buddy Strings 的完全分类 字符不一样 && 只存在两个位置交换的字符 字符一样 && 存在重复的字符 按照上面的分类,记录两个不同位置的下标和统计出现字符的情况 public boolean buddyStrings(String s, 阅读全文
posted @ 2022-10-19 21:16 iyiluo 阅读(18) 评论(0) 推荐(0)
摘要:Flipping an Image 思路一: 遍历数组,先对数组进行 flip 然后再对数组进行 invert public int[][] flipAndInvertImage(int[][] image) { int N = image.length; for (int i = 0; i < N 阅读全文
posted @ 2022-10-19 21:16 iyiluo 阅读(22) 评论(0) 推荐(0)
摘要:Teemo Attacking 思路一: 对两个前后攻击序列,完全分类只可能出现两种情况,只要把重叠的时间都减去,就是所求时间。此分类对三个以上的重叠时间依旧成立 没有重叠,攻击时间累加 有重叠,攻击时间需要去除重叠部分 public int findPoisonedDuration(int[] t 阅读全文
posted @ 2022-10-19 07:39 iyiluo 阅读(20) 评论(0) 推荐(0)
摘要:Hamming Distance 思路一: 求 bit 不同的位数,XOR 可以把 bit 不同的位置变成 1,再用 bitCount() 统计 public int hammingDistance(int x, int y) { return Integer.bitCount(x ^ y); } 阅读全文
posted @ 2022-10-19 07:38 iyiluo 阅读(18) 评论(0) 推荐(0)
摘要:思路一: 用数组记录 public List<Integer> findDisappearedNumbers(int[] nums) { int[] m = new int[nums.length]; for (int num : nums) { if (num-1 <= m.length) { m 阅读全文
posted @ 2022-10-19 07:38 iyiluo 阅读(26) 评论(0) 推荐(0)
摘要:Find the Difference 思路一: xor 两个字符串 public char findTheDifference(String s, String t) { char result = 0; for (int i = 0; i < s.length(); i++) { result 阅读全文
posted @ 2022-10-19 07:37 iyiluo 阅读(32) 评论(0) 推荐(0)
摘要:Invert Binary Tree 思路一:递归,交换左右。这题比较出名,个人感觉面试的题目和实际工作中遇到的问题还是不太一样的,所以一点准备都不做就跑去面试,答不上来很正常。 一般能力强的人过一遍资料就有大致的知识结构了 public TreeNode invertTree(TreeNode r 阅读全文
posted @ 2022-10-19 07:37 iyiluo 阅读(30) 评论(0) 推荐(0)
摘要:Contains Duplicate II 思路一: for 循环遍历,结果超时 public boolean containsNearbyDuplicate(int[] nums, int k) { int left = -1; for (int i = 0; i < nums.length; i 阅读全文
posted @ 2022-10-19 07:36 iyiluo 阅读(27) 评论(0) 推荐(0)
摘要:Contains Duplicate 思路一: Set 检测 public static boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for (int num : nums) { if (se 阅读全文
posted @ 2022-10-19 07:36 iyiluo 阅读(22) 评论(0) 推荐(0)
摘要:To Lower Case 思路一: 遍历,遇到 A-Z 范围内的 char 转换到对应 a-z 范围 public String toLowerCase(String s) { if (s == null || s.isEmpty()) return s; int gap = 'a' - 'A'; 阅读全文
posted @ 2022-10-19 07:36 iyiluo 阅读(20) 评论(0) 推荐(0)
摘要:Add String 思路一: 模拟加法运算,字符串前面填零 public String addStrings(String num1, String num2) { int max = Math.max(num1.length(), num2.length()); num1 = pad(num1, 阅读全文
posted @ 2022-10-19 07:35 iyiluo 阅读(34) 评论(0) 推荐(0)
摘要:Reverse String 思路一: 首尾互换 public void reverseString(char[] s) { int mid = s.length / 2; int begin = 0; int end = s.length - 1; while (begin < mid) { ch 阅读全文
posted @ 2022-10-19 07:35 iyiluo 阅读(16) 评论(0) 推荐(0)
摘要:Ugly Number 思路一: 从数字中依次去除 2,3,5,查看剩余的值是否为 1 public boolean isUgly(int n) { if (n == 0) return false; while (n % 2 == 0) { n /= 2; } while (n % 3 == 0) 阅读全文
posted @ 2022-10-19 07:34 iyiluo 阅读(27) 评论(0) 推荐(0)
摘要:Happy Number 思路一: happy number 的结果完全分类,就两种情况 最后的值为 1 进入循环(用 map 记录) public boolean isHappy(int n) { Set<Integer> set = new HashSet<>(); while (true) { 阅读全文
posted @ 2022-10-19 07:34 iyiluo 阅读(23) 评论(0) 推荐(0)
摘要:Linked List Cycle 思路一: 用 set 记录每个节点的 hashCode,如果遇到重复,说明是循环 public boolean hasCycle(ListNode head) { Set<Integer> set = new HashSet<>(); while (head != 阅读全文
posted @ 2022-10-19 07:33 iyiluo 阅读(21) 评论(0) 推荐(0)
摘要:Single Number 思路一: 用 set 过滤,剩下唯一一个就是目标数字 public int singleNumber(int[] nums) { Set<Integer> set = new HashSet<>(); for (int num : nums) { if (set.cont 阅读全文
posted @ 2022-10-19 07:33 iyiluo 阅读(18) 评论(0) 推荐(0)
摘要:Maximum Depth of Binary Tree 思路一: 递归 public int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDept 阅读全文
posted @ 2022-10-19 07:32 iyiluo 阅读(24) 评论(0) 推荐(0)
摘要:Symmetric Tree 思路一: 递归 public boolean isSymmetric(TreeNode left, TreeNode right) { if (left == null && right == null) return true; if (left == null || 阅读全文
posted @ 2022-10-19 07:32 iyiluo 阅读(16) 评论(0) 推荐(0)
摘要:Same Tree public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p != null && q == null) return false; if (p 阅读全文
posted @ 2022-10-13 18:01 iyiluo 阅读(22) 评论(0) 推荐(0)