class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
String[] str = sentence.split(" ");
int len = searchWord.length();
for(int i = 0;i<str.length;i++){
if(str[i].length()>=len&&isPre(str[i],searchWord)){
return i+1;
}
}
return -1;
}
public boolean isPre(String s,String t){
for(int i = 0;i<t.length();i++){
if(s.charAt(i)!=t.charAt(i)){
return false;
}
}
return true;
}
}
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
if (sentence == null || sentence.length() == 0 || searchWord == null || searchWord.length() == 0) {
return -1;
}
String [] str = sentence.split(" ");
for (int i = 0; i < str.length; i ++) {
String s = str[i];
//startsWith() 方法用于检测字符串是否以指定的前缀开始。
if (s.startsWith(searchWord)) {
return i + 1;
}
}
return -1;
}
}
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
String[] split = sentence.split(" ");
for (int i = 0 ; i < split.length ; i++) {
//indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
if (split[i].indexOf(searchWord) == 0)
return i+1;
}
return -1;
}
}
//滑动窗口
class Solution {
public int maxVowels(String s, int k) {
int left = 0;
int right = 0;
int max = 0;
int count = 0;
while(right<s.length()){
char temp = s.charAt(right);
if(temp=='a'||temp=='e'||temp=='i'||temp=='o'||temp=='u'){
count++;
}
right++;
if (right-left==k){
max = Math.max(max,count);
char c = s.charAt(left);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
count--;
}
left++;
}
}
return max;
}
}
//集合类
class Solution {
public int maxVowels(String s, int k) {
Set<Character> set = new HashSet();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
int r = 0, cur = 0;
for (r = 0; r < k; r++) {
if (r >= s.length()) return cur;
if (set.contains(s.charAt(r))) cur++;
}
int l = 0, max = cur;
while (r < s.length()) {
if (set.contains(s.charAt(l++))) cur--;
if (set.contains(s.charAt(r++))) cur++;
max = Math.max(max, cur);
}
return max;
}
}
//前缀和
class Solution {
public int maxVowels(String s, int k) {
int n = s.length();
int maxLen = 0;
int[] prefixSum = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
prefixSum[i] = prefixSum[i - 1] + (isVowel(s.charAt(i - 1)) ? 1 : 0);
}
for (int i = 0; i < n - k + 1; i++) {
maxLen = Math.max(maxLen, prefixSum[i + k] - prefixSum[i]);
}
return maxLen;
}
private boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
}
class Solution {
List<List<Integer>> list = new ArrayList<>();
public int pseudoPalindromicPaths (TreeNode root) {
if(root==null){
return 0;
}
List<Integer> res = new ArrayList<>();
dfs(root,res);
System.out.println(list);
int count = 0;
for(int i = 0;i<list.size();i++){
if(isPrim(list.get(i))){
count++;
}
}
return count;
}
public void dfs(TreeNode node,List<Integer> res){
if(node==null) return;
res.add(node.val);
if(node.left ==null && node.right==null){
list.add(new ArrayList<>(res));
}else{
dfs(node.left,res);
dfs(node.right,res);
}
res.remove(res.size()-1);
}
public boolean isPrim(List<Integer> res){
int n = 0;
for(int i = 0;i<res.size();i++){
n ^= (1<<res.get(i));
}
if(n==0||(n&(n-1))==0){//判断是否为伪回文
return true;
}
return false;
}
}
class Solution {
int ans=0;
public int pseudoPalindromicPaths (TreeNode root) {
if(root==null) return 0;
helper(root,0);
return ans;
}
public void helper(TreeNode node,int temp){
temp^=(1<<node.val);
if(node.left==null&&node.right==null){
if(temp==0||(temp&(temp-1))==0){//判断是否为伪回文
ans++;
}
}
if(node.left!=null) helper(node.left,temp);
if(node.right!=null) helper(node.right,temp);
return;
}
}
拓展
public boolean isPrim(List<Integer> res){
int[] t = new int[10];
for (int i=0; i<res.size(); i++) {
t[res.get(i)]++;
}
int even=0;
for (int i=0; i<10; i++) {
if (t[i]%2 == 1) {
even++;
}
}
return even<2;
}
public boolean isPrim(List<Integer> res){
int n = 0;
for(int i = 0;i<res.size();i++){
n ^= (1<<res.get(i));
}
if(n==0||(n&(n-1))==0){//判断是否为伪回文
return true;
}
return false;
}
class Solution {
public int maxDotProduct(int[] nums1, int[] nums2) {
int[][] dp = new int[nums1.length][nums2.length];
for (int[] temp : dp) {
Arrays.fill(temp, -1001);
}
dp[0][0] = nums1[0] * nums2[0];
int res = dp[0][0];
for (int i = 1; i < dp[0].length; i++) {
dp[0][i] = Math.max(dp[0][i], Math.max(nums1[0] * nums2[i], dp[0][i - 1]));
}
for (int i = 1; i < dp.length; i++) {
dp[i][0] = Math.max(dp[i][0], Math.max(nums2[0] * nums1[i], dp[i - 1][0]));
}
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = Math.max(Math.max(dp[i - 1][j - 1] + nums1[i] * nums2[j], nums1[i] * nums2[j]), Math.max(dp[i - 1][j], dp[i][j - 1]));
}
}
return dp[nums1.length - 1][nums2.length - 1];
}
}
你知道的越多,你不知道的越多。