-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
taowong
committed
Jan 31, 2024
1 parent
d92a39e
commit 8c12206
Showing
11 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/Top100Liked/leetcode/editor/cn/FindAllAnagramsInAString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。 | ||
// | ||
// 异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
// 输入: s = "cbaebabacd", p = "abc" | ||
// 输出: [0,6] | ||
// 解释: | ||
// 起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。 | ||
// 起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
// 输入: s = "abab", p = "ab" | ||
// 输出: [0,1,2] | ||
// 解释: | ||
// 起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。 | ||
// 起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。 | ||
// 起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 1 <= s.length, p.length <= 3 * 10⁴ | ||
// s 和 p 仅包含小写字母 | ||
// | ||
// | ||
// Related Topics 哈希表 字符串 滑动窗口 👍 1372 👎 0 | ||
|
||
// package Top100Liked.leetcode.editor.cn; | ||
// public class FindAllAnagramsInAString{ | ||
// public static void main(String[] args) { | ||
// Solution solution = new FindAllAnagramsInAString().new Solution(); | ||
// } | ||
// //leetcode submit region begin(Prohibit modification and deletion) | ||
// class Solution { | ||
// public List<Integer> findAnagrams(String s, String p) { | ||
// | ||
// } | ||
// } | ||
//// leetcode submit region end(Prohibit modification and deletion) | ||
|
||
// } |
80 changes: 80 additions & 0 deletions
80
src/Top100Liked/leetcode/editor/cn/LongestSubstringWithoutRepeatingCharacters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
// 输入: s = "abcabcbb" | ||
// 输出: 3 | ||
// 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
// 输入: s = "bbbbb" | ||
// 输出: 1 | ||
// 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
// 输入: s = "pwwkew" | ||
// 输出: 3 | ||
// 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 | ||
// 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 0 <= s.length <= 5 * 10⁴ | ||
// s 由英文字母、数字、符号和空格组成 | ||
// | ||
// | ||
// Related Topics 哈希表 字符串 滑动窗口 👍 9940 👎 0 | ||
|
||
package Top100Liked.leetcode.editor.cn; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class LongestSubstringWithoutRepeatingCharacters { | ||
public static void main(String[] args) { | ||
Solution solution = new LongestSubstringWithoutRepeatingCharacters().new Solution(); | ||
System.out.println(solution.lengthOfLongestSubstring("dvdf")); | ||
} | ||
// leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
char[] charArray = s.toCharArray(); | ||
if (charArray.length == 0) { | ||
return 0; | ||
} else if (charArray.length == 1) { | ||
return 1; | ||
} else if (s.isBlank()) { | ||
return 1; | ||
} | ||
int result = 0, right = -1; | ||
Set<Character> mySet = new HashSet<>(); | ||
for (int i = 0; i < charArray.length; i++) { | ||
if (i != 0) { | ||
mySet.remove(charArray[i - 1]); | ||
} | ||
while (right < charArray.length - 1 && !mySet.contains(charArray[right + 1])) { | ||
mySet.add(charArray[right + 1]); | ||
right++; | ||
} | ||
result = Math.max(result, right - i + 1); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
// leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/Top100Liked/leetcode/editor/cn/SubarraySumEqualsK.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的子数组的个数 。 | ||
// | ||
// 子数组是数组中元素的连续非空序列。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
// 输入:nums = [1,1,1], k = 2 | ||
// 输出:2 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
// 输入:nums = [1,2,3], k = 3 | ||
// 输出:2 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 1 <= nums.length <= 2 * 10⁴ | ||
// -1000 <= nums[i] <= 1000 | ||
// -10⁷ <= k <= 10⁷ | ||
// | ||
// | ||
// Related Topics 数组 哈希表 前缀和 👍 2235 👎 0 | ||
|
||
// package Top100Liked.leetcode.editor.cn; | ||
// public class SubarraySumEqualsK{ | ||
// public static void main(String[] args) { | ||
// Solution solution = new SubarraySumEqualsK().new Solution(); | ||
// } | ||
// //leetcode submit region begin(Prohibit modification and deletion) | ||
// class Solution { | ||
// public int subarraySum(int[] nums, int k) { | ||
// | ||
// } | ||
// } | ||
//// leetcode submit region end(Prohibit modification and deletion) | ||
// | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// 给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != | ||
// k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请 | ||
// | ||
// 你返回所有和为 0 且不重复的三元组。 | ||
// | ||
// 注意:答案中不可以包含重复的三元组。 | ||
// | ||
// | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
// 输入:nums = [-1,0,1,2,-1,-4] | ||
// 输出:[[-1,-1,2],[-1,0,1]] | ||
// 解释: | ||
// nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。 | ||
// nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。 | ||
// nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。 | ||
// 不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。 | ||
// 注意,输出的顺序和三元组的顺序并不重要。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
// 输入:nums = [0,1,1] | ||
// 输出:[] | ||
// 解释:唯一可能的三元组和不为 0 。 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
// 输入:nums = [0,0,0] | ||
// 输出:[[0,0,0]] | ||
// 解释:唯一可能的三元组和为 0 。 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 3 <= nums.length <= 3000 | ||
// -10⁵ <= nums[i] <= 10⁵ | ||
// | ||
// | ||
// Related Topics 数组 双指针 排序 👍 6668 👎 0 | ||
|
||
package Top100Liked.leetcode.editor.cn; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ThreeSum { | ||
public static void main(String[] args) { | ||
Solution solution = new ThreeSum().new Solution(); | ||
System.out.println(solution.threeSum(new int[] {-1, 0, 1, 2, -1, -4})); | ||
} | ||
|
||
// leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
if (nums == null || nums.length < 3) { | ||
return List.of(); | ||
} | ||
Arrays.sort(nums); | ||
if (nums[0] > 0) { | ||
return List.of(); | ||
} else if (nums[0] == 0 && nums[1] == 0 && nums[2] == 0) { | ||
return List.of(List.of(0, 0, 0)); | ||
} | ||
List<List<Integer>> result = new ArrayList<>(); | ||
for (int i = 0; i < nums.length; i++) { | ||
if (nums[i] > 0) { | ||
break; | ||
} | ||
if (i > 0 && nums[i] == nums[i - 1]) { | ||
continue; | ||
} | ||
int left = i + 1, right = nums.length - 1; | ||
while (left < right) { | ||
if (nums[i] + nums[left] + nums[right] == 0) { | ||
result.add(Arrays.asList(nums[i], nums[left], nums[right])); | ||
while (left < right && nums[left] == nums[left + 1]) { | ||
left++; | ||
} | ||
while (left < right && nums[right] == nums[right - 1]) { | ||
right--; | ||
} | ||
left++; | ||
right--; | ||
} else if (nums[i] + nums[left] + nums[right] < 0) { | ||
left++; | ||
} else if (nums[i] + nums[left] + nums[right] > 0) { | ||
right--; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
// leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
// | ||
// | ||
// 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] | ||
// 输出:6 | ||
// 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
// 输入:height = [4,2,0,3,2,5] | ||
// 输出:9 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// n == height.length | ||
// 1 <= n <= 2 * 10⁴ | ||
// 0 <= height[i] <= 10⁵ | ||
// | ||
// | ||
// Related Topics 栈 数组 双指针 动态规划 单调栈 👍 4988 👎 0 | ||
|
||
package Top100Liked.leetcode.editor.cn; | ||
|
||
public class TrappingRainWater { | ||
public static void main(String[] args) { | ||
Solution solution = new TrappingRainWater().new Solution(); | ||
System.out.println(solution.trap(new int[] {4, 2, 0, 3, 2, 5})); | ||
} | ||
// leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public int trap(int[] height) { | ||
int length = height.length; | ||
if (length == 0) { | ||
return 0; | ||
} | ||
int[] leftMax = new int[length], rightMax = new int[length]; | ||
leftMax[0] = height[0]; | ||
for (int i = 1; i < length; i++) { | ||
leftMax[i] = Math.max(leftMax[i - 1], height[i]); | ||
} | ||
rightMax[length - 1] = height[length - 1]; | ||
for (int i = length - 2; i >= 0; i--) { | ||
rightMax[i] = Math.max(rightMax[i + 1], height[i]); | ||
} | ||
int result = 0; | ||
for (int i = 0; i < length; i++) { | ||
result += Math.min(leftMax[i], rightMax[i]) - height[i]; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
// leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/Top100Liked/leetcode/editor/cn/doc/content/FindAllAnagramsInAString.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<p>给定两个字符串 <code>s</code> 和 <code>p</code>,找到 <code>s</code><strong> </strong>中所有 <code>p</code><strong> </strong>的 <strong>异位词 </strong>的子串,返回这些子串的起始索引。不考虑答案输出的顺序。</p> | ||
|
||
<p><strong>异位词 </strong>指由相同字母重排列形成的字符串(包括相同的字符串)。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong>示例 1:</strong></p> | ||
|
||
<pre> | ||
<strong>输入: </strong>s = "cbaebabacd", p = "abc" | ||
<strong>输出: </strong>[0,6] | ||
<strong>解释:</strong> | ||
起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。 | ||
起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。 | ||
</pre> | ||
|
||
<p><strong> 示例 2:</strong></p> | ||
|
||
<pre> | ||
<strong>输入: </strong>s = "abab", p = "ab" | ||
<strong>输出: </strong>[0,1,2] | ||
<strong>解释:</strong> | ||
起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。 | ||
起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。 | ||
起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。 | ||
</pre> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code></li> | ||
<li><code>s</code> 和 <code>p</code> 仅包含小写字母</li> | ||
</ul> | ||
|
||
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>滑动窗口</li></div></div><br><div><li>👍 1372</li><li>👎 0</li></div> |
Oops, something went wrong.