Compare commits

...

15 Commits

Author SHA1 Message Date
算法基地
e8255d73ec
Merge pull request #17 from liyupi/patch-1
Update 荷兰国旗.md
2021-04-26 19:37:47 +08:00
程序员鱼皮
f51ee922d0
Update 荷兰国旗.md
add C++ Code
2021-04-26 17:17:19 +08:00
chefyuan
b470e3d12f Merge branch 'main' of https://github.com/chefyuan/algorithm-base into main 2021-04-26 15:31:39 +08:00
chefyuan
6264b97823 chefyuan 2021-04-26 15:24:01 +08:00
算法基地
a4f68eddab
Merge pull request #16 from azl397985856/patch-5
feat(ml): leetcode1052爱生气的书店老板 添加 Python3 Code
2021-04-26 15:11:32 +08:00
算法基地
9a7580e39b
Merge pull request #15 from azl397985856/patch-4
feat(ml): leetcode485最大连续1的个数 添加 Python3 Code
2021-04-26 15:09:37 +08:00
算法基地
5917f1c1c5
Merge pull request #14 from azl397985856/patch-3
feat(ml):leetcode41缺失的第一个正数 添加 Python3 代码
2021-04-26 15:07:02 +08:00
算法基地
df9a5805e9
Merge pull request #13 from azl397985856/patch-2
feat(ml): leetcode27移除元素 添加 Python3 代码
2021-04-26 15:04:26 +08:00
算法基地
c4a6b4f5d3
Merge pull request #12 from azl397985856/patch-1
feat(ml): leetcode1两数之和 添加 JS 和 CPP 代码
2021-04-26 14:58:34 +08:00
lucifer
5908cad71b
Update leetcode1两数之和.md 2021-04-26 14:55:44 +08:00
lucifer
ae8cfeb9bc
Update leetcode1052爱生气的书店老板.md 2021-04-26 14:54:12 +08:00
lucifer
c6060983ec
Update leetcode485最大连续1的个数.md 2021-04-26 14:52:03 +08:00
lucifer
2e003abb47
feat(ml):leetcode41缺失的第一个正数 添加 Python3 代码 2021-04-26 14:50:27 +08:00
lucifer
f5da40eb54
feat(ml): leetcode27移除元素 添加 Python3 代码 2021-04-26 14:48:29 +08:00
lucifer
9a8b4f47a4
Update leetcode1两数之和.md 2021-04-26 14:44:47 +08:00
7 changed files with 179 additions and 2 deletions

View File

