From 0a6b066776ca3e3710b394e9fbb59a5f3cfa8f71 Mon Sep 17 00:00:00 2001 From: fleyx Date: Wed, 10 Jan 2024 17:27:57 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q125.java | 38 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q209.java | 27 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q3.java | 27 +++++++++++++++- 5.leetcode/src/com/fanxb/common/Q392.java | 35 +++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q125.java create mode 100644 5.leetcode/src/com/fanxb/common/Q209.java create mode 100644 5.leetcode/src/com/fanxb/common/Q392.java diff --git a/5.leetcode/src/com/fanxb/common/Q125.java b/5.leetcode/src/com/fanxb/common/Q125.java new file mode 100644 index 0000000..0b8667b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q125.java @@ -0,0 +1,38 @@ +package com.fanxb.common; + +/** + * 题目地址: https://leetcode-cn.com/problems/partition-labels/ + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q125 { + + public boolean isPalindrome(String s) { + char[] chars = s.toLowerCase().toCharArray(); + int i=0,j=chars.length-1; + while (i='a' && ci<='z') || (ci>='0' && ci<='9'); + } + + public static void main(String[] args) { + int[] prices = {7, 1, 5, 3, 6, 4}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q209.java b/5.leetcode/src/com/fanxb/common/Q209.java new file mode 100644 index 0000000..e5550c2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q209.java @@ -0,0 +1,27 @@ +package com.fanxb.common; + +import java.util.Arrays; + +public class Q209 { + + public int minSubArrayLen(int target, int[] nums) { + int res = Integer.MAX_VALUE, l = 0, r = 0; + int sum = 0; + while (r < nums.length) { + //循环添加右边的元素到窗口中 + sum += nums[r]; + //如果和大于target了,就开始减掉左边的元素,并计算最小值 + while (sum >= target) { + res = Math.min(res, r - l + 1); + sum -= nums[l]; + l++; + } + r++; + } + return res == Integer.MAX_VALUE ? 0 : res; + } + + public static void main(String[] args) { + int[] s = {1, 2, 3, 1}; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q3.java b/5.leetcode/src/com/fanxb/common/Q3.java index b7f19c9..9954a4d 100644 --- a/5.leetcode/src/com/fanxb/common/Q3.java +++ b/5.leetcode/src/com/fanxb/common/Q3.java @@ -39,7 +39,32 @@ public class Q3 { return Math.max(characters.size(), res); } + public int lengthOfLongestSubstring1(String s) { + int l = 0, r = 0, length = s.length(); + int[] map = new int[129]; + int res = 0; + while (r < length) { + char c = s.charAt(r); + boolean has = map[c] == 1; + if (has) { + res = Math.max(res, r - l); + //遇到重复的,把l右移直到排除掉重复的 + while (true) { + char c1 = s.charAt(l); + map[c1] = 0; + l++; + if (c1 == c) break; + } + } else if (r == length - 1) { + res = Math.max(res, r - l + 1); + } + map[c] = 1; + r++; + } + return res; + } + public static void main(String[] args) { - System.out.println(new Q3().lengthOfLongestSubstring("bbbbbbbbbbbbbbbbb")); + System.out.println(new Q3().lengthOfLongestSubstring1("abcabcbb")); } } diff --git a/5.leetcode/src/com/fanxb/common/Q392.java b/5.leetcode/src/com/fanxb/common/Q392.java new file mode 100644 index 0000000..4d1e2d2 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q392.java @@ -0,0 +1,35 @@ +package com.fanxb.common; + +/** + * 两数相加 + * + * @author fanxb + * @date 2021/6/1 + **/ +public class Q392 { + public boolean isSubsequence(String s, String t) { + int sLength=s.length(),tLength=t.length(),si=0,ti=0; + while (si=sLength; + } + + public static void main(String[] args) { + } +}