This commit is contained in:
Mrhongxing committed 2026-10-13 02:01:30 +08:00
1 parent b43a46f482
commit b3c7a28b1c
2 files changed
+110

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
package com.hong.www;
import java.util.*;
public class A135 {
public int candy(int[] ratings) {
int temp=-1;
int[] counts=new int[ratings.length];
Arrays.fill(counts,1);
for(int i=1;i<ratings.length;i++){
if(ratings[i]>ratings[i-1]){
counts[i]=counts[i-1]+1;
}
}
for(int i=ratings.length-2;i>=0;i--){
if(ratings[i]>ratings[i+1]&&counts[i]<=counts[i+1]){
counts[i]=counts[i+1]+1;
}
}
int currect=0;
for(int x:counts){
currect+=x;
}
return currect;
}
public static void main(String[] args) {
A135 solution = new A135();
int[] ratings = {1, 3, 2,2,1};
System.out.println(solution.candy(ratings));
}
}
+78
View File
@@ -0,0 +1,78 @@
package com.hong.www;
import java.util.*;
public class A30 {
/*
* public void aaa(int time, String[] words,ArrayList<String> temp){
* time++;
* if(time==words.length+1){
*
* }else{
* ArrayList<String> temp1 = new ArrayList<>();
* for()
* }
* }
*/
public List<Integer> findSubstring(String s, String[] words) {
/*
* int time=0;
* ArrayList<String> temp = new ArrayList<>();
* aaa(time,words,temp);
*
* List<Integer> result = new ArrayList<>();
* for(String x:words){
* temp.setLength(0);
* }
*/
char[] a = s.toCharArray();
HashMap<String, Integer> temp = new HashMap<>();
List<Integer> result = new ArrayList<>();
HashSet<String> temp1 = new HashSet<>();
for(String x:words){
temp.put(x, 0);
}
int left=0;
int right=words[0].toCharArray().length+left;
StringBuilder sb = new StringBuilder();
int time=0;
int time1=0;
while (right<a.length) {
for(String x:words){
if(s.substring(left,right).equals(sb.toString()+x)&&!temp1.contains(x)){
sb.append(x);
temp1.add(x);
time++;
}else if(s.subSequence(right-words[0].toCharArray().length, right).equals(x)&&temp1.contains(x)){
sb.setLength(0);
temp1.clear();
sb.append(x);
temp1.add(x);
time=1;
left=right-words[0].toCharArray().length;
}
}
if(time==words.length){
temp1.remove(s.substring(left,left+words[0].toCharArray().length));
result.add(left);
time--;
left+=3;
sb.delete(0,words[0].toCharArray().length);
}
if(time==0){
left++;
}
right++;
}
//s.substring(left,right);
return result;
}
public static void main(String[] args) {
A30 solution = new A30();
String s = "barfoofoobarthefoobarman";
String[] words = { "bar", "foo", "the" };
System.out.println(solution.findSubstring(s, words));
}
}