@ -36,7 +36,7 @@
- 符合动画思想 - 符合动画思想
- 可以对代码进行简写难懂的地方注意添加注释因为我们的基地主要是为刚刷题的同学服务所以就尽量让大家容易理解一些 - 可以对代码进行简写难懂的地方注意添加注释因为我们的基地主要是为刚刷题的同学服务所以就尽量让大家容易理解一些
如果想要贡献代码的大佬可以添加我的微信 **tan45du_one** 备注贡献仓库即可 如果想要贡献代码的大佬可以添加我的微信 [**tan45du_one**](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io@master/个人微信.15egrcgqd94w.jpg) 备注贡献仓库即可
在这里先替所有使用仓库的同学谢谢各位贡献者啦 在这里先替所有使用仓库的同学谢谢各位贡献者啦
@ -203,7 +203,7 @@
- [C++程序喵大人 ](https://github.com/fightingwangzq/cpp-learning) by 帅强 - [C++程序喵大人 ](https://github.com/fightingwangzq/cpp-learning) by 帅强
- [编程资源](https://www.code-nav.cn) by 编程导航 - [编程资源](https://www.code-nav.cn) by 编程导航
- [Java知识地图](https://github.com/smileArchitect/JavaMap) by 帅小雷 - [Java知识地图](https://github.com/smileArchitect/JavaMap) by 帅小雷
- [腾讯云开发](https://github.com/liyupi) by [ - [腾讯云开发](https://github.com/liyupi) by
### 🍰数据库学习 ### 🍰数据库学习

View File

@ -72,6 +72,8 @@
下面我们直接看代码吧和三向切分基本一致 下面我们直接看代码吧和三向切分基本一致
Java Code
```java ```java
class Solution { class Solution {
public void sortColors(int[] nums) { public void sortColors(int[] nums) {
@ -99,6 +101,37 @@ class Solution {
} }
``` ```
C++ Code
```c++
class Solution {
public:
void sortColors(vector<int>& nums) {
int len = nums.size();
int left = 0;
//这里和三向切分不完全一致
int i = left;
int right = len-1;
while (i <= right) {
if (nums[i] == 2) {
swap(nums,i,right--);
} else if (nums[i] == 0) {
swap(nums,i++,left++);
} else {
i++;
}
}
}
void swap (vector<int>& nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
};
```
另外我们看这段代码有什么问题呢那就是我们即使完全符合时仍会交换元素这样会大大降低我们的效率 另外我们看这段代码有什么问题呢那就是我们即使完全符合时仍会交换元素这样会大大降低我们的效率
例如[0,0,0,1,1,1,2,2,2] 例如[0,0,0,1,1,1,2,2,2]
@ -117,6 +150,8 @@ class Solution {
另一种代码表示 另一种代码表示
Java Code
```java ```java
class Solution { class Solution {
public void sortColors(int[] nums) { public void sortColors(int[] nums) {
@ -148,5 +183,38 @@ class Solution {
} }
``` ```
C++ Code
```c++
class Solution {
public:
void sortColors(vector<int>& nums) {
int left = 0;
int len = nums.size();
int right = len - 1;
for (int i = 0; i <= right; ++i) {
if (nums[i] == 0) {
swap(nums,i,left);
left++;
}
if (nums[i] == 2) {
swap(nums,i,right);
right--;
//如果不等于 1 则需要继续判断所以不移动 i 指针i--
if (nums[i] != 1) {
i--;
}
}
}
}
void swap (vector<int>& nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
};
```
好啦这个问题到这就结束啦是不是很简单啊我们明天见 好啦这个问题到这就结束啦是不是很简单啊我们明天见

View File

@ -56,6 +56,8 @@ winsum 也会发生变化, winsum 需要加上新加入窗口的值,减去
好啦知道怎么做了我们直接开整吧 好啦知道怎么做了我们直接开整吧
Java Code:
```java ```java
class Solution { class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) { public int maxSatisfied(int[] customers, int[] grumpy, int X) {
@ -101,5 +103,16 @@ class Solution {
} }
``` ```
Python3 Code:
```py
class Solution:
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
t = ans = sum(customers[:X]) + sum(map(lambda x: customers[X+x[0]] if x[1] == 0 else 0, enumerate(grumpy[X:])))
for j in range(X, len(customers)):
t += customers[j] * grumpy[j] - customers[j-X] * grumpy[j-X]
ans = max(ans, t)
return ans
```

View File

@ -69,6 +69,8 @@ class Solution {
**题目代码** **题目代码**
Java Code:
```java ```java
class Solution { class Solution {
public int[] twoSum(int[] nums, int target) { public int[] twoSum(int[] nums, int target) {
@ -88,5 +90,37 @@ class Solution {
} }
``` ```
C++ Code:
```cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); ++i) {
int t = target - nums[i];
if (m.count(t)) return { m[t], i };
m[nums[i]] = i;
}
return {};
}
};
```
JS Code:
```js
const twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const diff = target - nums[i];
if (map.has(diff)) {
return [map.get(diff), i];
}
map.set(nums[i], i);
}
};
```

View File

@ -85,6 +85,8 @@ class Solution {
**题目代码** **题目代码**
Java Code:
```java ```java
class Solution { class Solution {
public int removeElement(int[] nums, int val) { public int removeElement(int[] nums, int val) {
@ -106,3 +108,16 @@ class Solution {
} }
``` ```
Python3 Code:
```py
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0
for j in range(len(nums)):
if nums[j] != val:
nums[i] = nums[j]
i += 1
return i
```

View File

@ -83,6 +83,8 @@ class Solution {
题目代码 题目代码
Java Code:
```java ```java
class Solution { class Solution {
public int firstMissingPositive(int[] nums) { public int firstMissingPositive(int[] nums) {
@ -115,3 +117,29 @@ class Solution {
} }
``` ```
Python3 Code:
```py
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
def swap(nums, a, b):
temp = nums[a]
nums[a] = nums[b]
nums[b] = temp
i = 0
while i < n:
num = nums[i]
# 已经就位
if num <= 0 or num >= n or num == i + 1 or nums[num - 1] == num:
i += 1
# 可以交换
else:
swap(nums, i, num - 1)
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1
```

View File

@ -66,6 +66,8 @@ class Solution {
好啦下面我们直接看代码吧 好啦下面我们直接看代码吧
Java Code:
```java ```java
class Solution { class Solution {
public int findMaxConsecutiveOnes(int[] nums) { public int findMaxConsecutiveOnes(int[] nums) {
@ -88,3 +90,20 @@ class Solution {
} }
``` ```
Python3 Code:
```py
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = i = t = 0
for j in range(len(nums)):
if nums[j] == 1:
t += 1
ans = max(ans, t)
else:
i = j + 1
t = 0
return ans
```