1624
代码
class Solution {
public int maxLengthBetweenEqualCharacters(String s) {
Map<Character,Integer> map=new HashMap<>();
int max=-1;
for(int i=0;i<s.length();i++){
if(map.containsKey(s.charAt(i))){
max=Math.max(max,i-map.get(s.charAt(i))-1);
}else{
map.put(s.charAt(i),i);
}
}
return max;
}
}
1876
一个朴实无华的思路 太朴实了
class Solution {
public int countGoodSubstrings(String s) {
int count=0;
for(int i=0;i<=s.length()-3;i++){
if(s.charAt(i)!=s.charAt(i+1)&&s.charAt(i)!=s.charAt(i+2)&&s.charAt(i+1)!=s.charAt(i+2)){
count++;
}
}
return count;
}
}
692
代码
class Solution {
public List<String> topKFrequent(String[] words, int k) {
Map<String,Integer> map=new HashMap<>();//放入哈希表中
for(String word:words){
map.put(word,map.getOrDefault(word,0)+1);
}
List<String>res=new ArrayList<>();
for(Map.Entry<String,Integer> entry:map.entrySet()){
res.add(entry.getKey());
}
Collections.sort(res,new Comparator<String>(){
public int compare(String word1,String word2){
return map.get(word1)==map.get(word2)?word1.compareTo(word2):map.get(word2)-map.get(word1);
}
});
return res.subList(0,k);
}
}
1295
朴素的暴力求解
class Solution {
public int findNumbers(int[] nums) {
int count=0;
for(int i=0;i<nums.length;i++){
if((nums[i]>9&&nums[i]<100)||(nums[i]>999&&nums[i]<10000)||(nums[i]>99999)){
count++;
}
}
return count;
}
}