leetcode-1207. 独一无二的出现次数
leetcode-1207. 独一无二的出现次数
123456789101112131415161718192021222324class Solution { public boolean uniqueOccurrences(int[] arr) { Arrays.sort(arr); int n = arr.length; boolean[] st = new boolean[n]; int count = 1; for(int i = 1; i < n; i++){ if(arr[i] == arr[i-1]){ count++; }else{ if(st[count] == true){ return false; } ...
leetcode-144. 二叉树的前序遍历
leetcode-144. 二叉树的前序遍历
1234567891011121314151617181920class Solution { List<Integer> ans = new ArrayList<>(); public List<Integer> preorderTraversal(TreeNode root) { dfs(root); return ans; } public void dfs(TreeNode root){ if(root == null){ return; } ans.add(root.val); if(root.left != null){ dfs(root.left); } if(root.right != null){ ...
leetcode-1365. 有多少小于当前数字的数字
leetcode-1365. 有多少小于当前数字的数字
12345678910111213141516171819202122232425262728class Solution { public int[] smallerNumbersThanCurrent(int[] nums) { int n = nums.length; int[] ans = new int[n]; Map<Integer, Integer> map = new HashMap<>(); int tmp = 0; int[] nums2= Arrays.copyOf(nums, n); Arrays.sort(nums2); for (int i = 0; i < n; i++) { if (i == 0) { map.put(nums2[i], i); ...
leetcode-1024. 视频拼接
leetcode-1024. 视频拼接
双指针
123456789101112131415161718192021222324252627282930313233class Solution { public int videoStitching(int[][] clips, int T) { int count = 0; int l = 0; int r = 0; while (r < T) { boolean bool = false; int l2 = l; int r2 = r; for (int[] clip : clips) { int a = clip[0]; int b = clip[1]; if (a <= r2 && a >= l2 && ...
leetcode-1130. 叶值的最小代价生成树
leetcode-1130. 叶值的最小代价生成树
区间dp
1234567891011121314151617181920212223242526272829class Solution { public int mctFromLeafValues(int[] arr) { int n = arr.length; int[][] dp = new int[n + 1][n + 1]; int[][] max = new int[n + 1][n + 1]; for (int i = 1; i <= n; i++) { int maxValue = arr[i - 1]; for (int j = i; j <= n; j++) { maxValue = Math.max(maxValue, arr[j - 1]); max[i][j] = maxValue; ...
leetcode-234. 回文链表
leetcode-234. 回文链表
使用双端队列
12345678910111213141516171819202122class Solution { public boolean isPalindrome(ListNode head) { LinkedList<ListNode> queue = new LinkedList<ListNode>(); ListNode tmp = head; while (tmp != null) { queue.offer(tmp); tmp = tmp.next; } while (!queue.isEmpty()){ ListNode frist = queue.pollFirst(); ListNode last = queue.pollLast(); if (last == nu ...
leetcode-LCP 07. 传递信息
leetcode-LCP 07. 传递信息
1234567891011121314151617181920212223242526class Solution { int count = 0; public int numWays(int n, int[][] relation, int k) { dfs(0, n, relation, k); return count; } public void dfs(int cur, int n, int[][] relation, int k) { if (k == 0 && cur == n - 1) { count++; return; } if (k < 0) { return; } for (int[] arr : relation) { ...
leetcode-LCP 06. 拿硬币
leetcode-LCP 06. 拿硬币
1234567891011class Solution { public int minCount(int[] coins) { int ans = 0; for (int n : coins) { int i = n / 2; int j = n % 2; ans = ans + i + j; } return ans; }}
leetcode-763. 划分字母区间
leetcode-763. 划分字母区间
12345678910111213141516171819202122232425262728class Solution { public List<Integer> partitionLabels(String S) { List<Integer> result = new ArrayList<>(); int n = S.length(); int pre = 0; Set<Character> set = new HashSet<>(); for (int i = 0; i < n; i++) { set.add(S.charAt(i)); boolean b = judge(set, S.substring(i + 1)); if (!b) { result. ...
leetcode-474. 一和零
leetcode-474. 一和零
原始思路
123456789101112131415161718192021222324252627282930313233343536class Solution { public int findMaxForm(String[] strs, int m, int n) { int n1 = strs.length; int[][][] dp = new int[n1+1][m+1][n+1]; for (int i = 1; i <= strs.length; i++) { int[] arr = getZeorAndOne(strs[i-1]); int zero = arr[0]; int one = arr[1]; for (int j = 0; j <= m; j++) { for (int k = 0; k <= ...