diff --git a/code/lc344.java b/code/lc344.java new file mode 100644 index 0000000000000000000000000000000000000000..ec07daab691b1d147abf38a4466deb73a9a4d520 --- /dev/null +++ b/code/lc344.java @@ -0,0 +1,21 @@ +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 pq = new PriorityQueue(new Comparator() { + @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 valToInd; + List 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); + } + } +} diff --git a/code/lc384.java b/code/lc384.java new file mode 100644 index 0000000000000000000000000000000000000000..1d3011823989320eb150d616611759555524913e --- /dev/null +++ b/code/lc384.java @@ -0,0 +1,35 @@ +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; + } + } +} diff --git a/code/lc454.java b/code/lc454.java new file mode 100644 index 0000000000000000000000000000000000000000..5f5aa49052baca44a652998195822c4521e94579 --- /dev/null +++ b/code/lc454.java @@ -0,0 +1,31 @@ +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 hm = new HashMap(); + int res=0; + for(int i=0; i