摘要: 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 阅读(16) 评论(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 阅读(13) 评论(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 阅读(26) 评论(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 阅读(6) 评论(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 阅读(13) 评论(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 阅读(17) 评论(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 阅读(15) 评论(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 阅读(11) 评论(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 阅读(17) 评论(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 阅读(11) 评论(0) 推荐(0)