自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Belle_Chou的博客

菜鸟程序媛

  • 博客(403)
  • 问答 (1)
  • 收藏
  • 关注

原创 [leetcode] 286 + 542 BFS

都是通过bfs来一层一层找最短路径542class Solution { int[][] dir = new int[][]{{0,1},{0,-1},{1,0},{-1,0}}; public int[][] updateMatrix(int[][] matrix) { Queue<int[]> q = new LinkedList&l...

2020-02-19 14:36:41 249

原创 [leetcode] 1192. Critical Connections in a Network

class Solution { private int time = 0; public List<List<Integer>> criticalConnections(int n, List<List<Integer>> connections) { List<List<Integer>> ...

2020-01-14 12:53:52 340

原创 [leetcode]384. Shuffle an Array

Proof: Suppose this algorithm works, i.e. for each position j (starting from i), the probability of any number in the range[i,n-1] to be at position j is 1/(n-i).Let’s look at int i = random.nextInt(...

2019-12-02 01:43:53 206

原创 [leetcode] 261. Graph Valid Tree

valid tree: = 无向图+无环+只一个连通分量Solution 1 : BFS思路参考:算法1:我们知道对于环 1-2-3-4-1,每个节点的度都是2,基于此我们有如下算法(这是类似于有向图的拓扑排序):求出图中所有顶点的度删除图中所有度 <=1 的顶点以及与该顶点相关的边,把与这些边相关的顶点的度减一如果还有度<=1的顶点重复步骤2最后如果还存在未被删除的顶...

2019-11-12 00:31:15 300

原创 [leetcode]875. Koko Eating Bananas

class Solution { public int minEatingSpeed(int[] piles, int H) { int lo = 1; int hi = 1_000_000_000; while (lo + 1 < hi) { int mi = (lo + hi) / 2; ...

2019-10-16 01:24:15 125

原创 [leetcode]621. Task Scheduler

https://leetcode.com/problems/task-scheduler/discuss/104500/Java-O(n)-time-O(1)-space-1-pass-no-sorting-solution-with-detailed-explanationclass Solution { public int leastInterval(char[] tasks, i...

2019-10-13 04:29:34 100

原创 [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List

中序遍历的变形/*// Definition for a Node.class Node { public int val; public Node left; public Node right; public Node() {} public Node(int _val,Node _left,Node _right) { val...

2019-10-02 12:30:51 98

原创 [leetcode] 708. Insert into a Cyclic Sorted List

/*// Definition for a Node.class Node { public int val; public Node next; public Node() {} public Node(int _val,Node _next) { val = _val; next = _next; }};*/...

2019-10-02 11:05:47 186

原创 QuickSort和QuickSelect

class Solution { public int[] sortArray(int[] nums) { quickSort(nums,0,nums.length-1); return nums; } public void quickSort(int[] nums, int l,int r){ ...

2019-09-29 05:44:45 211

原创 MergeSort

class Solution { public int[] sortArray(int[] nums) { int[] temp = new int[nums.length]; mergeSort(nums,0,nums.length-1,temp); return nums; } ...

2019-09-29 05:40:53 103 1

原创 [leetcode]253. Meeting Rooms II

class Solution { public int minMeetingRooms(int[][] intervals) { if(intervals == null || intervals.length == 0) return 0; PriorityQueue<Integer> pq = new PriorityQueue<&gt...

2019-09-23 12:56:12 89

原创 [leetcode]301. Remove Invalid Parentheses

class Solution { public List<String> removeInvalidParentheses(String s) { int left = 0; int right = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt...

2019-09-23 05:10:04 94

原创 [leetcode]349. Intersection of Two Arrays

Solution 1: 二分法 nlognset去除重复class Solution { public int[] intersection(int[] nums1, int[] nums2) { HashSet<Integer> set = new HashSet<Integer>(); Arrays.sort(nums2);...

2019-09-20 05:11:45 87

原创 [leetcode]254. Factor Combinations

class Solution { public List<List<Integer>> getFactors(int n) { List<List<Integer>> res = new ArrayList<>(); backtrack(2, n, new ArrayList<Integer&...

2019-09-18 23:09:02 110

原创 [leetcode]697. Degree of an Array

class Solution { public int findShortestSubArray(int[] nums) { HashMap<Integer, Integer> map = new HashMap<>(); int freq = 0, count = 0; int left = 0, right = 0...

2019-09-16 12:57:46 86

原创 [leetcode]973. K Closest Points to Origin

solution 1: quick select时间复杂度 o(n)空间O(n)class Solution { public int[][] kClosest(int[][] points, int K) { quick(points, 0,points.length-1, K); return Arrays.copyOf...

2019-09-08 11:30:58 212

原创 [leetcode]98. Validate Binary Search Tree

Solution 1 : recursive这一题要特别注意corner case。lower和upper用Integer.MIN_VALUE 和 Integer.MAX_VALUE是过不了的。因为BST规定: left.val<root.val && right.val>root.val。如果root正好是Integer.MAX_VALUE,会被误判为false...

2019-09-05 23:33:42 113

原创 [leetcode]187. Repeated DNA Sequences

/* * @lc app=leetcode id=187 lang=java * * [187] Repeated DNA Sequences * * https://leetcode.com/problems/repeated-dna-sequences/description/ * * algorithms * Medium (36.72%) * Likes: 489...

2019-08-29 11:32:08 139

原创 [leetcode]367. Valid Perfect Square

Solution 1: 二分法/* * @lc app=leetcode id=367 lang=java * * [367] Valid Perfect Square */class Solution { public boolean isPerfectSquare(int num) { long l = 1; long h = num; ...

2019-08-28 23:30:57 100

原创 [leetcode]149. Max Points on a Line

/* * @lc app=leetcode id=149 lang=java * * [149] Max Points on a Line */class Solution { public int maxPoints(int[][] points) { if(points == null || points.length == 0) return 0; ...

2019-08-28 22:44:57 121

原创 [leetcode]Top K Frequent Elements

用最大堆import java.util.HashMap;import java.util.Map;import java.util.PriorityQueue;/* * @lc app=leetcode id=347 lang=java * * [347] Top K Frequent Elements */class Solution { public List&l...

2019-08-28 22:31:43 78

原创 [leetcode]75. Sort Colors

三分单向扫描法https://blog.csdn.net/Holmofy/article/details/71168530class Solution { public void sortColors(int[] nums) { //if(nums == null || nums.length == 0) return null; int l = 0;...

2019-08-28 03:06:50 65

原创 [leetcode]215. Kth Largest Element in an Array

Solution 1: 优先队列/heap堆默认就是最小堆时间复杂度:T(nlogk)空间复杂度:O(k) to store the heap elements.class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> pq = new P...

2019-08-28 02:36:09 122

原创 [leetcode]149. Max Points on a Line

相同点斜率为无穷正常求斜率这里的gcd是防止精度问题,将6/2 12/4都化为3/1/* * @lc app=leetcode id=149 lang=java * * [149] Max Points on a Line */class Solution { public int maxPoints(int[][] points) { if(poin...

2019-08-26 13:02:28 85

原创 [leetcode]297. Serialize and Deserialize Binary Tree

时间空间都是o(n)/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class C...

2019-08-25 13:13:40 75

原创 [leetcode]273. Integer to English Words

class Solution { String []less20 = {"", "One", "Two","Three","Four","Five","Six","Seven","Eight","Nine", "Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","...

2019-08-24 11:36:40 79

原创 [leetcode]97. Interleaving String

class Solution { public boolean isInterleave(String s1, String s2, String s3) { int m = s1.length() + 1; int n = s2.length() + 1; if(m + n - 2 != s3.length()) return false;...

2019-08-20 13:38:53 63

原创 [leetcode]91. Decode Ways

Solution 1: O(n) dpdp初始化,为空的时候也有一种编码方式,所以dp【0】= 1;“0” 没有编码方式class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0) return 0; int len = s.length();...

2019-08-12 23:31:31 80

原创 [leetcode]188. Best Time to Buy and Sell Stock IV

public int maxProfit(int k, int[] prices) { int len = prices.length; if (k >= len / 2) return quickSolve(prices); int[][] t = new int[k + 1][len]; for (int ...

2019-08-12 12:09:14 90

原创 [leetcode]279. Perfect Squares

自己写的 时间复杂度差class Solution { public int numSquares(int n) { int [] dp = new int[n+1]; Arrays.fill(dp, Integer.MAX_VALUE); for(int i = 1 ;i <= n ;i++){ ...

2019-08-08 22:20:28 73

原创 [leetcode]63. Unique Paths II

Solution: dp同62题。一行一行向下扫class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[] dp...

2019-08-08 20:55:40 108

原创 [leetcode]62. Unique Paths

Solution 1:二维的dp时间复杂度:0(mn)空间复杂度:0(mn)class Solution { public int uniquePaths(int m, int n) { int [][] dp = new int[n][m]; for(int i = 0; i < m ;i++){ dp[0][i] = ...

2019-08-08 20:20:43 107

原创 [leetcode]395. Longest Substring with At Least K Repeating Characters

class Solution { public int longestSubstring(String s, int k) { if(s == null||s.length() == 0) return 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>...

2019-08-07 22:17:17 83

原创 Leetcode 159. Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most K Distinct Characters 把count初始设为K就可以 public int lengthOfLongestSubstringTwoDistinct(String s) { Map<Character, Integer> map = new HashMap<>();...

2019-08-07 18:21:12 83

原创 [leetcode]290. Word Pattern

class Solution { public boolean wordPattern(String pattern, String str) { if(pattern ==null || pattern.length() == 0 || str == null || str.length() == 0 ) return false; HashMap<...

2019-08-07 17:08:38 68

原创 [leetcode]3. Longest Substring Without Repeating Characters

class Solution { public int lengthOfLongestSubstring(String s) { int [] map = new int[128]; int len = Integer.MIN_VALUE; int start = 0; int end = 0; while(e...

2019-08-07 17:07:14 78

原创 [leetcode]30. Substring with Concatenation of All Words

class Solution { public List<Integer> findSubstring(String s, String[] words) { List<Integer> res = new ArrayList<Integer>(); if(s == null || s.length() == 0|| wo...

2019-08-07 17:02:29 75

原创 [leetcode]205. Isomorphic Strings

不要再用hashtable了map.containsKey复杂度是O(n),所以总复杂度0(n*n)空间复杂度就是128 所以是O(1)class Solution { public boolean isIsomorphic(String s, String t) { if(s == null || s.length() == 0) return true; ...

2019-08-05 00:33:23 63

原创 [leetcode] 209. Minimum Size Subarray Sum

sliding windowsclass Solution { public int minSubArrayLen(int s, int[] nums) { int start = 0; int end = 0; int total = 0; int count = Integer.MAX_VALUE; wh...

2019-08-03 00:52:41 65

原创 [leetcode] 438. Find All Anagrams in a String

class Solution { public int minSubArrayLen(int s, int[] nums) { int start = 0; int end = 0; int total = 0; int count = Integer.MAX_VALUE; while(end < num...

2019-08-03 00:49:01 73

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除