Offer.P32-I[从上到下打印二叉树]
Offer.P32-I[从上到下打印二叉树]
方法一:层序遍历123456789101112131415161718192021222324252627/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int[] levelOrder(TreeNode root) { List<Integer> list = new ArrayList<>(); Deque<TreeNode> queue = new ArrayDeque<>(); if (root != null) queue.addLast(root); ...
LC.P2611[老鼠和奶酪]
LC.P2611[老鼠和奶酪]
方法一:贪心+排序1234567891011121314class Solution { public int miceAndCheese(int[] reward1, int[] reward2, int k) { int ans = 0, n = reward1.length; for (int i = 0; i < n; ++i) { ans += reward2[i]; reward1[i] -= reward2[i]; } Arrays.sort(reward1); for (int i = 0; i < k; ++i) { ans += reward1[n - i - 1]; } return ans; }}
时间复杂度:$O(nlogn)$
空间复杂度:$O(n)$
Offer.P35[复杂链表的复制]
Offer.P35[复杂链表的复制]
方法一:哈希表1234567891011121314151617181920212223242526272829303132/*// Definition for a Node.class Node { int val; Node next; Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; }}*/class Solution { public Node copyRandomList(Node head) { if (head == null) return null; Node p = head; Map<Node, Node> map = new HashMap<>(); while (p != nu ...
Offer.P24[反转链表]
Offer.P24[反转链表]
方法一:迭代(双指针)1234567891011121314151617181920/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null, cur = head; while (cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } ...
Offer.P22[链表中倒数第k个节点]
Offer.P22[链表中倒数第k个节点]
方法一:模拟1234567891011121314151617181920212223/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode getKthFromEnd(ListNode head, int k) { ListNode p = head; int n = 0; while (p != null) { ++n; p = p.next; } p = head; for (int i = 1; i <= n - k; ++i ...
LC.P2352[相等行列对]
LC.P2352[相等行列对]
方法一:暴力123456789101112131415161718class Solution { public int equalPairs(int[][] grid) { int n = grid.length, ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (isEqual(i, j, n, grid)) ++ans; } } return ans; } private boolean isEqual(int row, int col, int n, int[][] grid) { for (int i = 0; i < n; ++i) { if (grid[row ...
LC.P472[连接词]
LC.P472[连接词]
方法一:Trie+记忆化搜索123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657class Solution { Trie trie; public List<String> findAllConcatenatedWordsInADict(String[] words) { trie = new Trie(); List<String> ans = new ArrayList<>(); Arrays.sort(words, (a, b) -> a.length() - b.length()); for (String word : words) { int n = word.length(); if (n == 0) con ...
LC.P2460[对数组执行操作]
LC.P2460[对数组执行操作]
方法一:模拟123456789101112131415161718192021class Solution { public int[] applyOperations(int[] nums) { int n = nums.length; for (int i = 0; i < n - 1; ++i) { if (nums[i] == nums[i + 1]) { nums[i] *= 2; nums[i + 1] = 0; } } int k = 0; for (int i = 0; i < n; ++i) { if (nums[i] != 0) { nums[k++] = nums[i]; } ...
LC.P583[两个字符串的删除操作]
LC.P583[两个字符串的删除操作]
方法一:转化为最长公共子序列求将两个字符串删除任意字符后能相同的最短步数,可以等价于求两个字符串的最长公共子序列LCS,最后使得两字符串删除任意字符后能相同的最短步数$= n - lcs + m - lcs$
123456789101112131415161718class Solution { public int minDistance(String word1, String word2) { int n = word1.length(), m = word2.length(); int[][] f = new int[n + 1][m + 1]; // 求最长公共子序列 (LC.P1143) for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (word1.charAt(i - 1) == ...
LC.P2465[不同的平均值数目]
LC.P2465[不同的平均值数目]
方法一:排序+哈希表1234567891011class Solution { public int distinctAverages(int[] nums) { Arrays.sort(nums); int n = nums.length; Set<Integer> set = new HashSet<>(); for (int i = 0, j = n - 1; i < j; ++i, --j) { set.add(nums[i] + nums[j]); } return set.size(); }}
时间复杂度:$O(nlogn)$
空间复杂度:$O(n)$