Skip to content

Commit ada859e

Browse files
committed
update
1 parent 5ef7ab2 commit ada859e

4 files changed

+170
-0
lines changed

215_findKthLargest.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Quick Select解法
2+
算法复杂度average -- O(n), worst -- O(n^2)
3+
4+
public class Solution {
5+
public int findKthLargest(int[] nums, int k) {
6+
int n = nums.length;
7+
int index = partition(nums,0,n-1,n+1-k);
8+
return nums[index];
9+
}
10+
11+
public int partition(int[] nums, int l, int r, int k){
12+
int pivot = nums[r];
13+
int storeIndex = l;
14+
for(int i = l; i < r; i++){
15+
if(nums[i]<= pivot){
16+
swap(nums,i, storeIndex);
17+
storeIndex++;
18+
}
19+
}
20+
swap(nums, storeIndex,r);
21+
int left = storeIndex+1-l;
22+
if(left == k) return storeIndex;
23+
else if(left < k) return partition(nums, storeIndex+1, r, k-left);
24+
else return partition(nums,l, storeIndex-1,k);
25+
}
26+
27+
private void swap(int[] nums, int i, int j){
28+
int temp = nums[i];
29+
nums[i] = nums[j];
30+
nums[j] = temp;
31+
}
32+
}
33+
34+
35+
36+
37+
PQ解法,时间复杂度:构建整个的heap的PQ用时是NlogN插入操作是logN但因为我们要插入N个点所以O(NlogN)。
38+
注意pq不加comparator实现的是MIN HEAP,所以升序解里面保证最小值都被踢掉了最后pq里面剩下了k个值peek的那个就是第k个最大值
39+
40+
41+
升序解
42+
public class Solution {
43+
public int findKthLargest(int[] nums, int k) {
44+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
45+
for(int num: nums){
46+
pq.offer(num);
47+
while(pq.size()>k){
48+
pq.poll();
49+
}
50+
}
51+
return pq.peek();
52+
}
53+
}
54+
55+
降序解
56+
Collections.reverseOrder()是这个Comparator!!!可代替它
57+
58+
public class Solution {
59+
public int findKthLargest(int[] nums, int k) {
60+
int result = 0;
61+
Comparator<Integer> comparator = new Comparator<Integer>(){
62+
public int compare(Integer a, Integer b){
63+
return b-a;
64+
}
65+
};
66+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(nums.length, comparator);
67+
for(int num: nums){
68+
pq.offer(num);
69+
}
70+
while(k > 0){
71+
result = pq.poll();
72+
k--;
73+
}
74+
return result;
75+
}
76+
}
77+
78+

36_validSudoku.java

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
更简洁的写法用一个长方形划定一个区域检测其为valid or not
2+
public class Solution {
3+
public boolean isValidSudoku(char[][] board) {
4+
for(int i = 0; i < 9; i++){
5+
if(!isValid(board,0,i,8,i)) return false; //check col
6+
if(!isValid(board,i,0,i,8)) return false; //check row
7+
}
8+
9+
for(int i = 0; i < 3; i++){
10+
for(int j = 0; j < 3; j++){
11+
if(!isValid(board,i*3,j*3,i*3+2,j*3+2)) return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
public boolean isValid(char[][] board, int x1, int y1, int x2, int y2){
18+
HashSet<Character> set = new HashSet<Character>();
19+
for(int i = x1; i <= x2; i++){
20+
for(int j = y1; j <= y2; j++){
21+
if(board[i][j] == '.') continue;
22+
if(set.contains(board[i][j])) return false;
23+
else set.add(board[i][j]);
24+
}
25+
}
26+
return true;
27+
}
28+
}
29+
30+
131
//http://xiaoyaoworm.com/blog/2015/04/14/%E6%96%B0leetcode-hashtable-1-valid-sudoku/
232

333
public class Solution {

49_groupAnagrams.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public List<List<String>> groupAnagrams(String[] strs) {
3+
List<List<String>> result = new ArrayList<List<String>>();
4+
if(strs == null || strs.length == 0) return result;
5+
6+
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
7+
8+
for(String str: strs){
9+
char[] arr = str.toCharArray();
10+
Arrays.sort(arr);
11+
String key = new String(arr);
12+
if(map.containsKey(key)) map.get(key).add(str);
13+
else {
14+
List<String> list = new ArrayList<String>();
15+
list.add(str);
16+
map.put(key,list);
17+
}
18+
}
19+
for(String k: map.keySet()){
20+
result.add(map.get(k));
21+
}
22+
return result;
23+
}
24+
}

54_spiralOrder.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
public List<Integer> spiralOrder(int[][] matrix) {
3+
List<Integer> result = new ArrayList<Integer>();
4+
if(matrix == null || matrix.length == 0) return result;
5+
6+
int rowStart = 0;
7+
int rowEnd = matrix.length-1;
8+
int colStart = 0;
9+
int colEnd = matrix[0].length-1;
10+
11+
while(rowStart <= rowEnd && colStart <= colEnd){ //&& AND relation!!!
12+
for(int i = colStart; i <= colEnd; i++){
13+
result.add(matrix[rowStart][i]);
14+
}
15+
rowStart++;
16+
17+
for(int i = rowStart; i <= rowEnd; i++){
18+
result.add(matrix[i][colEnd]);
19+
}
20+
colEnd--;
21+
22+
if(rowStart<=rowEnd){ // dont forget!!!! because rowStart++
23+
for(int i = colEnd; i >= colStart; i--){
24+
result.add(matrix[rowEnd][i]);
25+
}
26+
}
27+
rowEnd--;
28+
29+
if(colStart <= colEnd){ // dont forget!!!! because colEnd--
30+
for(int i = rowEnd; i >= rowStart; i--){
31+
result.add(matrix[i][colStart]);
32+
}
33+
}
34+
colStart++;
35+
}
36+
return result;
37+
}
38+
}

0 commit comments

Comments
 (0)