Skip to content

Commit 0e078c5

Browse files
committed
20190305
1 parent 0765a89 commit 0e078c5

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

code/lc344.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package code;
2+
/*
3+
* 344. Reverse String
4+
* 题意:反转字符串
5+
* 难度:Easy
6+
* 分类:Two Pointers, String
7+
* 思路:
8+
* Tips:
9+
*/
10+
public class lc344 {
11+
public void reverseString(char[] s) {
12+
int begin = 0, end = s.length-1;
13+
while(begin<end){
14+
char ch = s[begin];
15+
s[begin] = s[end];
16+
s[end] = ch;
17+
begin++;
18+
end--;
19+
}
20+
}
21+
}

code/lc378.java

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,62 @@
11
package code;
2+
3+
import java.util.Comparator;
4+
import java.util.PriorityQueue;
5+
26
/*
37
* 378. Kth Smallest Element in a Sorted Matrix
48
* 题意:在矩阵中搜索第k大的数,横轴和纵轴都是有序的
59
* 难度:Medium
610
* 分类:Binary Search, Heap
7-
* 思路:
11+
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
12+
* 2是二分,二分的是val,看比这个val小的数是不是k
813
* Tips:lc23方法很像
914
*/
1015
public class lc378 {
16+
class Cell{
17+
int val, row, col;
18+
Cell(int v, int r, int c){
19+
val = v;
20+
row = r;
21+
col = c;
22+
}
23+
}
1124
public int kthSmallest(int[][] matrix, int k) {
12-
25+
PriorityQueue<Cell> pq = new PriorityQueue(new Comparator<Cell>() {
26+
@Override
27+
public int compare(Cell o1, Cell o2) {
28+
return o1.val-o2.val;
29+
}
30+
});
31+
for (int i = 0; i < matrix.length ; i++) pq.add(new Cell(matrix[i][0], i, 0));
32+
while(k>1){
33+
Cell c = pq.remove();
34+
if(c.col+1<matrix[0].length) pq.add(new Cell(matrix[c.row][c.col+1], c.row, c.col+1));
35+
k--;
36+
}
37+
return pq.remove().val;
38+
}
39+
40+
public int kthSmallest2(int[][] matrix, int k) {
41+
int low = matrix[0][0];
42+
int high = matrix[matrix.length-1][matrix[0].length-1];
43+
while(low<=high){ //确保区间最后缩到0
44+
int mid = low+(high-low)/2;
45+
int num = getLessNum(matrix, mid);
46+
if(num<k) low = mid+1;
47+
else high = mid-1;
48+
}
49+
return low-1;
50+
}
51+
public int getLessNum(int[][] matrix, int val){ //求矩阵中比这个数小的数的个数
52+
int res = 0;
53+
int row = 0;
54+
while(row<matrix.length){
55+
int col = 0;
56+
while(col<matrix[0].length && matrix[row][col]<val) col++;
57+
res += col;
58+
row++;
59+
}
60+
return res;
1361
}
1462
}

code/lc380.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
/*
8+
* 380. Insert Delete GetRandom O(1)
9+
* 题意:设计一个数据结构,插入,删除,随机获得一个元素 这三个操作的复杂度都为O(1)
10+
* 难度:Medium
11+
* 分类:Array, Hash Table, Design
12+
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
13+
* Tips:
14+
*/
15+
public class lc380 {
16+
public class RandomizedSet {
17+
18+
HashMap<Integer, Integer> valToInd;
19+
List<Integer> list;
20+
int ind = 0;
21+
22+
/** Initialize your data structure here. */
23+
public RandomizedSet() {
24+
valToInd = new HashMap<>();
25+
list = new ArrayList<>();
26+
}
27+
28+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
29+
public boolean insert(int val) {
30+
if(valToInd.containsKey(val)) return false;
31+
list.add(val);
32+
valToInd.put(val,list.size()-1);
33+
return true;
34+
}
35+
36+
/** Removes a value from the set. Returns true if the set contained the specified element. */
37+
public boolean remove(int val) {
38+
int ind = valToInd.getOrDefault(val,-1);
39+
if(ind == -1) return false;
40+
Collections.swap(list,ind,list.size()-1);
41+
int swappedWith = list.get(ind);
42+
valToInd.put(swappedWith,ind);
43+
list.remove(list.size()-1);
44+
valToInd.remove(val);
45+
return true;
46+
}
47+
48+
/** Get a random element from the set. */
49+
public int getRandom() {
50+
int max = list.size();
51+
int min = 0;
52+
int ind = (int)(Math.random() * (max - min) + min);
53+
return list.get(ind);
54+
}
55+
}
56+
}

code/lc384.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package code;
2+
/*
3+
* 384. Shuffle an Array
4+
* 题意:重排列一个数组,每个值在每个位置的概率都是均匀的
5+
* 难度:Medium
6+
* 分类:
7+
* 思路:
8+
* Tips:
9+
*/
10+
public class lc384 {
11+
public class Solution {
12+
13+
private int[] nums;
14+
15+
public Solution(int[] nums) {
16+
this.nums = nums;
17+
}
18+
19+
/** Resets the array to its original configuration and return it. */
20+
public int[] reset() {
21+
return nums;
22+
}
23+
24+
/** Returns a random shuffling of the array. */
25+
public int[] shuffle() {
26+
int[] rand = new int[nums.length];
27+
for (int i = 0; i < nums.length; i++){
28+
int r = (int) (Math.random() * (i+1));
29+
rand[i] = rand[r];
30+
rand[r] = nums[i];
31+
}
32+
return rand;
33+
}
34+
}
35+
}

code/lc454.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package code;
2+
3+
import java.util.HashMap;
4+
5+
/*
6+
* 454. 4Sum II
7+
* 题意:从4个数组中各挑一个数,使得和为0
8+
* 难度:Medium
9+
* 分类:Hash Table, Binary Search
10+
* 思路:自己没想起来,看了答案后感觉很无聊
11+
* 两个集合暴力求出所有和的可能,然后2Sum的思路,利用Hashmap即可
12+
* 其实是利用了二分的思想,把4个数的和变为两个2个数的和
13+
* Tips:
14+
*/
15+
public class lc454 {
16+
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
17+
HashMap<Integer, Integer> hm = new HashMap();
18+
int res=0;
19+
for(int i=0; i<A.length; i++){
20+
for(int j=0; j<B.length; j++){ //从0开始
21+
hm.put(A[i]+B[j],hm.getOrDefault(A[i]+B[j],0)+1);
22+
}
23+
}
24+
for(int i=0; i<C.length; i++){
25+
for(int j=0; j<D.length; j++){
26+
res+=hm.getOrDefault(-C[i]-D[j],0);
27+
}
28+
}
29+
return res;
30+
}
31+
}

code/lc76.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* 分类:Hash Table, Two Pointers, String
77
* 思路:两个指针,移动右指针使得满足条件,移动左指针缩短距离。用hashmap存储进行判断是否满足条件。
88
* Tips:很难的题,思路记一下。
9+
* https://leetcode.com/problems/minimum-window-substring/discuss/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems
910
*/
1011
import java.util.HashMap;
1112

0 commit comments

Comments
 (0)