随笔分类 -  leetcode

上一页 1 ··· 11 12 13 14 15
摘要:Binary Tree Inorder Traversal public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); t(root, list); return lis 阅读全文
posted @ 2022-10-13 18:01 iyiluo 阅读(19) 评论(0) 推荐(0)
摘要:Merge Sorted Array 思路一: 比较两个数组前面最小值,依次插入到新数组中,最后复制新数组到 num1 中 public void merge(int[] nums1, int m, int[] nums2, int n) { int[] result = new int[nums1 阅读全文
posted @ 2022-10-13 18:01 iyiluo 阅读(24) 评论(0) 推荐(0)
摘要:Remove Duplicates from Sorted List 思路一: 双指针,左指针记录链表最后有效位置,右指针向前扫描,遇到不重复的值,加到左指针后面,双指针依次向前 public ListNode deleteDuplicates(ListNode head) { if (head = 阅读全文
posted @ 2022-10-13 18:00 iyiluo 阅读(20) 评论(0) 推荐(0)
摘要:Climbing Stairs 思路一: 动态规划,假设爬上第 n 阶楼梯,完全分类只可能存在两种情况 在 n-1 楼梯处直接一步上来 在 n-2 楼梯处直接两步上来 所以 爬上第 n 阶楼梯的方法: f(n) = f(n-1) + f(n+1) public int climbStairs(int 阅读全文
posted @ 2022-10-13 09:19 iyiluo 阅读(21) 评论(0) 推荐(0)
摘要:Sqrt(x) 思路一: 暴力 public int mySqrt(int x) { long begin = 1L; while ((begin * begin) <= x) { begin++; } return Long.valueOf(begin).intValue() - 1; } 思路二 阅读全文
posted @ 2022-10-12 18:48 iyiluo 阅读(19) 评论(0) 推荐(0)
摘要:Add Binary 思路一: 先计算公共部分,最后补充未计算的位置,模拟二进制加法,写的太丑了 public String addBinary(String a, String b) { char ONE = '0' + '1'; char TWO = '1' + '1'; StringBuild 阅读全文
posted @ 2022-10-11 22:13 iyiluo 阅读(27) 评论(0) 推荐(0)
摘要:Length of Last Word 思路一: 从后面非空格字符开始扫描,记录非空格字符个数。优化:不用 char[],直接用 charAt() 判断 public int lengthOfLastWord(String s) { char[] chars = s.toCharArray(); i 阅读全文
posted @ 2022-10-11 17:59 iyiluo 阅读(18) 评论(0) 推荐(0)
摘要:Plus One 思路一: 暴力,方向想错了,不能把 digits 当做一个整数看 public int[] plusOne(int[] digits) { if (digits[digits.length - 1] != 9) { digits[digits.length - 1]++; retu 阅读全文
posted @ 2022-10-11 17:59 iyiluo 阅读(26) 评论(0) 推荐(0)
摘要:Remove Duplicates from Sorted Array 思路一: 双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指针交换。 public int removeDuplicates(int[] nums) { i 阅读全文
posted @ 2022-10-11 17:58 iyiluo 阅读(23) 评论(0) 推荐(0)
摘要:Search Insert Position 思路一: 二分查找,然后处理没有找到的情况 public int searchInsert(int[] nums, int target) { int i = Arrays.binarySearch(nums, target); if (i >= 0) 阅读全文
posted @ 2022-10-10 18:03 iyiluo 阅读(9) 评论(0) 推荐(0)
摘要:Longest Common Prefix 思路一: 取第一个字符串为基准,依次对比剩余的字符串,取公共串 private static int commonPrefix(char[] arr1, char[] arr2) { int n = Math.min(arr1.length, arr2.l 阅读全文
posted @ 2022-10-10 18:02 iyiluo 阅读(15) 评论(0) 推荐(0)
摘要:Valid Parentheses 思路一: 用栈处理,括号匹配就出栈,如果最后栈不为空,说明输入的括号不匹配 public boolean isValid(String s) { char[] chars = s.toCharArray(); Deque<Character> stack = ne 阅读全文
posted @ 2022-10-10 18:02 iyiluo 阅读(9) 评论(0) 推荐(0)
摘要:remove element 思路一: 先排序,然后去除数字 public static int removeElement(int[] nums, int val) { Arrays.sort(nums); int INVALID = -1; int L = INVALID; for (int i 阅读全文
posted @ 2022-10-10 18:01 iyiluo 阅读(22) 评论(0) 推荐(0)
摘要:merge two sorted lists 思路一: 暴力求解,依次把两链表元素插入到新链表中 public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 == null && list2 == null) r 阅读全文
posted @ 2022-10-09 17:53 iyiluo 阅读(22) 评论(0) 推荐(0)
摘要:Roman to Integer 思路一: 暴力求解,遍历字符,遇到 6 种特殊的组合字符单独计算 static Map<Character, Integer> map = new HashMap<>(); static Map<String, Integer> combine = new Hash 阅读全文
posted @ 2022-10-09 17:15 iyiluo 阅读(19) 评论(0) 推荐(0)
摘要:回文数 思路一: 暴力求解,把数字一个一个拆分,放队列里面,最后取队列的首尾,对比是否相同 public static boolean isPalindrome2(int x) { if (x < 0) return false; if (x < 10) return true; Deque<Int 阅读全文
posted @ 2022-10-09 14:48 iyiluo 阅读(26) 评论(0) 推荐(0)
摘要:思路一:暴力求解 public int[] twoSum(int[] nums, int target) { for(int i = 0; i < nums.length; i++) { for(int j = i+1; j < nums.length; j++) { if (nums[i] + n 阅读全文
posted @ 2022-10-09 11:15 iyiluo 阅读(14) 评论(0) 推荐(0)
摘要:思路一:分别用两个 map 记录最大值和最小值,然后遍历这两个 map 求得最大距离 public int maxDistance(int[] colors) { Map<Integer, Integer> min = new HashMap<>(); Map<Integer, Integer> m 阅读全文
posted @ 2022-10-09 10:59 iyiluo 阅读(23) 评论(0) 推荐(0)

上一页 1 ··· 11 12 13 14 15