LeetCode
凌晨两点半还不回家
归云一去无踪迹,何处是前期。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
面试题17.14
class Solution { public int[] smallestK(int[] arr, int k) { if (k == 0){ return new int[]{}; } //建立大根堆 PriorityQueue<Integer> maxheap = new PriorityQueue<>((v1,v2)->v2-v1); ...原创 2020-07-05 09:01:44 · 154 阅读 · 0 评论 -
Offer40
class Solution { public int[] getLeastNumbers(int[] arr, int k) { //java中的堆是优先队列 if(k == 0 || arr.length == 0){ return new int[0]; } //求前k个最小值,用大根堆 Queue<Integer> pq = new PriorityQueue<&...原创 2020-07-05 09:02:15 · 120 阅读 · 0 评论 -
Offer24
/** * 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; ...原创 2020-07-05 09:02:29 · 129 阅读 · 0 评论 -
Offer11
class Solution { public int minArray(int[] numbers) { if(numbers.length == 0){ return -1; } int left = 0; int right = numbers.length - 1; while(left <= right){ int mid = left + (righ...原创 2020-07-05 09:02:41 · 119 阅读 · 0 评论 -
LeetCode1470
class Solution { public int[] shuffle(int[] nums, int n) { int[] ans = new int[nums.length]; int index = 0; for(int i = 0 ;i < n;i++){ ans[index++] = nums[i]; ans[index++] = nums[n+i]; } ...原创 2020-07-05 09:02:54 · 142 阅读 · 0 评论 -
LeetCode1439
class Solution { public int kthSmallest(int[][] mat, int k) { if (mat == null || mat.length == 0) { return -1; } int len = mat.length, sum = 0; for (int i = 0; i < len; ++i) { sum += mat[i][...原创 2020-07-05 09:03:06 · 244 阅读 · 0 评论 -
LeetCode1325
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) {...原创 2020-07-05 09:03:19 · 140 阅读 · 0 评论 -
LeetCode1218
class Solution { public int longestSubsequence(int[] arr, int difference) { // int[] dp = new int[arr.length]; // //base // Arrays.fill(dp,1); // int ans = 0; // //状态转移 // for(int i = 0;i<arr.len...原创 2020-07-05 09:03:29 · 214 阅读 · 0 评论 -
LeetCode1143
class Solution { public int longestCommonSubsequence(String text1, String text2) { int[][] dp = new int[text1.length()+1][text2.length()+1]; //base for(int i = 0;i <= text2.length();i++){ dp[0][i] = 0; ...原创 2020-07-05 09:03:40 · 113 阅读 · 0 评论 -
LeetCode1047
class Solution { public String removeDuplicates(String S) { StringBuilder sb = new StringBuilder(); int sbLength = 0; for (char character : S.toCharArray()) { if (sbLength != 0 && character == sb.charAt(sb...原创 2020-07-05 09:04:00 · 202 阅读 · 0 评论 -
LeetCode986
class Solution { public int[][] intervalIntersection(int[][] A, int[][] B) { List<int[]> ans = new ArrayList<>(); int i = 0; int j = 0; while(i < A.length && j < B.length){ int l ...原创 2020-07-06 10:15:54 · 170 阅读 · 0 评论 -
LeetCode977
class Solution { public int[] sortedSquares(int[] A) { int n = A.length; int[] res = new int[n]; int j = 0; int t = 0; while(j<n && A[j]<0){ j++; } int i=j-1; ...原创 2020-07-06 10:16:14 · 208 阅读 · 0 评论 -
LeetCode973
class Solution { public int[][] kClosest(int[][] points, int K) { //建立大根堆 PriorityQueue<int[]> maxheap = new PriorityQueue<>((v1,v2)->cal(v2)-cal(v1)); //先插入K个数组 for(int i = 0;i < K;i++){ ...原创 2020-07-06 10:16:27 · 165 阅读 · 0 评论 -
LeetCode887
// class Solution {// public int superEggDrop(int K, int N) {// /* 超出时间限制// int[][] dp = new int[K+1][N+1];// for (int i = 0; i <= K; i++) {// for(int j = 0;j<=N;j++){// dp[i][j] = 999...原创 2020-07-06 10:16:38 · 196 阅读 · 0 评论 -
LeetCode704
class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while(left <= right){ int mid = left + (right - left)/2; if(nums[mid] == target){ ...原创 2020-07-06 10:16:50 · 1500 阅读 · 0 评论 -
LeetCode674
class Solution { public int findLengthOfLCIS(int[] nums) { if(nums.length==0){ return 0; } int[] dp = new int[nums.length]; //base Arrays.fill(dp,1); int max = 1; for(int i = 1;i ...原创 2020-07-06 10:17:05 · 137 阅读 · 0 评论 -
LeetCode673
class Solution { public int findNumberOfLIS(int[] nums) { int[] dp = new int[nums.length]; int[] count = new int[nums.length]; //base Arrays.fill(dp,1); Arrays.fill(count,1); int max = 0; //状态...原创 2020-07-06 10:17:19 · 134 阅读 · 0 评论 -
LeetCode658
public class Solution { public List<Integer> findClosestElements(int[] arr, int k, int x) { int size = arr.length; int left = 0; int right = size - k -1; while (left <= right) { int mid = left ...原创 2020-07-06 10:17:32 · 192 阅读 · 0 评论 -
LeetCode637
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<Double> averageOfLevels(TreeNode root) ...原创 2020-07-06 10:17:45 · 117 阅读 · 0 评论 -
LeetCode567
class Solution { public boolean checkInclusion(String s1, String s2) { Map<Character,Integer> need = new HashMap<>(); Map<Character,Integer> window = new HashMap<>(); for(char c : s1.toCharArray()){ ...原创 2020-07-06 10:17:57 · 167 阅读 · 0 评论 -
LeetCode521
class Solution { public int findLUSlength(String a, String b) { if(a.equals(b)) return -1; return a.length() > b.length() ? a.length() : b.length(); }}原创 2020-07-07 09:07:33 · 149 阅读 · 0 评论 -
LeetCode518
class Solution { public int change(int amount, int[] coins) { int[][] dp = new int [coins.length+1][amount+1]; //base for(int i = 0;i<=coins.length;i++){ dp[i][0] = 1; } for(int i = 1;i<=am...原创 2020-07-07 09:07:53 · 106 阅读 · 0 评论 -
LeetCode516
class Solution { public int longestPalindromeSubseq(String s) { int[][] dp = new int[s.length()][s.length()]; //base for(int i = 0;i<s.length();i++){ dp[i][i] = 1; } for(int i = 1;i<s.lengt...原创 2020-07-07 09:08:07 · 147 阅读 · 0 评论 -
LeetCode494
class Solution{ public int findTargetSumWays(int[] nums, int S){ if(nums.length == 0) return 0; int sum = 0; for(int i = 0; i < nums.length; i++) sum += nums[i]; if (Math.abs(S) > Math.abs(sum)) return 0; ...原创 2020-07-07 09:08:43 · 177 阅读 · 0 评论 -
LeetCode480
class Solution { PriorityQueue<Integer> maxHeap = new PriorityQueue<>((Collections.reverseOrder())); PriorityQueue<Integer> minHeap = new PriorityQueue<>(); public double[] medianSlidingWindow(int[] nums, int k) { ...原创 2020-07-07 09:09:00 · 243 阅读 · 0 评论 -
LeetCode474
public class Solution { private int[] calcZeroAndOne(String str) { int[] res = new int[2]; for (char c : str.toCharArray()) { res[c - '0']++; } return res; } public int findMaxForm(String[] strs,...原创 2020-07-07 09:09:16 · 97 阅读 · 0 评论 -
LeetCode451
class Solution { public String frequencySort(String s) { //使用字典统计每个元素的个数 HashMap<Character,Integer> map = new HashMap<>(); for(char chr : s.toCharArray() ){ if(map.containsKey(chr)){ ma...原创 2020-07-07 09:09:34 · 180 阅读 · 0 评论 -
LeetCode438
class Solution { public List<Integer> findAnagrams(String s, String p) { List<Integer> res = new ArrayList<>(); Map<Character,Integer> need = new HashMap<>(); Map<Character,Integer> window ...原创 2020-07-07 09:13:52 · 206 阅读 · 0 评论 -
LeetCode429
/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val;...原创 2020-07-07 09:14:06 · 128 阅读 · 0 评论 -
LeetCode416
class Solution { public boolean canPartition(int[] nums) { int sum = 0; for(int i = 0;i < nums.length;i++){ sum += nums[i]; } if(sum % 2 != 0) return false; sum = sum / 2; //定义d...原创 2020-07-12 13:14:15 · 100 阅读 · 0 评论 -
LeetCode347
class Solution { public int[] topKFrequent(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for(int num : nums){ if(map.containsKey(num)){ map.put(num,map.get(num)+1); ...原创 2020-07-12 13:14:31 · 107 阅读 · 0 评论 -
LeetCode337
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int rob(TreeNode root) { //计算当前节点偷与不偷所能获...原创 2020-07-12 13:14:44 · 161 阅读 · 0 评论 -
LeetCode322
public class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0; for (int i = 1; i <= amount; i++) { for (int coin :coins) { ...原创 2020-07-12 13:14:55 · 160 阅读 · 0 评论 -
LeetCode300
class Solution { public int lengthOfLIS(int[] nums) { int[] dp = new int[nums.length]; //base 全部初始化为1 Arrays.fill(dp,1); //状态转移 for(int i = 0;i < nums.length;i++){ for(int j = 0;j < i;j++)...原创 2020-07-12 13:15:08 · 105 阅读 · 0 评论 -
LeetCode295
class MedianFinder { PriorityQueue<Integer> maxHeap; PriorityQueue<Integer> minHeap; /** initialize your data structure here. */ public MedianFinder() { maxHeap = new PriorityQueue<>((a,b) -> b - a); ...原创 2020-07-12 13:15:21 · 179 阅读 · 0 评论 -
LeetCode279
class Solution { public int numSquares(int n) { int[] dp = new int[n + 1]; // 默认初始化值都为0 for (int i = 1; i <= n; i++) { dp[i] = i; // 最坏的情况就是每次+1 for (int j = 1; i - j * j >= 0; j++) { dp...原创 2020-07-12 13:15:33 · 89 阅读 · 0 评论 -
LeetCode268
class Solution { public int missingNumber(int[] nums) { Arrays.sort(nums); int n = nums.length; if(nums[n-1] != n){ return n; } if(nums[0]!=0){ return 0; } for(int i = 1...原创 2020-07-12 13:15:44 · 87 阅读 · 0 评论 -
LeetCode257
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<String> binaryTreePaths(TreeNode root) ...原创 2020-07-12 13:15:56 · 129 阅读 · 0 评论 -
LeetCode215数组中的第K个最大元素
class Solution { public int findKthLargest(int[] nums, int k) { if(nums.length == 0){ return -1; } //建立小根堆 PriorityQueue<Integer> minheap = new PriorityQueue<>((V1,V2)->V1-V2);/** ...原创 2020-07-12 13:16:09 · 134 阅读 · 0 评论 -
LeetCode213:打家劫舍||
class Solution { public int rob(int[] nums) { if(nums.length == 0) return 0; if(nums.length == 1) return nums[0]; int[] nums1 = Arrays.copyOfRange(nums, 1, nums.length); int[] nums2 = Arrays.copyOfRange(nums, 0, nums....原创 2020-07-13 20:01:43 · 206 阅读 · 0 评论
分享