LCR.P67[数组中两个数的最大异或值]
LCR.P67[数组中两个数的最大异或值]
方法一:贪心+Trie12345678910111213141516171819202122232425262728293031323334353637383940414243class Solution { public int findMaximumXOR(int[] nums) { int ans = 0; Trie trie = new Trie(); for (int num : nums) { trie.add(num); int x = trie.getVal(num); ans = Math.max(ans, num ^ x); } return ans; } private static class Trie { Trie[] node = new Trie[2]; public voi ...
LC.P1268[搜索推荐系统]
LC.P1268[搜索推荐系统]
方法一:排序+Trie123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960class Solution { public List<List<String>> suggestedProducts(String[] products, String searchWord) { Trie trie = new Trie(); Arrays.sort(products); for (int i = 0; i < products.length; ++i) trie.insert(products[i], i); List<List<String>> ans = new ArrayList<>(); StringBuilder pre ...
LC.P344[反转字符串]
LC.P344[反转字符串]
方法一:双指针123456789class Solution { public void reverseString(char[] s) { for (int left = 0, right = s.length - 1; left < right; ++left, --right) { char temp = s[left]; s[left] = s[right]; s[right] = temp; } }}
时间复杂度:$O(n)$
空间复杂度:$O(1)$
LC.P24[两两交换链表中的节点]
LC.P24[两两交换链表中的节点]
方法一:递归12345678910111213141516171819/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) return head; ListNode newHead ...
LC.P21[合并两个有序链表]
LC.P21[合并两个有序链表]
方法一:迭代123456789101112131415161718192021222324252627/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode ans = new ListNode(), p = ans; ...
LC.P448[找到所有数组中消失的数字]
LC.P448[找到所有数组中消失的数字]
方法一:原地修改1234567891011121314class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { int n = nums.length; List<Integer> ans = new ArrayList<>(); for (int num : nums) { int index = (num - 1) % n; nums[index] += n; } for (int i = 0; i < n; ++i) { if (nums[i] <= n) ans.add(i + 1); } return ans; }}
时间复杂度:$O(n) ...
Offer.P3[数组中重复的数字]
Offer.P3[数组中重复的数字]
方法一:原地哈希12345678910111213141516class Solution { public int findRepeatNumber(int[] nums) { int i = 0; while (i < nums.length) { if (nums[i] == i) { ++i; continue; } if (nums[i] == nums[nums[i]]) return nums[i]; int temp = nums[i]; nums[i] = nums[temp]; nums[temp] = temp; } return -1; }}
时间复杂度:$O(n)$
空间复杂度: ...
LC.P287[寻找重复数]
LC.P287[寻找重复数]
方法一:快慢指针123456789101112131415class Solution { public int findDuplicate(int[] nums) { int slow = nums[0], fast = nums[nums[0]]; while (slow != fast) { slow = nums[slow]; fast = nums[nums[fast]]; } slow = 0; while (slow != fast) { slow = nums[slow]; fast = nums[fast]; } return slow; }}
时间复杂度:$O(n)$
空间复杂度:$O(1)$
LC.P980[不同路径III]
LC.P980[不同路径III]
方法一:回溯123456789101112131415161718192021222324252627282930313233343536373839404142class Solution { int[][] grid; int m, n, cnt; boolean[][] visited; static int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int uniquePathsIII(int[][] grid) { this.grid = grid; m = grid.length; n = grid[0].length; visited = new boolean[m][n]; int x = 0, y = 0; f ...
LCR.P5[最大单词长度乘积]
LCR.P5[最大单词长度乘积]
方法一:暴力12345678910111213141516171819202122232425262728class Solution { public int maxProduct(String[] words) { Set<Character> set = new HashSet<>(); int ans = 0, n = words.length; for (int i = 0; i < n - 1; ++i) { String word1 = words[i]; for (int k = 0; k < word1.length(); ++k) { set.add(word1.charAt(k)); } for (int j = i + 1; j < n; ++j) { ...