所谓理解,通常不过是误解的总和。——村上春树 《斯普特尼克恋人》
class Solution {
public long waysToBuyPensPencils(int total, int cost1, int cost2) {
if(cost1>cost2){
return waysToBuyPensPencils(total,cost2,cost1);
}
int c2 = 0; long res = 0;
while(c2*cost2<=total){
res += Math.floor((total-c2*cost2)/cost1) + 1;
c2++;
}
return res;
}
}
// 哈希
cnt.put(sub, cnt.getOrDefault(sub, 0) + 1);
substring() 是小写
不需要用KMP,直接s.substring(i,i+10); 即可
class Solution {
public List<String> findRepeatedDnaSequences(String s) {
Map<String, Integer> hash = new HashMap<String,Integer>();
List<String> map = new LinkedList<>();
for(int i=0;i+9<s.length();i++){
String temp = s.substring(i,i+10);
hash.put(temp,hash.getOrDefault(temp,0)+1);
if(hash.get(temp)==2){
map.add(temp);
}
}
return map;
}
}
class Solution {
public int strStr(String haystack, String needle) {
Map<String, Integer> hash = new HashMap<>();
hash.put(needle,hash.getOrDefault(needle,0)+1);
for(int i=0;i+needle.length()<=haystack.length();i++){
String s = haystack.substring(i,i+needle.length());
hash.put(s,hash.getOrDefault(s,0)+1);
if(hash.get(needle)>1){
return i;
}
}
return -1;
}
}