1. Two Sum
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i++){
if(map.containsKey(target-nums[i])){
res[0]=map.get(target-nums[i]);
res[1]=i;
return res;
}else{
map.put(nums[i],i);
}
}
return res;
}
}
187. Repeated DNA Sequences
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Map<String,Integer> map = new HashMap<>();
List<String> res = new ArrayList<>();
for(int i=0;i+10<=s.length();i++){
String str = s.substring(i,i+10);
map.put(str,map.containsKey(str)?map.get(str)+1:1);
if(map.get(str)==2) res.add(str);
}
return res;
}
}
560. Subarray Sum Equals K
前缀和+hash
class Solution {
public int subarraySum(int[] nums, int k) {
int n = nums.length;
int res = 0;
Map<Integer,Integer> map = new HashMap<>();
map.put(0,1);
for(int i=0,sum=0;i<n;i++){
sum+=nums[i];
if(map.containsKey(sum-k)) res+=map.get(sum-k);
map.put(sum,map.containsKey(sum)?map.get(sum)+1:1);
}
return res;
}
}
547. Friend Circles
并查集
class Solution {
int[] p;
public int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
public int findCircleNum(int[][] M) {
int n = M.length;
int res = n;
p = new int[n];
for(int i=0;i<n;i++){
p[i]=i;
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(M[i][j]==0) continue;
if(find(i)!=find(j)){
p[find(i)]=find(j);
res--;
}
}
}
return res;
}
}
684. Redundant Connection
class Solution {
int[] p;
public int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
public int[] findRedundantConnection(int[][] edges) {
int n = edges.length;
p = new int[n+1];
for(int i=0;i<=n;i++){
p[i]=i;
}
for(int i=0;i<n;i++){
if(find(edges[i][0])==find(edges[i][1])){
return new int[]{edges[i][0],edges[i][1]};
}else{
p[find(edges[i][0])]=find(edges[i][1]);
}
}
return new int[]{-1,-1};
}
}
692. Top K Frequent Words
注意:第一顺位是数量,第二顺位是字典序的实现方式。
class Solution {
public List<String> topKFrequent(String[] words, int k) {
List<String> res = new ArrayList<>();
Map<String,Integer> map = new HashMap<>();
for(int i=0;i<words.length;i++){
if(map.containsKey(words[i])){
map.put(words[i],map.get(words[i])+1);
}else{
map.put(words[i],1);
}
}
PriorityQueue<String> pq =new PriorityQueue<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(map.get(o1)==map.get(o2)){
return o2.compareTo(o1);//字典序逆序
} else{
return map.get(o1)-map.get(o2);//小根堆,最小元素最先poll
}
}});
for(String str : map.keySet()){
pq.add(str);
if(pq.size()>k) pq.poll();
}
while(!pq.isEmpty()){//必须用poll的弹出顺序
res.add(pq.poll());//字典序逆序,前K个元素 从小到大 排列
}
Collections.reverse(res);//反转后 字典序顺序且从大到小排列
return res;
}
}
295. Find Median from Data Stream
class MedianFinder {
PriorityQueue<Integer> up = new PriorityQueue<>();//最小堆
PriorityQueue<Integer> down = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});//最大堆
/** initialize your data structure here. */
public MedianFinder() {
}
public void addNum(int num) {
if(down.isEmpty() || num >= down.peek()){
up.add(num);
}else{
down.add(num);
up.add(down.poll());
}
if(up.size()>down.size()+1){
down.add(up.poll());
}
}
public double findMedian() {
if((up.size()+down.size())%2==1) return up.peek();
else return (up.peek()+down.peek())/2.0;
}
}