142.环形链表Ⅱ
不会,不过答案用了数学的想法,我以为计算机里只有暴力呢。
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null)
return null;
ListNode p=head;
ListNode p2=head;
while(true){
if(p2.next==null||p2.next.next==null)
return null;
p=p.next;
p2=p2.next.next;
if(p==p2)
break;
}
p=head;
while(true){
if(p==p2)
return p;
else {p=p.next;
p2=p2.next;}
}
}
}
1002.查找共用字符
不算难,但我也做了大半天,因为一开始老是报数组越界,想死没想出来哪里越界了,莫名其妙的,好在做出来了。
class Solution {
public List<String> commonChars(String[] words) {
List<String> ans=new ArrayList<>();
int n=words.length;
int[][] a=new int[n][26];
for(int i=0;i<n;i++){
for(int j=0;j<words[i].length();j++){
a[i][words[i].charAt(j)-'a']++;
}
}
for(int i=0;i<26;i++){
int num1=101;
for(int j=0;j<n;j++){
num1=Math.min(a[j][i],num1);
}
for(int k=0;k<num1;k++){
ans.add(String.valueOf((char)('a'+i)));
}
}
return ans;
}
}