diff --git a/README.md b/README.md index 3fa3e96db9017..ffb1159ee98f7 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ ## 维护者 -[Yang Libin](https://github.com/yanglbme): GitHub / Gitee 技术社区 @Doocs 创建者;开源组织 [@TheAlgorithms](https://github.com/TheAlgorithms) 核心成员之一。 +[Yang Libin](https://github.com/yanglbme): GitHub / Gitee 技术社区 @Doocs 创建者;[@TheAlgorithms](https://github.com/TheAlgorithms) 组织成员。 ## 加入我们 @@ -165,8 +165,8 @@ 如果你对本项目感兴趣,并且希望加入我们刷题大军,欢迎随时提交 [PR](https://github.com/doocs/leetcode/pulls)。请参考如下步骤: -1. 将本项目 fork 到你的个人 GitHub / Gitee 帐户,然后 clone 到你的本地机器; -1. 对项目做出一些变更,然后使用 git add、commit、push 等命令将你的变更提交到你的远程 GitHub / Gitee 仓库; +1. 将本项目 fork 到你的个人 GitHub / Gitee 帐户,然后 clone 到你的本地机器; +1. 对项目做出一些变更,然后使用 git addcommitpush 等命令将你的变更提交到你的远程 GitHub / Gitee 仓库; 1. 将你的变更以 PR 的形式提交过来,项目的维护人员会在第一时间对你的变更进行 review! 1. 你也可以参考帮助文档「[GitHub](https://help.github.com/cn) / [Gitee](https://gitee.com/help)」了解更多细节。 diff --git a/README_EN.md b/README_EN.md index e1578e21194cb..c32fe7cb5323a 100644 --- a/README_EN.md +++ b/README_EN.md @@ -154,8 +154,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF I'm looking for long-term contributors/partners to this repo! Send me [PRs](https://github.com/doocs/leetcode/pulls) if you're interested! See the following: -1. Fork [this repository](https://github.com/doocs/leetcode) to your own GitHub account and then clone it to your local machine. -1. Make some changes to your leetcode repository, then push the changes to your remote GitHub repository. +1. Fork [this repository](https://github.com/doocs/leetcode) to your own GitHub account and then clone it to your local machine. +1. Make some changes to your leetcode repository, then add, commit and push the changes to your remote GitHub repository. 1. Create a pull request with your changes! 1. See [CONTRIBUTING](https://github.com/doocs/.github/blob/main/CONTRIBUTING.md) or [GitHub Help](https://help.github.com/en) for more details. diff --git a/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.cpp b/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.cpp new file mode 100644 index 0000000000000..e205eb64923d2 --- /dev/null +++ b/solution/0000-0099/0026.Remove Duplicates from Sorted Array/Solution.cpp @@ -0,0 +1,20 @@ +/** + * Author: Moriarty12138 + */ +class Solution { +public: + int removeDuplicates(vector& nums) { + int n = nums.size(); + if(n < 2) { + return n; + } + + int idx = 0; + for(int n : nums) { + if(idx < 1 || nums[idx-1] < n) { + nums[idx++] = n; + } + } + return idx; + } +}; diff --git a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp index 5246179ad3841..8e0f2857b74ef 100644 --- a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp +++ b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp @@ -4,14 +4,14 @@ class Solution { if(nums.empty())return 0; size_t len = nums.size(); if(len == 1)return 1; - + auto iter = nums.begin(); iter++; int k = 1; while(iter != nums.end()){ if(*iter == *(iter-1))k++; else k = 1; - + if(k==3){ nums.erase(iter); k--; @@ -20,8 +20,27 @@ class Solution { iter++; } } - + len = nums.size(); return len; } -}; \ No newline at end of file +}; + +/** + * Author: Moriarty12138 + */ + class Solution { + public: + int removeDuplicates(vector& nums) { + if(nums.size() < 3) { + nums.size(); + } + int ans = 0; + for(int n : nums) { + if(ans < 2 || nums[ans - 2] < n) { + nums[ans++] = n; + } + } + return ans; + } + }; diff --git a/solution/1500-1599/1507.Reformat Date/README.md b/solution/1500-1599/1507.Reformat Date/README.md index c3a4d46b9bf09..ae4d64af2f900 100644 --- a/solution/1500-1599/1507.Reformat Date/README.md +++ b/solution/1500-1599/1507.Reformat Date/README.md @@ -53,8 +53,6 @@ -切分 `date` 字符串,获取对应的 `year`, `month`, `day`,然后拼接起来即可。 - ### **Python3** @@ -62,15 +60,7 @@ ```python -class Solution: - def reformatDate(self, date: str) -> str: - months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - mapper = {v: str(k + 1) for k, v in enumerate(months)} - split = date.split(' ') - year = split[2] - month = mapper.get(split[1]) - day = split[0][:len(split[0]) - 2] - return year + '-' + month.zfill(2) + '-' + day.zfill(2) + ``` ### **Java** @@ -78,20 +68,7 @@ class Solution: ```java -class Solution { - public String reformatDate(String date) { - Map mapper = new HashMap<>(); - String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - for (int i = 0; i < months.length; ++i) { - mapper.put(months[i], i + 1); - } - String[] split = date.split(" "); - int year = Integer.parseInt(split[2]); - int month = mapper.get(split[1]); - int day = Integer.parseInt(split[0].substring(0, split[0].length() -2)); - return String.format("%d-%02d-%02d", year, month, day); - } -} + ``` ### **...** diff --git a/solution/1500-1599/1507.Reformat Date/README_EN.md b/solution/1500-1599/1507.Reformat Date/README_EN.md index 9b3935f701dd4..c46cbc884734b 100644 --- a/solution/1500-1599/1507.Reformat Date/README_EN.md +++ b/solution/1500-1599/1507.Reformat Date/README_EN.md @@ -56,34 +56,13 @@ ### **Python3** ```python -class Solution: - def reformatDate(self, date: str) -> str: - months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - mapper = {v: str(k + 1) for k, v in enumerate(months)} - split = date.split(' ') - year = split[2] - month = mapper.get(split[1]) - day = split[0][:len(split[0]) - 2] - return year + '-' + month.zfill(2) + '-' + day.zfill(2) + ``` ### **Java** ```java -class Solution { - public String reformatDate(String date) { - Map mapper = new HashMap<>(); - String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - for (int i = 0; i < months.length; ++i) { - mapper.put(months[i], i + 1); - } - String[] split = date.split(" "); - int year = Integer.parseInt(split[2]); - int month = mapper.get(split[1]); - int day = Integer.parseInt(split[0].substring(0, split[0].length() -2)); - return String.format("%d-%02d-%02d", year, month, day); - } -} + ``` ### **...** diff --git a/solution/1500-1599/1507.Reformat Date/Solution.java b/solution/1500-1599/1507.Reformat Date/Solution.java deleted file mode 100644 index 54814dfdfc67a..0000000000000 --- a/solution/1500-1599/1507.Reformat Date/Solution.java +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { - public String reformatDate(String date) { - Map mapper = new HashMap<>(); - String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; - for (int i = 0; i < months.length; ++i) { - mapper.put(months[i], i + 1); - } - String[] split = date.split(" "); - int year = Integer.parseInt(split[2]); - int month = mapper.get(split[1]); - int day = Integer.parseInt(split[0].substring(0, split[0].length() -2)); - return String.format("%d-%02d-%02d", year, month, day); - } -} \ No newline at end of file diff --git a/solution/1500-1599/1507.Reformat Date/Solution.py b/solution/1500-1599/1507.Reformat Date/Solution.py deleted file mode 100644 index a5d3867900d4a..0000000000000 --- a/solution/1500-1599/1507.Reformat Date/Solution.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def reformatDate(self, date: str) -> str: - months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] - mapper = {v: str(k + 1) for k, v in enumerate(months)} - split = date.split(' ') - year = split[2] - month = mapper.get(split[1]) - day = split[0][:len(split[0]) - 2] - return year + '-' + month.zfill(2) + '-' + day.zfill(2)