This commit is contained in:
fanxb 2021-04-03 17:37:12 +08:00
parent b5c48629c4
commit c7cba88e2d
5 changed files with 190 additions and 3 deletions

View File

@ -0,0 +1,50 @@
package com.fanxb.common;
import java.util.Arrays;
import java.util.List;
/**
* 通过删除字母匹配到字典里最长单词
* 地址 https://leetcode-cn.com/problems/longest-word-in-dictionary-through-deleting/
* 思路
* 1. 返回长度最长且字典顺序最小的字符串,首先需要对字符串进行排序这样就不需要扫描所有的字符串了返回第一个符合要求的即可
* 2. 使用两个指针i指向s的开头j指向目标字符串item的开头
* 3. 如果s[i++]!=item[j]说明要删除i位置的字符否则j++,并判断j是否到达item末尾到达说明item符合要求
*
*
* @author fanxb
* @date 2021/4/3
**/
public class Q524 {
public String findLongestWord(String s, List<String> dictionary) {
dictionary.sort((a, b) -> {
if (a.length() == b.length()) {
return a.compareTo(b);
} else {
return b.length() - a.length();
}
});
for (String item : dictionary) {
if (s.length() < item.length()) {
continue;
}
int i = 0, j = 0;
while (i < s.length() && s.length() - i >= item.length() - j) {
if (s.charAt(i) == item.charAt(j)) {
j++;
if (j == item.length()) {
return item;
}
}
i++;
}
}
return "";
}
public static void main(String[] args) {
Q524 q = new Q524();
System.out.println(q.findLongestWord("abpcplea", Arrays.asList("ale", "apple", "monkey", "plea")));
}
}

View File

@ -0,0 +1,39 @@
package com.fanxb.common;
/**
* 平方数之和
* 题目地址: https://leetcode-cn.com/problems/sum-of-square-numbers/
* 解题思路 滑动窗口
* 1. 这两个数的取值只可能是[0,(int) Math.sqrt(c)]中的因此采用滑动窗口设定l=0,r=(int)Math.sqrt(c)
* 2. 判断l是否不大于r,否则返回false
* 3. sum=l*l+r*r.如果sum&lt;c,l++.
* 4. 如果sum=c,返回true
* 5. 如果sum&lt;c,l++,重复2
* 6. 如果sum>c,r--,l++; 当r--的时候l必然++
*
* @author fanxb
* Date: 2021/4/2 15:10
*/
public class Q633 {
public static boolean solution(int c) {
int l = 0, r = (int) Math.sqrt(c);
int sum;
while (l <= r) {
if ((sum = l * l + r * r) < c) {
l++;
} else if (sum == c) {
return true;
} else {
r--;
l++;
}
}
return false;
}
public static void main(String[] args) {
System.out.println(solution(1));
}
}

View File

@ -0,0 +1,42 @@
package com.fanxb.common;
import java.util.Arrays;
/**
* 验证回文字符串
* 题目地址https://leetcode-cn.com/problems/valid-palindrome-ii/
* 解题思路先按照正常的回文数逻辑从两边向中间检查直到发现不想等的一组字符分辨判断删除左边或右边还是否为回文数
*
* @author fanxb
* Date: 2021/04/03 15:10
*/
public class Q680 {
public boolean check(String s, int l, int r) {
while (l < r) {
if (s.charAt(l++) != s.charAt(r--)) {
return false;
}
}
return true;
}
public boolean solution(String s) {
int l = 0, r = s.length() - 1;
while (l < r) {
if (s.charAt(l) == s.charAt(r)) {
l++;
r--;
} else {
return check(s, l + 1, r) || check(s, l, r - 1);
}
}
return true;
}
public static void main(String[] args) {
Q680 q = new Q680();
System.out.println(q.solution("aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"));
}
}

View File

@ -0,0 +1,58 @@
package com.fanxb.common;
import java.util.*;
import java.util.stream.Collectors;
/**
* 最小覆盖子串
* 题目地址: https://leetcode-cn.com/problems/minimum-window-substring/
* 解题思路 滑动窗口指定两个指针l,r.r向右滑动直到所有的目标字符都存在然后再让l向右滑动直到刚好满足要求判断当前是否为较小的子串
* 然后在让l=l+1,重复上述过程直到r到达末尾
*
* @author fanxb
* Date: 2021/4/2 15:10
*/
public class Q76 {
public static String solution(String s, String t) {
int totalChars = t.length();
int[] target = new int[128];
for (int i = 0; i < totalChars; i++) {
target[t.charAt(i)]++;
}
//当前最小长度的开始,最小长度的结束
int start = -1, end = 0;
int l = 0, r = 0;
char temp;
while (r < s.length()) {
temp = s.charAt(r++);
if (target[temp] > 0) {
totalChars--;
}
target[temp]--;
if (totalChars == 0) {
//说明当前i->j-1的字符串已经满足要求
// 再让l向右移动直到刚好满足要求
while (target[(temp = s.charAt(l))] < 0) {
target[temp]++;
l++;
}
if (start < 0 || r - l < end - start + 1) {
//判断当前是否为最优
start = l;
end = r - 1;
}
//重新从i+1开始搜索
l = l + 1;
totalChars++;
target[temp]++;
}
}
return start < 0 ? "" : s.substring(start, end + 1);
}
public static void main(String[] args) {
System.out.println(solution("a", "aa"));
}
}

View File

@ -33,9 +33,7 @@ public class Q88 {
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 0, 0, 0};
int[] nums2 = {2, 5, 6};
solution(nums1, 3, nums2, 3);
int[] nums1 = new int[12];
System.out.println(Arrays.toString(nums1));
}
}