提交 0e078c55 编写于 作者: L liu13

20190305

上级 0765a89f
package code;
/*
* 344. Reverse String
* 题意:反转字符串
* 难度:Easy
* 分类:Two Pointers, String
* 思路:
* Tips:
*/
public class lc344 {
public void reverseString(char[] s) {
int begin = 0, end = s.length-1;
while(begin<end){
char ch = s[begin];
s[begin] = s[end];
s[end] = ch;
begin++;
end--;
}
}
}
package code;
import java.util.Comparator;
import java.util.PriorityQueue;
/*
* 378. Kth Smallest Element in a Sorted Matrix
* 题意:在矩阵中搜索第k大的数,横轴和纵轴都是有序的
* 难度:Medium
* 分类:Binary Search, Heap
* 思路:
* 思路:两种思路。 1是类似多个有序链表合并的思路,优先队列。
* 2是二分,二分的是val,看比这个val小的数是不是k
* Tips:lc23方法很像
*/
public class lc378 {
class Cell{
int val, row, col;
Cell(int v, int r, int c){
val = v;
row = r;
col = c;
}
}
public int kthSmallest(int[][] matrix, int k) {
PriorityQueue<Cell> pq = new PriorityQueue(new Comparator<Cell>() {
@Override
public int compare(Cell o1, Cell o2) {
return o1.val-o2.val;
}
});
for (int i = 0; i < matrix.length ; i++) pq.add(new Cell(matrix[i][0], i, 0));
while(k>1){
Cell c = pq.remove();
if(c.col+1<matrix[0].length) pq.add(new Cell(matrix[c.row][c.col+1], c.row, c.col+1));
k--;
}
return pq.remove().val;
}
public int kthSmallest2(int[][] matrix, int k) {
int low = matrix[0][0];
int high = matrix[matrix.length-1][matrix[0].length-1];
while(low<=high){ //确保区间最后缩到0
int mid = low+(high-low)/2;
int num = getLessNum(matrix, mid);
if(num<k) low = mid+1;
else high = mid-1;
}
return low-1;
}
public int getLessNum(int[][] matrix, int val){ //求矩阵中比这个数小的数的个数
int res = 0;
int row = 0;
while(row<matrix.length){
int col = 0;
while(col<matrix[0].length && matrix[row][col]<val) col++;
res += col;
row++;
}
return res;
}
}
package code;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/*
* 380. Insert Delete GetRandom O(1)
* 题意:设计一个数据结构,插入,删除,随机获得一个元素 这三个操作的复杂度都为O(1)
* 难度:Medium
* 分类:Array, Hash Table, Design
* 思路:List 的插入和删除都是O(1), 通过hashmap绑定来使得Get也为O(1)
* Tips:
*/
public class lc380 {
public class RandomizedSet {
HashMap<Integer, Integer> valToInd;
List<Integer> list;
int ind = 0;
/** Initialize your data structure here. */
public RandomizedSet() {
valToInd = new HashMap<>();
list = new ArrayList<>();
}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
if(valToInd.containsKey(val)) return false;
list.add(val);
valToInd.put(val,list.size()-1);
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
int ind = valToInd.getOrDefault(val,-1);
if(ind == -1) return false;
Collections.swap(list,ind,list.size()-1);
int swappedWith = list.get(ind);
valToInd.put(swappedWith,ind);
list.remove(list.size()-1);
valToInd.remove(val);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
int max = list.size();
int min = 0;
int ind = (int)(Math.random() * (max - min) + min);
return list.get(ind);
}
}
}
package code;
/*
* 384. Shuffle an Array
* 题意:重排列一个数组,每个值在每个位置的概率都是均匀的
* 难度:Medium
* 分类:
* 思路:
* Tips:
*/
public class lc384 {
public class Solution {
private int[] nums;
public Solution(int[] nums) {
this.nums = nums;
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
return nums;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
int[] rand = new int[nums.length];
for (int i = 0; i < nums.length; i++){
int r = (int) (Math.random() * (i+1));
rand[i] = rand[r];
rand[r] = nums[i];
}
return rand;
}
}
}
package code;
import java.util.HashMap;
/*
* 454. 4Sum II
* 题意:从4个数组中各挑一个数,使得和为0
* 难度:Medium
* 分类:Hash Table, Binary Search
* 思路:自己没想起来,看了答案后感觉很无聊
* 两个集合暴力求出所有和的可能,然后2Sum的思路,利用Hashmap即可
* 其实是利用了二分的思想,把4个数的和变为两个2个数的和
* Tips:
*/
public class lc454 {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
HashMap<Integer, Integer> hm = new HashMap();
int res=0;
for(int i=0; i<A.length; i++){
for(int j=0; j<B.length; j++){ //从0开始
hm.put(A[i]+B[j],hm.getOrDefault(A[i]+B[j],0)+1);
}
}
for(int i=0; i<C.length; i++){
for(int j=0; j<D.length; j++){
res+=hm.getOrDefault(-C[i]-D[j],0);
}
}
return res;
}
}
......@@ -6,6 +6,7 @@ package code;
* 分类:Hash Table, Two Pointers, String
* 思路:两个指针,移动右指针使得满足条件,移动左指针缩短距离。用hashmap存储进行判断是否满足条件。
* Tips:很难的题,思路记一下。
* https://leetcode.com/problems/minimum-window-substring/discuss/26808/here-is-a-10-line-template-that-can-solve-most-substring-problems
*/
import java.util.HashMap;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册