mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 13:03:41 +00:00
Compare commits
15 Commits
107732a6c8
...
e8255d73ec
Author | SHA1 | Date | |
---|---|---|---|
|
e8255d73ec | ||
|
f51ee922d0 | ||
|
b470e3d12f | ||
|
6264b97823 | ||
|
a4f68eddab | ||
|
9a7580e39b | ||
|
5917f1c1c5 | ||
|
df9a5805e9 | ||
|
c4a6b4f5d3 | ||
|
5908cad71b | ||
|
ae8cfeb9bc | ||
|
c6060983ec | ||
|
2e003abb47 | ||
|
f5da40eb54 | ||
|
9a8b4f47a4 |
@ -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 帅强
|
||||
- [编程资源](https://www.code-nav.cn) by 编程导航
|
||||
- [Java知识地图](https://github.com/smileArchitect/JavaMap) by 帅小雷
|
||||
- [腾讯云开发](https://github.com/liyupi) by [皮
|
||||
- [腾讯云开发](https://github.com/liyupi) by 鱼皮
|
||||
|
||||
### 🍰数据库学习
|
||||
|
||||
|
@ -72,6 +72,8 @@
|
||||
|
||||
下面我们直接看代码吧,和三向切分基本一致。
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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]
|
||||
@ -117,6 +150,8 @@ class Solution {
|
||||
|
||||
另一种代码表示
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
好啦,这个问题到这就结束啦,是不是很简单啊,我们明天见!
|
||||
|
||||
|
@ -56,6 +56,8 @@ winsum 也会发生变化, winsum 需要加上新加入窗口的值,减去
|
||||
|
||||
好啦,知道怎么做了,我们直接开整吧。
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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
|
||||
```
|
||||
|
||||
|
||||
|
@ -69,6 +69,8 @@ class Solution {
|
||||
|
||||
**题目代码:**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -85,6 +85,8 @@ class Solution {
|
||||
|
||||
**题目代码:**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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
|
||||
```
|
||||
|
||||
|
@ -83,6 +83,8 @@ class Solution {
|
||||
|
||||
题目代码:
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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
|
||||
```
|
||||
|
||||
|
@ -66,6 +66,8 @@ class Solution {
|
||||
|
||||
好啦,下面我们直接看代码吧。
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
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
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user