diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 380f6a59f18be..9cbadf57631dc 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,6 +1,7 @@ name: deploy on: + workflow_dispatch: push: branches: - main @@ -12,86 +13,118 @@ on: - lcof/** - lcci/** - basic/** - workflow_dispatch: - -env: - MKDOCS_API_KEYS: ${{ secrets.MKDOCS_API_KEYS }} - -permissions: - contents: write concurrency: - group: ${{github.workflow}} - ${{github.ref}} + group: ${{ github.workflow }} - ${{ github.ref }} cancel-in-progress: true jobs: - deploy: + build: runs-on: ubuntu-latest - if: github.repository == 'doocs/leetcode' steps: - - uses: actions/checkout@v4 - - uses: actions/checkout@v4 + - name: Checkout main branch + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Checkout docs branch + uses: actions/checkout@v4 with: ref: docs path: mkdocs - - run: | - mv -f mkdocs/* . + fetch-depth: 0 + + - name: Sync docs branch content + run: | + rsync -a --remove-source-files --exclude='.git' mkdocs/ ./ + rm -rf mkdocs mv solution/CONTEST_README.md docs/contest.md mv solution/CONTEST_README_EN.md docs-en/contest.md + - name: Configure Git Credentials run: | - git config user.name github-actions[bot] - git config user.email 41898282+github-actions[bot]@users.noreply.github.com + git config --global user.name github-actions[bot] + git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com - - uses: actions/setup-python@v5 + - name: Setup Python + uses: actions/setup-python@v5 with: python-version: 3.x - - run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV + - name: Restore pip cache + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- - - uses: actions/cache@v4 + - name: Restore mkdocs-material cache + uses: actions/cache@v4 with: - key: mkdocs-material-${{ env.cache_id }} path: .cache + key: mkdocs-material-${{ env.cache_id }} restore-keys: | mkdocs-material- - + - name: Install dependencies run: | python3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt python3 -m pip install "mkdocs-material[imaging]" - sudo apt-get install pngquant + sudo apt-get install -y pngquant + + - name: Set MKDOCS_API_KEYS + run: echo "MKDOCS_API_KEYS=${{ secrets.MKDOCS_API_KEYS }}" >> $GITHUB_ENV - - run: | + - name: Build site + run: | python3 main.py mkdocs build -f mkdocs.yml mkdocs build -f mkdocs-en.yml - - name: Deploy - uses: peaceiris/actions-gh-pages@v4 + - name: Generate CNAME + run: echo "leetcode.doocs.org" > ./site/CNAME + + - name: Commit committer cache to docs branch + if: github.ref == 'refs/heads/main' + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + CACHE_FILE=".git-committers-cache.json" + if [[ ! -f "$CACHE_FILE" ]]; then + echo "Cache file not found; skip commit." + exit 0 + fi + + echo "Cloning docs branch ..." + git clone --depth 1 --branch docs "https://x-access-token:${GH_TOKEN}@github.com/${GH_REPO}.git" docs-cache + cp "$CACHE_FILE" docs-cache/ + + cd docs-cache + git config user.name github-actions[bot] + git config user.email 41898282+github-actions[bot]@users.noreply.github.com + + git add .git-committers-cache.json + git commit -m "chore: update committer cache [skip ci]" || echo "No changes to commit" + git push origin docs + + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./site - cname: leetcode.doocs.org - - # sync: - # runs-on: ubuntu-latest - # needs: deploy - # if: github.repository == 'doocs/leetcode' - # steps: - # - name: Sync to gitee.com - # uses: wearerequired/git-mirror-action@master - # env: - # SSH_PRIVATE_KEY: ${{ secrets.RSA_PRIVATE_KEY }} - # with: - # source-repo: git@github.com:doocs/leetcode.git - # destination-repo: git@gitee.com:Doocs/leetcode.git - - # - name: Build Gitee Pages - # uses: yanglbme/gitee-pages-action@main - # with: - # gitee-username: yanglbme - # gitee-password: ${{ secrets.GITEE_PASSWORD }} - # gitee-repo: doocs/leetcode - # branch: gh-pages + path: ./site + + deploy: + needs: build + runs-on: ubuntu-latest + permissions: + pages: write + id-token: write + environment: + name: github_pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/.github/workflows/pr-add-label.yml b/.github/workflows/pr-add-label.yml new file mode 100644 index 0000000000000..82661e9eb237e --- /dev/null +++ b/.github/workflows/pr-add-label.yml @@ -0,0 +1,25 @@ +name: pr-add-label + +on: + pull_request_target: + types: [opened, edited, reopened, synchronize] + +jobs: + add-label: + permissions: + contents: read + pull-requests: write + runs-on: ubuntu-latest + if: github.repository == 'doocs/leetcode' + steps: + - name: Check PR number + id: pr_number + run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV + + - name: Run add-label Action + uses: actionv/pr-label-action@master + with: + github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }} + pr_number: ${{ env.PR_NUMBER }} + organize_name: "doocs" + team_name: "leetcode-algorithm" diff --git a/.gitignore b/.gitignore index e8ebbedd5ad1e..521323b59d92d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,10 +3,13 @@ .vscode .temp .vitepress -.cache *.iml __pycache__ /node_modules /solution/result.json /solution/__pycache__ /solution/.env +.cache +!.cache/plugin/ +!.cache/plugin/git-committers/ +!.cache/plugin/git-committers/page-authors.json \ No newline at end of file diff --git a/README.md b/README.md index 308efcb8b4cd7..aa8c9b92c5efa 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@
## 介绍 @@ -20,7 +21,7 @@ ## 站点 -https://leetcode.doocs.org +给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
-@@ -555,6 +555,48 @@ class Solution { } ``` +#### C + +```c +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +} +``` + diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index ffad07737163e..dba840d7bd8ee 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -551,6 +551,48 @@ class Solution { } ``` +#### C + +```c +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +} +``` + diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c new file mode 100644 index 0000000000000..e02c971a3a8c6 --- /dev/null +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.c @@ -0,0 +1,37 @@ +char* d[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; + +char** letterCombinations(char* digits, int* returnSize) { + if (!*digits) { + *returnSize = 0; + return NULL; + } + + int size = 1; + char** ans = (char**) malloc(sizeof(char*)); + ans[0] = strdup(""); + + for (int x = 0; digits[x]; ++x) { + char* s = d[digits[x] - '2']; + int len = strlen(s); + char** t = (char**) malloc(sizeof(char*) * size * len); + int tSize = 0; + + for (int i = 0; i < size; ++i) { + for (int j = 0; j < len; ++j) { + int oldLen = strlen(ans[i]); + char* tmp = (char*) malloc(oldLen + 2); + strcpy(tmp, ans[i]); + tmp[oldLen] = s[j]; + tmp[oldLen + 1] = '\0'; + t[tSize++] = tmp; + } + free(ans[i]); + } + free(ans); + ans = t; + size = tSize; + } + + *returnSize = size; + return ans; +} diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1200px-telephone-keypad2svg.png b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1200px-telephone-keypad2svg.png new file mode 100644 index 0000000000000..cbf69ae96598d Binary files /dev/null and b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1200px-telephone-keypad2svg.png differ diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1752723054-mfIHZs-image.png b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1752723054-mfIHZs-image.png new file mode 100644 index 0000000000000..e4d9a76bfe05c Binary files /dev/null and b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/1752723054-mfIHZs-image.png differ diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/17_telephone_keypad.png b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/17_telephone_keypad.png deleted file mode 100644 index ef7a0bffee3e2..0000000000000 Binary files a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/17_telephone_keypad.png and /dev/null differ diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-Telephone-keypad2.svg.png b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-Telephone-keypad2.svg.png deleted file mode 100644 index 38764028d1835..0000000000000 Binary files a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-Telephone-keypad2.svg.png and /dev/null differ diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-telephone-keypad2svg.png b/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-telephone-keypad2svg.png deleted file mode 100644 index 38764028d1835..0000000000000 Binary files a/solution/0000-0099/0017.Letter Combinations of a Phone Number/images/200px-telephone-keypad2svg.png and /dev/null differ diff --git a/solution/0000-0099/0020.Valid Parentheses/README.md b/solution/0000-0099/0020.Valid Parentheses/README.md index e4c364fe798a6..d09f9eda8a015 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README.md +++ b/solution/0000-0099/0020.Valid Parentheses/README.md @@ -61,6 +61,14 @@ tags:
输出:true
+示例 5:
+ +输入:s = "([)]"
+ +输出:false
+
提示:
diff --git a/solution/0000-0099/0020.Valid Parentheses/README_EN.md b/solution/0000-0099/0020.Valid Parentheses/README_EN.md index cf0fc8a16deba..24c94a545cabb 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0020.Valid Parentheses/README_EN.md @@ -60,6 +60,14 @@ tags:Output: true
+Example 5:
+ +Input: s = "([)]"
+ +Output: false
+
Constraints:
diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md index e7e07e00330c5..787342715e2e0 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md +++ b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md @@ -35,7 +35,7 @@ tags: 1->3->4, 2->6 ] -merging them into one sorted list: +merging them into one sorted linked list: 1->1->2->3->4->4->5->6 diff --git a/solution/0000-0099/0049.Group Anagrams/README.md b/solution/0000-0099/0049.Group Anagrams/README.md index 404f965482ca4..20255463c7d4d 100644 --- a/solution/0000-0099/0049.Group Anagrams/README.md +++ b/solution/0000-0099/0049.Group Anagrams/README.md @@ -19,30 +19,41 @@ tags: -给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
- -字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
+给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。
示例 1:
-
-输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
-输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
+输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
+ +输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
+ +解释:
+ +"bat"
。"nat"
和 "tan"
是字母异位词,因为它们可以重新排列以形成彼此。"ate"
,"eat"
和 "tea"
是字母异位词,因为它们可以重新排列以形成彼此。示例 2:
-
-输入: strs = [""]
-输出: [[""]]
-
+输入: strs = [""]
+ +输出: [[""]]
+示例 3:
-
-输入: strs = ["a"]
-输出: [["a"]]
+输入: strs = ["a"]
+ +输出: [["a"]]
+diff --git a/solution/0000-0099/0066.Plus One/README.md b/solution/0000-0099/0066.Plus One/README.md index 6c48f063bb6d3..a63a22c802037 100644 --- a/solution/0000-0099/0066.Plus One/README.md +++ b/solution/0000-0099/0066.Plus One/README.md @@ -17,11 +17,9 @@ tags: -
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。
+给定一个表示 大整数 的整数数组 digits
,其中 digits[i]
是整数的第 i
位数字。这些数字按从左到右,从最高位到最低位排列。这个大整数不包含任何前导 0
。
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
- -你可以假设除了整数 0 之外,这个整数不会以零开头。
+将大整数加 1,并返回结果的数字数组。
@@ -31,6 +29,8 @@ tags: 输入:digits = [1,2,3] 输出:[1,2,4] 解释:输入数组表示数字 123。 +加 1 后得到 123 + 1 = 124。 +因此,结果应该是 [1,2,4]。
示例 2:
@@ -39,6 +39,8 @@ tags: 输入:digits = [4,3,2,1] 输出:[4,3,2,2] 解释:输入数组表示数字 4321。 +加 1 后得到 4321 + 1 = 4322。 +因此,结果应该是 [4,3,2,2]。示例 3:
@@ -58,6 +60,7 @@ tags:1 <= digits.length <= 100
0 <= digits[i] <= 9
digits
不包含任何前导 0
。有效 二叉搜索树定义如下:
A valid BST is defined as follows:
给你两个字符串 s
和 t
,统计并返回在 s
的 子序列 中 t
出现的个数,结果需要对 109 + 7 取模。
给你两个字符串 s
和 t
,统计并返回在 s
的 子序列 中 t
出现的个数。
测试用例保证结果在 32 位有符号整数范围内。
diff --git a/solution/0100-0199/0118.Pascal's Triangle/README.md b/solution/0100-0199/0118.Pascal's Triangle/README.md index fd03f8d453b2d..d15d69697e1bf 100644 --- a/solution/0100-0199/0118.Pascal's Triangle/README.md +++ b/solution/0100-0199/0118.Pascal's Triangle/README.md @@ -57,7 +57,7 @@ tags: 我们先创建一个答案数组 $f$,然后将 $f$ 的第一行元素设为 $[1]$。接下来,我们从第二行开始,每一行的开头和结尾元素都是 $1$,其它 $f[i][j] = f[i - 1][j - 1] + f[i - 1][j]$。 -时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是行数。 +时间复杂度 $O(n^2)$,其中 $n$ 为给定的行数。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -83,8 +83,8 @@ class Solution { for (int i = 0; i < numRows - 1; ++i) { List
提示:
gas.length == n
cost.length == n
n == gas.length == cost.length
1 <= n <= 105
0 <= gas[i], cost[i] <= 104
n == gas.length == cost.length
1 <= n <= 105
0 <= gas[i], cost[i] <= 104
1
个糖果。请你给每个孩子分发糖果,计算并返回需要准备的 最少糖果数目 。
@@ -203,6 +203,35 @@ function candy(ratings: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn candy(ratings: Vec-
查询 Employee
表中第 n
高的工资。如果没有第 n
个最高工资,查询结果应该为 null
。
编写一个解决方案查询 Employee
表中第 n
高的 不同 工资。如果少于 n
个不同工资,查询结果应该为 null
。
查询结果格式如下所示。
diff --git a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md index 37c056f9d75ef..3373a1cd68980 100644 --- a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md +++ b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md @@ -31,7 +31,7 @@ Each row of this table contains information about the salary of an employee.-
Write a solution to find the nth
highest salary from the Employee
table. If there is no nth
highest salary, return null
.
Write a solution to find the nth
highest distinct salary from the Employee
table. If there are less than n
distinct salaries, return null
.
The result format is in the following example.
diff --git a/solution/0100-0199/0184.Department Highest Salary/README.md b/solution/0100-0199/0184.Department Highest Salary/README.md index f22679744afbb..fe36aca302cbb 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README.md +++ b/solution/0100-0199/0184.Department Highest Salary/README.md @@ -114,6 +114,31 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/README_EN.md b/solution/0100-0199/0184.Department Highest Salary/README_EN.md index 68aad979846ff..48725a31b1208 100644 --- a/solution/0100-0199/0184.Department Highest Salary/README_EN.md +++ b/solution/0100-0199/0184.Department Highest Salary/README_EN.md @@ -116,6 +116,31 @@ WHERE ); ``` +### Pandas + +```python +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result +``` + diff --git a/solution/0100-0199/0184.Department Highest Salary/Solution.py b/solution/0100-0199/0184.Department Highest Salary/Solution.py new file mode 100644 index 0000000000000..5b661d76ea904 --- /dev/null +++ b/solution/0100-0199/0184.Department Highest Salary/Solution.py @@ -0,0 +1,20 @@ +import pandas as pd + + +def department_highest_salary( + employee: pd.DataFrame, department: pd.DataFrame +) -> pd.DataFrame: + # Merge the two tables on departmentId and department id + merged = employee.merge(department, left_on='departmentId', right_on='id') + + # Find the maximum salary for each department + max_salaries = merged.groupby('departmentId')['salary'].transform('max') + + # Filter employees who have the highest salary in their department + top_earners = merged[merged['salary'] == max_salaries] + + # Select required columns and rename them + result = top_earners[['name_y', 'name_x', 'salary']].copy() + result.columns = ['Department', 'Employee', 'Salary'] + + return result diff --git a/solution/0100-0199/0190.Reverse Bits/README.md b/solution/0100-0199/0190.Reverse Bits/README.md index e151edf982a68..4538936385624 100644 --- a/solution/0100-0199/0190.Reverse Bits/README.md +++ b/solution/0100-0199/0190.Reverse Bits/README.md @@ -28,28 +28,67 @@ tags:-
示例 1:
- --输入:n = 00000010100101000001111010011100 -输出:964176192 (00111001011110000010100101000000) -解释:输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, - 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。- -
示例 2:
- --输入:n = 11111111111111111111111111111101 -输出:3221225471 (10111111111111111111111111111111) -解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293, - 因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111 。+
示例 1:
+ +输入:n = 43261596
+ +输出:964176192
+ +解释:
+ +整数 | +二进制 | +
---|---|
43261596 | +00000010100101000001111010011100 | +
964176192 | +00111001011110000010100101000000 | +
示例 2:
+ +输入:n = 2147483644
+ +输出:1073741822
+ +解释:
+ +整数 | +二进制 | +
---|---|
2147483644 | +01111111111111111111111111111100 | +
1073741822 | +00111111111111111111111111111110 | +
提示:
32
的二进制字符串0 <= n <= 231 - 2
n
为偶数diff --git a/solution/0100-0199/0190.Reverse Bits/README_EN.md b/solution/0100-0199/0190.Reverse Bits/README_EN.md index c9cb6f63778a4..3e84004956967 100644 --- a/solution/0100-0199/0190.Reverse Bits/README_EN.md +++ b/solution/0100-0199/0190.Reverse Bits/README_EN.md @@ -23,31 +23,70 @@ tags:
-3
and the output represents the signed integer -1073741825
.-3
and the output represents the signed integer -1073741825
.
Example 1:
--Input: n = 00000010100101000001111010011100 -Output: 964176192 (00111001011110000010100101000000) -Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000. -+
Input: n = 43261596
+ +Output: 964176192
+ +Explanation:
+ +Integer | +Binary | +
---|---|
43261596 | +00000010100101000001111010011100 | +
964176192 | +00111001011110000010100101000000 | +
Example 2:
--Input: n = 11111111111111111111111111111101 -Output: 3221225471 (10111111111111111111111111111111) -Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111. -+
Input: n = 2147483644
+ +Output: 1073741822
+ +Explanation:
+ +Integer | +Binary | +
---|---|
2147483644 | +01111111111111111111111111111100 | +
1073741822 | +00111111111111111111111111111110 | +
Constraints:
32
0 <= n <= 231 - 2
n
is even.diff --git a/solution/0100-0199/0195.Tenth Line/README_EN.md b/solution/0100-0199/0195.Tenth Line/README_EN.md index 7ca9f7187d210..69bdfb2395242 100644 --- a/solution/0100-0199/0195.Tenth Line/README_EN.md +++ b/solution/0100-0199/0195.Tenth Line/README_EN.md @@ -23,26 +23,41 @@ tags:
Assume that file.txt
has the following content:
+ Line 1 + Line 2 + Line 3 + Line 4 + Line 5 + Line 6 + Line 7 + Line 8 + Line 9 + Line 10 +
Your script should output the tenth line, which is:
+ Line 10 +
给定一个 无重复元素 的 有序 整数数组 nums
。
返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说,nums
的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 nums
的数字 x
。
区间 [a,b]
是从 a
到 b
(包含)的所有整数的集合。
返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表 。也就是说,nums
的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个区间但不属于 nums
的数字 x
。
列表中的每个区间范围 [a,b]
应该按如下格式输出:
给定一个二叉树,统计该二叉树数值相同的子树个数。
+给定一个二叉树,统计该二叉树数值相同的 子树 个数。
同值子树是指该子树的所有节点都拥有相同的数值。
-示例:
++
示例 1:
+-输入: root = [5,1,5,5,5,null,5] +输入:root = [5,1,5,5,5,null,5] +输出:4 +- 5 - / \ - 1 5 - / \ \ - 5 5 5 +
示例 2:
-输出: 4 ++输入:root = [] +输出:0+
示例 3:
+ ++输入:root = [5,5,5,5,5,null,5] +输出:6 ++ +
+ +
提示:
+ +[0, 1000]
范围内-1000 <= Node.val <= 1000
-
表:Users
-
取消率 的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)。
编写解决方案找出 "2013-10-01"
至 "2013-10-03"
期间有 至少 一次行程的非禁止用户(乘客和司机都必须未被禁止)的 取消率。非禁止用户即 banned 为 No 的用户,禁止用户即 banned 为 Yes 的用户。其中取消率 Cancellation Rate
需要四舍五入保留 两位小数 。
+
Table: Users
+
The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.
@@ -59,7 +59,7 @@ banned is an ENUM (category) type of ('Yes', 'No').Return the result table in any order.
-The result format is in the following example.
+The result format is in the following example.
Example 1:
diff --git a/solution/0200-0299/0269.Alien Dictionary/README.md b/solution/0200-0299/0269.Alien Dictionary/README.md index b1185f65b06a0..308a96369b8e8 100644 --- a/solution/0200-0299/0269.Alien Dictionary/README.md +++ b/solution/0200-0299/0269.Alien Dictionary/README.md @@ -288,6 +288,87 @@ public: }; ``` +#### Go + +```go +func alienOrder(words []string) string { + g := [26][26]bool{} + s := [26]bool{} + cnt := 0 + n := len(words) + for i := 0; i < n-1; i++ { + for _, c := range words[i] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + m := len(words[i]) + for j := 0; j < m; j++ { + if j >= len(words[i+1]) { + return "" + } + c1, c2 := words[i][j]-'a', words[i+1][j]-'a' + if c1 == c2 { + continue + } + if g[c2][c1] { + return "" + } + g[c1][c2] = true + break + } + } + for _, c := range words[n-1] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + + inDegree := [26]int{} + for _, out := range g { + for i, v := range out { + if v { + inDegree[i]++ + } + } + } + q := []int{} + for i, in := range inDegree { + if in == 0 && s[i] { + q = append(q, i) + } + } + ans := "" + for len(q) > 0 { + t := q[0] + q = q[1:] + ans += string(t + 'a') + for i, v := range g[t] { + if v { + inDegree[i]-- + if inDegree[i] == 0 && s[i] { + q = append(q, i) + } + } + } + } + if len(ans) < cnt { + return "" + } + return ans +} +``` + diff --git a/solution/0200-0299/0269.Alien Dictionary/README_EN.md b/solution/0200-0299/0269.Alien Dictionary/README_EN.md index 9677cf9f30970..24d8925dfb9a6 100644 --- a/solution/0200-0299/0269.Alien Dictionary/README_EN.md +++ b/solution/0200-0299/0269.Alien Dictionary/README_EN.md @@ -269,6 +269,87 @@ public: }; ``` +#### Go + +```go +func alienOrder(words []string) string { + g := [26][26]bool{} + s := [26]bool{} + cnt := 0 + n := len(words) + for i := 0; i < n-1; i++ { + for _, c := range words[i] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + m := len(words[i]) + for j := 0; j < m; j++ { + if j >= len(words[i+1]) { + return "" + } + c1, c2 := words[i][j]-'a', words[i+1][j]-'a' + if c1 == c2 { + continue + } + if g[c2][c1] { + return "" + } + g[c1][c2] = true + break + } + } + for _, c := range words[n-1] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + + inDegree := [26]int{} + for _, out := range g { + for i, v := range out { + if v { + inDegree[i]++ + } + } + } + q := []int{} + for i, in := range inDegree { + if in == 0 && s[i] { + q = append(q, i) + } + } + ans := "" + for len(q) > 0 { + t := q[0] + q = q[1:] + ans += string(t + 'a') + for i, v := range g[t] { + if v { + inDegree[i]-- + if inDegree[i] == 0 && s[i] { + q = append(q, i) + } + } + } + } + if len(ans) < cnt { + return "" + } + return ans +} +``` + diff --git a/solution/0200-0299/0269.Alien Dictionary/Solution.go b/solution/0200-0299/0269.Alien Dictionary/Solution.go new file mode 100644 index 0000000000000..b49abee4bad20 --- /dev/null +++ b/solution/0200-0299/0269.Alien Dictionary/Solution.go @@ -0,0 +1,76 @@ +func alienOrder(words []string) string { + g := [26][26]bool{} + s := [26]bool{} + cnt := 0 + n := len(words) + for i := 0; i < n-1; i++ { + for _, c := range words[i] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + m := len(words[i]) + for j := 0; j < m; j++ { + if j >= len(words[i+1]) { + return "" + } + c1, c2 := words[i][j]-'a', words[i+1][j]-'a' + if c1 == c2 { + continue + } + if g[c2][c1] { + return "" + } + g[c1][c2] = true + break + } + } + for _, c := range words[n-1] { + if cnt == 26 { + break + } + c -= 'a' + if !s[c] { + cnt++ + s[c] = true + } + } + + inDegree := [26]int{} + for _, out := range g { + for i, v := range out { + if v { + inDegree[i]++ + } + } + } + q := []int{} + for i, in := range inDegree { + if in == 0 && s[i] { + q = append(q, i) + } + } + ans := "" + for len(q) > 0 { + t := q[0] + q = q[1:] + ans += string(t + 'a') + for i, v := range g[t] { + if v { + inDegree[i]-- + if inDegree[i] == 0 && s[i] { + q = append(q, i) + } + } + } + } + if len(ans) < cnt { + return "" + } + return ans +} diff --git a/solution/0200-0299/0275.H-Index II/README.md b/solution/0200-0299/0275.H-Index II/README.md index 546a3242358e5..47b3a5e04dfb9 100644 --- a/solution/0200-0299/0275.H-Index II/README.md +++ b/solution/0200-0299/0275.H-Index II/README.md @@ -17,7 +17,7 @@ tags: -给你一个整数数组 citations
,其中 citations[i]
表示研究者的第 i
篇论文被引用的次数,citations
已经按照 升序排列 。计算并返回该研究者的 h 指数。
给你一个整数数组 citations
,其中 citations[i]
表示研究者的第 i
篇论文被引用的次数,citations
已经按照 非降序排列 。计算并返回该研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h
指数是指他(她)的 (n
篇论文中)至少 有 h
篇论文分别被引用了至少 h
次。
Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their ith
paper and citations
is sorted in ascending order, return the researcher's h-index.
Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their ith
paper and citations
is sorted in non-descending order, return the researcher's h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h
such that the given researcher has published at least h
papers that have each been cited at least h
times.
给定一个仅包含数字 0-9
的字符串 num
和一个目标值整数 target
,在 num
的数字之间添加 二元 运算符(不是一元)+
、-
或 *
,返回 所有 能够得到 target
的表达式。
给定一个仅包含数字 0-9
的字符串 num
和一个目标值整数 target
,在 num
的数字之间添加 二元 运算符(不是一元)+
、-
或 *
,返回 所有 能够得到 target
的表达式。
注意,返回表达式中的操作数 不应该 包含前导零。
+注意,一个数字可以包含多个数位。
+
示例 1:
diff --git a/solution/0200-0299/0282.Expression Add Operators/README_EN.md b/solution/0200-0299/0282.Expression Add Operators/README_EN.md index 083949924e34b..7b0d14c5eba93 100644 --- a/solution/0200-0299/0282.Expression Add Operators/README_EN.md +++ b/solution/0200-0299/0282.Expression Add Operators/README_EN.md @@ -22,6 +22,8 @@ tags:Note that operands in the returned expressions should not contain leading zeros.
+Note that a number can contain multiple digits.
+
Example 1:
diff --git a/solution/0300-0399/0317.Shortest Distance from All Buildings/README.md b/solution/0300-0399/0317.Shortest Distance from All Buildings/README.md index a41c44f4a91dc..b533ce8d0056f 100644 --- a/solution/0300-0399/0317.Shortest Distance from All Buildings/README.md +++ b/solution/0300-0399/0317.Shortest Distance from All Buildings/README.md @@ -32,8 +32,6 @@ tags:总旅行距离 是朋友们家到聚会地点的距离之和。
-使用 曼哈顿距离 计算距离,其中距离 (p1, p2) = |p2.x - p1.x | + | p2.y - p1.y |
。
示例 1:
diff --git a/solution/0300-0399/0317.Shortest Distance from All Buildings/README_EN.md b/solution/0300-0399/0317.Shortest Distance from All Buildings/README_EN.md index 816bbabef6c3d..edd540c52e03d 100644 --- a/solution/0300-0399/0317.Shortest Distance from All Buildings/README_EN.md +++ b/solution/0300-0399/0317.Shortest Distance from All Buildings/README_EN.md @@ -32,8 +32,6 @@ tags:The total travel distance is the sum of the distances between the houses of the friends and the meeting point.
-The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|
.
Example 1:
给定单链表的头节点 head
,将所有索引为奇数的节点和索引为偶数的节点分别组合在一起,然后返回重新排序的列表。
给定单链表的头节点 head
,将所有索引为奇数的节点和索引为偶数的节点分别分组,保持它们原有的相对顺序,然后把偶数索引节点分组连接到奇数索引节点分组之后,返回重新排序的链表。
第一个节点的索引被认为是 奇数 , 第二个节点的索引为 偶数 ,以此类推。
diff --git a/solution/0300-0399/0348.Design Tic-Tac-Toe/README.md b/solution/0300-0399/0348.Design Tic-Tac-Toe/README.md index c3bdea5faf274..a9b134fb52acd 100644 --- a/solution/0300-0399/0348.Design Tic-Tac-Toe/README.md +++ b/solution/0300-0399/0348.Design Tic-Tac-Toe/README.md @@ -20,7 +20,7 @@ tags: -请在 n × n 的棋盘上,实现一个判定井字棋(Tic-Tac-Toe)胜负的神器,判断每一次玩家落子后,是否有胜出的玩家。
+请在 n × n 的棋盘上,实现一个判定井字棋(Tic-Tac-Toe)胜负的神器,判断每一次玩家落子后,是否有胜出的玩家。
在这个井字棋游戏中,会有 2 名玩家,他们将轮流在棋盘上放置自己的棋子。
@@ -34,7 +34,8 @@ tags:示例:
-给定棋盘边长 n = 3, 玩家 1 的棋子符号是 "X",玩家 2 的棋子符号是 "O"。 ++给定棋盘边长 n = 3, 玩家 1 的棋子符号是 "X",玩家 2 的棋子符号是 "O"。 TicTacToe toe = new TicTacToe(3); @@ -76,7 +77,17 @@ toe.move(2, 1, 1); -> 函数返回 1 (此时,玩家 1 赢得了该场比赛-
进阶:
+提示:
+ +
2 <= n <= 100
1
或 2
。0 <= row, col < n
move
时 (row, col)
都是不同的。move
n2
次。进阶:
您有没有可能将每一步的 move()
操作优化到比 O(n2) 更快吗?
给你一个整数 n
,找出从 1
到 n
各个整数的 Fizz Buzz 表示,并用字符串数组 answer
(下标从 1 开始)返回结果,其中:
给你一个整数 n
,返回一个字符串数组 answer
(下标从 1 开始),其中:
answer[i] == "FizzBuzz"
如果 i
同时是 3
和 5
的倍数。示例 3:
-输入:sentence = ["I", "had", "apple", "pie"], rows = 4, cols = 5 +输入:sentence = ["i", "had", "apple", "pie"], rows = 4, cols = 5 输出:1 解释: -I-had +i-had apple -pie-I +pie-i had-- 字符 '-' 表示屏幕上的一个空白位置。diff --git a/solution/0400-0499/0427.Construct Quad Tree/README.md b/solution/0400-0499/0427.Construct Quad Tree/README.md index 561c58bac14f4..7afa044e1460a 100644 --- a/solution/0400-0499/0427.Construct Quad Tree/README.md +++ b/solution/0400-0499/0427.Construct Quad Tree/README.md @@ -50,7 +50,7 @@ class Node {
如果你想了解更多关于四叉树的内容,可以参考 wiki 。
+如果你想了解更多关于四叉树的内容,可以参考 百科 。
四叉树格式:
diff --git a/solution/0400-0499/0435.Non-overlapping Intervals/README.md b/solution/0400-0499/0435.Non-overlapping Intervals/README.md index d11d9c230875c..239271f1d1323 100644 --- a/solution/0400-0499/0435.Non-overlapping Intervals/README.md +++ b/solution/0400-0499/0435.Non-overlapping Intervals/README.md @@ -65,9 +65,18 @@ tags: -### 方法一:转换为最长上升子序列问题 +### 方法一:排序 + 贪心 -最长上升子序列问题,动态规划的做法,时间复杂度是 $O(n^2)$,这里可以采用贪心优化,将复杂度降至 $O(n\log n)$。 +我们首先将区间按照右边界升序排序,用一个变量 $\textit{pre}$ 记录上一个区间的右边界,用一个变量 $\textit{ans}$ 记录需要移除的区间数量,初始时 $\textit{ans} = \textit{intervals.length}$。 + +然后遍历区间,对于每一个区间: + +- 若当前区间的左边界大于等于 $\textit{pre}$,说明该区间无需移除,直接更新 $\textit{pre}$ 为当前区间的右边界,然后将 $\textit{ans}$ 减一; +- 否则,说明该区间需要移除,不需要更新 $\textit{pre}$ 和 $\textit{ans}$。 + +最后返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为区间的数量。 @@ -77,12 +86,12 @@ tags: class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x: x[1]) - ans, t = 0, intervals[0][1] - for s, e in intervals[1:]: - if s >= t: - t = e - else: - ans += 1 + ans = len(intervals) + pre = -inf + for l, r in intervals: + if pre <= l: + ans -= 1 + pre = r return ans ``` @@ -91,13 +100,14 @@ class Solution: ```java class Solution { public int eraseOverlapIntervals(int[][] intervals) { - Arrays.sort(intervals, Comparator.comparingInt(a -> a[1])); - int t = intervals[0][1], ans = 0; - for (int i = 1; i < intervals.length; ++i) { - if (intervals[i][0] >= t) { - t = intervals[i][1]; - } else { - ++ans; + Arrays.sort(intervals, (a, b) -> a[1] - b[1]); + int ans = intervals.length; + int pre = Integer.MIN_VALUE; + for (var e : intervals) { + int l = e[0], r = e[1]; + if (pre <= l) { + --ans; + pre = r; } } return ans; @@ -111,13 +121,17 @@ class Solution { class Solution { public: int eraseOverlapIntervals(vectorThere is a ball in a maze
with empty spaces (represented as 0
) and walls (represented as 1
). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.
There is a ball in a maze
with empty spaces (represented as 0
) and walls (represented as 1
). The ball can go through the empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction (must be different from last chosen direction). There is also a hole in this maze. The ball will drop into the hole if it rolls onto the hole.
Given the m x n
maze
, the ball's position ball
and the hole's position hole
, where ball = [ballrow, ballcol]
and hole = [holerow, holecol]
, return a string instructions
of all the instructions that the ball should follow to drop in the hole with the shortest distance possible. If there are multiple valid instructions, return the lexicographically minimum one. If the ball can't drop in the hole, return "impossible"
.
"USA"
。"leetcode"
。"Google"
。"leetcode"
。"Google"
。给你一个字符串 word
。如果大写用法正确,返回 true
;否则,返回 false
。
- -
编写解决方案,报告在首次登录的第二天再次登录的玩家的 比率,四舍五入到小数点后两位。换句话说,你需要计算从首次登录日期开始至少连续两天登录的玩家的数量,然后除以玩家总数。
+编写解决方案,报告在首次登录的第二天再次登录的玩家的 比率,四舍五入到小数点后两位。换句话说,你需要计算从首次登录后的第二天登录的玩家数量,并将其除以总玩家数。
结果格式如下所示:
diff --git a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md index 75774152565b6..3612f647d84ed 100644 --- a/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md +++ b/solution/0500-0599/0550.Game Play Analysis IV/README_EN.md @@ -32,11 +32,11 @@ This table shows the activity of players of some games. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device. -+
-
Write a solution to report the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players.
+Write a solution to report the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to determine the number of players who logged in on the day immediately following their initial login, and divide it by the number of total players.
-The result format is in the following example.
+The result format is in the following example.
Example 1:
diff --git a/solution/0500-0599/0568.Maximum Vacation Days/README.md b/solution/0500-0599/0568.Maximum Vacation Days/README.md index 0b759e9bf93c8..c482a1c70a038 100644 --- a/solution/0500-0599/0568.Maximum Vacation Days/README.md +++ b/solution/0500-0599/0568.Maximum Vacation Days/README.md @@ -98,7 +98,7 @@ Ans = 7 + 7 + 7 = 21 我们定义 $f[k][j]$ 表示前 $k$ 周,且最后一周在城市 $j$ 休假的最长天数。初始时 $f[0][0]=0$,其它 $f[0][j]=-\infty$。答案为 $\max_{j=0}^{n-1} f[K][j]$。 -接下来,我们考虑如何计算 $f[k][j]$。对于当前这一周,我们可以枚举上一周所在的城市 $i$,城市 $i$ 可以和城市 $j$ 相等,那么 $f[k][j] = f[k-1][i]$;也可以和城市 $j$ 不相等,如果不相等,我们需要判断是否可以从城市 $i$ 飞到城市 $j$,如果可以,那么 $f[k][j] = max(f[k][j], f[k-1][i])$。最后,我们还需要加上这一周在城市 $j$ 休假的天数 $days[j][k-1]$。 +接下来,我们考虑如何计算 $f[k][j]$。对于当前这一周,我们可以枚举上一周所在的城市 $i$,城市 $i$ 可以和城市 $j$ 相等,那么 $f[k][j] = f[k-1][i]$;也可以和城市 $j$ 不相等,如果不相等,我们需要判断是否可以从城市 $i$ 飞到城市 $j$,如果可以,那么 $f[k][j] = \max(f[k][j], f[k-1][i])$。最后,我们还需要加上这一周在城市 $j$ 休假的天数 $\textit{days}[j][k-1]$。 最终的答案即为 $\max_{j=0}^{n-1} f[K][j]$。 @@ -220,6 +220,59 @@ func maxVacationDays(flights [][]int, days [][]int) (ans int) { } ``` +#### TypeScript + +```ts +function maxVacationDays(flights: number[][], days: number[][]): number { + const n = flights.length; + const K = days[0].length; + const inf = Number.NEGATIVE_INFINITY; + const f: number[][] = Array.from({ length: K + 1 }, () => Array(n).fill(inf)); + f[0][0] = 0; + for (let k = 1; k <= K; k++) { + for (let j = 0; j < n; j++) { + f[k][j] = f[k - 1][j]; + for (let i = 0; i < n; i++) { + if (flights[i][j]) { + f[k][j] = Math.max(f[k][j], f[k - 1][i]); + } + } + f[k][j] += days[j][k - 1]; + } + } + return Math.max(...f[K]); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_vacation_days(flights: Vec找出那些 没有被 id = 2
的客户 推荐 的客户的姓名。
找出以下客户的姓名:
+ +id != 2
的用户推荐。以 任意顺序 返回结果表。
diff --git a/solution/0500-0599/0584.Find Customer Referee/README_EN.md b/solution/0500-0599/0584.Find Customer Referee/README_EN.md index d6810684bb34e..0bd8a5c948aff 100644 --- a/solution/0500-0599/0584.Find Customer Referee/README_EN.md +++ b/solution/0500-0599/0584.Find Customer Referee/README_EN.md @@ -32,7 +32,12 @@ Each row of this table indicates the id of a customer, their name, and the id of-
Find the names of the customer that are not referred by the customer with id = 2
.
Find the names of the customer that are either:
+ +id != 2
.Return the result table in any order.
diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md index 06b75b6a92e88..faf66f120bb69 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md @@ -174,6 +174,28 @@ function findLHS(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn find_lhs(nums: Vec假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
+给定两个字符串数组 list1
和 list2
,找到 索引和最小的公共字符串。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设答案总是存在。
+公共字符串 是同时出现在 list1
和 list2
中的字符串。
具有 最小索引和的公共字符串 是指,如果它在 list1[i]
和 list2[j]
中出现,那么 i + j
应该是所有其他 公共字符串 中的最小值。
返回所有 具有最小索引和的公共字符串。以 任何顺序 返回答案。
@@ -29,7 +33,7 @@ tags:
输入: list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] 输出: ["Shogun"] -解释: 他们唯一共同喜爱的餐厅是“Shogun”。 +解释: 唯一的公共字符串是 “Shogun”。
示例 2:
@@ -37,9 +41,20 @@ tags:输入:list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["KFC", "Shogun", "Burger King"] 输出: ["Shogun"] -解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。 +解释: 具有最小索引和的公共字符串是 “Shogun”,它有最小的索引和 = (0 + 1) = 1。+
示例 3:
+ ++输入:list1 = ["happy","sad","good"], list2 = ["sad","happy","good"] +输出:["sad","happy"] +解释:有三个公共字符串: +"happy" 索引和 = (0 + 1) = 1. +"sad" 索引和 = (1 + 0) = 1. +"good" 索引和 = (2 + 2) = 4. +最小索引和的字符串是 "sad" 和 "happy"。+
提示:
diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/README.md b/solution/0600-0699/0604.Design Compressed String Iterator/README.md index 9d8866ead6b51..49ed7490b6fa8 100644 --- a/solution/0600-0699/0604.Design Compressed String Iterator/README.md +++ b/solution/0600-0699/0604.Design Compressed String Iterator/README.md @@ -267,6 +267,53 @@ func (this *StringIterator) HasNext() bool { */ ``` +#### TypeScript + +```ts +class StringIterator { + private d: [string, number][] = []; + private p: number = 0; + + constructor(compressedString: string) { + const n = compressedString.length; + let i = 0; + while (i < n) { + const c = compressedString[i]; + let x = 0; + i++; + while (i < n && !isNaN(Number(compressedString[i]))) { + x = x * 10 + Number(compressedString[i]); + i++; + } + this.d.push([c, x]); + } + } + + next(): string { + if (!this.hasNext()) { + return ' '; + } + const ans = this.d[this.p][0]; + this.d[this.p][1]--; + if (this.d[this.p][1] === 0) { + this.p++; + } + return ans; + } + + hasNext(): boolean { + return this.p < this.d.length && this.d[this.p][1] > 0; + } +} + +/** + * Your StringIterator object will be instantiated and called as such: + * var obj = new StringIterator(compressedString) + * var param_1 = obj.next() + * var param_2 = obj.hasNext() + */ +``` + diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md b/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md index cc5512a0be612..b5c23f018d70e 100644 --- a/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md +++ b/solution/0600-0699/0604.Design Compressed String Iterator/README_EN.md @@ -67,7 +67,13 @@ stringIterator.hasNext(); // return True -### Solution 1 +### Solution 1: Parsing and Storing + +Parse the `compressedString` into characters $c$ and their corresponding repetition counts $x$, and store them in an array or list $d$. Use $p$ to point to the current character. + +Then perform operations in `next` and `hasNext`. + +The initialization time complexity is $O(n)$, and the time complexity of the other operations is $O(1)$. Here, $n$ is the length of `compressedString`. @@ -260,6 +266,53 @@ func (this *StringIterator) HasNext() bool { */ ``` +#### TypeScript + +```ts +class StringIterator { + private d: [string, number][] = []; + private p: number = 0; + + constructor(compressedString: string) { + const n = compressedString.length; + let i = 0; + while (i < n) { + const c = compressedString[i]; + let x = 0; + i++; + while (i < n && !isNaN(Number(compressedString[i]))) { + x = x * 10 + Number(compressedString[i]); + i++; + } + this.d.push([c, x]); + } + } + + next(): string { + if (!this.hasNext()) { + return ' '; + } + const ans = this.d[this.p][0]; + this.d[this.p][1]--; + if (this.d[this.p][1] === 0) { + this.p++; + } + return ans; + } + + hasNext(): boolean { + return this.p < this.d.length && this.d[this.p][1] > 0; + } +} + +/** + * Your StringIterator object will be instantiated and called as such: + * var obj = new StringIterator(compressedString) + * var param_1 = obj.next() + * var param_2 = obj.hasNext() + */ +``` + diff --git a/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts b/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts new file mode 100644 index 0000000000000..6313052a68374 --- /dev/null +++ b/solution/0600-0699/0604.Design Compressed String Iterator/Solution.ts @@ -0,0 +1,42 @@ +class StringIterator { + private d: [string, number][] = []; + private p: number = 0; + + constructor(compressedString: string) { + const n = compressedString.length; + let i = 0; + while (i < n) { + const c = compressedString[i]; + let x = 0; + i++; + while (i < n && !isNaN(Number(compressedString[i]))) { + x = x * 10 + Number(compressedString[i]); + i++; + } + this.d.push([c, x]); + } + } + + next(): string { + if (!this.hasNext()) { + return ' '; + } + const ans = this.d[this.p][0]; + this.d[this.p][1]--; + if (this.d[this.p][1] === 0) { + this.p++; + } + return ans; + } + + hasNext(): boolean { + return this.p < this.d.length && this.d[this.p][1] > 0; + } +} + +/** + * Your StringIterator object will be instantiated and called as such: + * var obj = new StringIterator(compressedString) + * var param_1 = obj.next() + * var param_2 = obj.hasNext() + */ diff --git a/solution/0600-0699/0630.Course Schedule III/README.md b/solution/0600-0699/0630.Course Schedule III/README.md index 6375a17781381..0892c7c3ea024 100644 --- a/solution/0600-0699/0630.Course Schedule III/README.md +++ b/solution/0600-0699/0630.Course Schedule III/README.md @@ -179,13 +179,13 @@ func (h *hp) pop() int { return heap.Pop(h).(int) } ```ts function scheduleCourse(courses: number[][]): number { courses.sort((a, b) => a[1] - b[1]); - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue1 <= n <= 100
1 <= logs.length <= 500
2 <= logs.length <= 500
0 <= function_id < n
0 <= timestamp <= 109
1 <= n <= 100
1 <= logs.length <= 500
2 <= logs.length <= 500
0 <= function_id < n
0 <= timestamp <= 109