From c7cba88e2dec7bcceacbefb759e24ae564f71223 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sat, 3 Apr 2021 17:37:12 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q524.java | 50 +++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q633.java | 39 +++++++++++++++ 5.leetcode/src/com/fanxb/common/Q680.java | 42 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q76.java | 58 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q88.java | 4 +- 5 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q524.java create mode 100644 5.leetcode/src/com/fanxb/common/Q633.java create mode 100644 5.leetcode/src/com/fanxb/common/Q680.java create mode 100644 5.leetcode/src/com/fanxb/common/Q76.java diff --git a/5.leetcode/src/com/fanxb/common/Q524.java b/5.leetcode/src/com/fanxb/common/Q524.java new file mode 100644 index 0000000..0a33bfe --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q524.java @@ -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 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"))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q633.java b/5.leetcode/src/com/fanxb/common/Q633.java new file mode 100644 index 0000000..0bd68c4 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q633.java @@ -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<c,l++. + * 4. 如果sum=c,返回true + * 5. 如果sum<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)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q680.java b/5.leetcode/src/com/fanxb/common/Q680.java new file mode 100644 index 0000000..6ba5b14 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q680.java @@ -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")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q76.java b/5.leetcode/src/com/fanxb/common/Q76.java new file mode 100644 index 0000000..bea1a4e --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q76.java @@ -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")); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q88.java b/5.leetcode/src/com/fanxb/common/Q88.java index d3dc461..635d78c 100644 --- a/5.leetcode/src/com/fanxb/common/Q88.java +++ b/5.leetcode/src/com/fanxb/common/Q88.java @@ -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)); } }