Skip to content

Commit

Permalink
feat(Leetcode Top 100): Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
taowong committed Jan 29, 2024
0 parents commit d92a39e
Show file tree
Hide file tree
Showing 12 changed files with 805 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: update_readme

on:
workflow_dispatch:
issues:
types: [ opened, edited, labeled, unlabeled ]
issue_comment:
types: [ created, edited ]
push:
branches:
- master
paths:
- main.py

jobs:
update-readme:
runs-on: ubuntu-latest
if: github.repository_owner_id == github.event.issue.user.id || github.event_name == 'push'
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests PyGithub
- name: Get issue data and update README.md
run: python main.py --issue_number '${{ github.event.issue.number }}'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Push readme
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add backup/*.md
git commit -a -m 'update new record' || echo "nothing to commit"
git push || echo "nothing to push"
69 changes: 69 additions & 0 deletions src/Top100Liked/leetcode/editor/cn/ContainerWithMostWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
//
// 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
//
// 返回容器可以储存的最大水量。
//
// 说明:你不能倾斜容器。
//
//
//
// 示例 1:
//
//
//
//
//输入:[1,8,6,2,5,4,8,3,7]
//输出:49
//解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
//
// 示例 2:
//
//
//输入:height = [1,1]
//输出:1
//
//
//
//
// 提示:
//
//
// n == height.length
// 2 <= n <= 10⁵
// 0 <= height[i] <= 10⁴
//
//
// Related Topics 贪心 数组 双指针 👍 4806 👎 0


package Top100Liked.leetcode.editor.cn;

public class ContainerWithMostWater {
public static void main(String[] args) {
Solution solution = new ContainerWithMostWater().new Solution();
System.out.println(solution.maxArea(new int[]{1,8,6,2,5,4,8,3,7}));
}

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int maxArea(int[] height) {
int left = 0, right = height.length - 1, result = 0;
for (int i = 0; i < height.length; i++) {
int temp_result = Math.min(height[left], height[right]) * (right - left);
result = Math.max(temp_result, result);
if (height[left] > height[right]) {
right--;
} else if (height[left] <= height[right]) {
left++;
}
if (left == right) {
break;
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)

}
87 changes: 87 additions & 0 deletions src/Top100Liked/leetcode/editor/cn/GroupAnagrams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
//
// 字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
//
//
//
// 示例 1:
//
//
//输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
//输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
//
// 示例 2:
//
//
//输入: strs = [""]
//输出: [[""]]
//
//
// 示例 3:
//
//
//输入: strs = ["a"]
//输出: [["a"]]
//
//
//
// 提示:
//
//
// 1 <= strs.length <= 10⁴
// 0 <= strs[i].length <= 100
// strs[i] 仅包含小写字母
//
//
// Related Topics 数组 哈希表 字符串 排序 👍 1807 👎 0


package Top100Liked.leetcode.editor.cn;

import java.util.*;

public class GroupAnagrams {
public static void main(String[] args) {
Solution solution = new GroupAnagrams().new Solution();
System.out.println(solution.groupAnagrams(new String[]{"a"}));

}

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0) {
return List.of(new ArrayList<String>());
} else if (strs.length == 1 && Objects.equals(strs[0], "")) {
return List.of(List.of(""));
}
HashMap<String, List<String>> hashMap = new HashMap<>();
for (String str : strs) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
String key = Arrays.toString(charArray);
List<String> stringList = hashMap.getOrDefault(key, new ArrayList<>());
stringList.add(str);
hashMap.put(key, stringList);
}
return new ArrayList<>(hashMap.values());
}
}

//leetcode submit region end(Prohibit modification and deletion)
class Solution1 {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (String str : strs) {
char[] array = str.toCharArray();
Arrays.sort(array);
String key = new String(array);
List<String> list = map.getOrDefault(key, new ArrayList<String>());
list.add(str);
map.put(key, list);
}
return new ArrayList<List<String>>(map.values());
}
}

}
97 changes: 97 additions & 0 deletions src/Top100Liked/leetcode/editor/cn/LongestConsecutiveSequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
//
// 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
//
//
//
// 示例 1:
//
//
//输入:nums = [100,4,200,1,3,2]
//输出:4
//解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
//
// 示例 2:
//
//
//输入:nums = [0,3,7,2,5,8,4,6,0,1]
//输出:9
//
//
//
//
// 提示:
//
//
// 0 <= nums.length <= 10⁵
// -10⁹ <= nums[i] <= 10⁹
//
//
// Related Topics 并查集 数组 哈希表 👍 1961 👎 0


package Top100Liked.leetcode.editor.cn;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class LongestConsecutiveSequence {
public static void main(String[] args) {
Solution solution = new LongestConsecutiveSequence().new Solution();
System.out.println(solution.longestConsecutive(new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1}));
}

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> hashSet = new HashSet<>();
for (int num : nums) {
hashSet.add(num);
}
int result = 0;
for (Integer i : hashSet) {
int cur = i;
if (hashSet.contains(cur - 1)) {
continue;
} else {
while (hashSet.contains(cur + 1)) {
cur++;
}
}
result = Math.max(result, cur - i + 1);
}
return result;
}
}

//leetcode submit region end(Prohibit modification and deletion)
class Solution1 {
public int longestConsecutive(int[] nums) {
// key表示num,value表示num最远到达的连续右边界
Map<Integer, Integer> map = new HashMap<>();
// 初始化每个num的右边界为自己
for (int num : nums) {
map.put(num, num);
}

int ans = 0;
for (int num : nums) {
if (!map.containsKey(num - 1)) {
int right = map.get(num);
// 遍历得到最远的右边界
while (map.containsKey(right + 1)) {
right = map.get(right + 1);
}
// 更新右边界
map.put(num, right);
// 更新答案
ans = Math.max(ans, right - num + 1);
}

}
return ans;
}
}

}
66 changes: 66 additions & 0 deletions src/Top100Liked/leetcode/editor/cn/MoveZeroes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
//
// 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
//
//
//
// 示例 1:
//
//
//输入: nums = [0,1,0,3,12]
//输出: [1,3,12,0,0]
//
//
// 示例 2:
//
//
//输入: nums = [0]
//输出: [0]
//
//
//
// 提示:
//
//
//
// 1 <= nums.length <= 10⁴
// -2³¹ <= nums[i] <= 2³¹ - 1
//
//
//
//
// 进阶:你能尽量减少完成的操作次数吗?
//
// Related Topics 数组 双指针 👍 2294 👎 0


package Top100Liked.leetcode.editor.cn;

import java.util.Arrays;

public class MoveZeroes {
public static void main(String[] args) {
Solution solution = new MoveZeroes().new Solution();
int[] nums = {0, 1, 0, 3, 12};
solution.moveZeroes(nums);
System.out.println(Arrays.toString(nums));

}

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public void moveZeroes(int[] nums) {
int left = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[left++] = nums[i];
}
}
for (int i = left; i < nums.length; i++) {
nums[i] = 0;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)

}
Loading

0 comments on commit d92a39e

Please sign in to comment.