Skip to content

Commit b470e3d

Browse files
committed
Merge branch 'main' of https://github.com/chefyuan/algorithm-base into main
2 parents 6264b97 + a4f68ed commit b470e3d

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

animation-simulation/数组篇/leetcode1052爱生气的书店老板.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ winsum 也会发生变化, winsum 需要加上新加入窗口的值,减去
5656

5757
好啦,知道怎么做了,我们直接开整吧。
5858

59+
Java Code:
60+
5961
```java
6062
class Solution {
6163
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
@@ -101,5 +103,16 @@ class Solution {
101103
}
102104
```
103105

106+
Python3 Code:
107+
108+
```py
109+
class Solution:
110+
def maxSatisfied(self, customers: List[int], grumpy: List[int], X: int) -> int:
111+
t = ans = sum(customers[:X]) + sum(map(lambda x: customers[X+x[0]] if x[1] == 0 else 0, enumerate(grumpy[X:])))
112+
for j in range(X, len(customers)):
113+
t += customers[j] * grumpy[j] - customers[j-X] * grumpy[j-X]
114+
ans = max(ans, t)
115+
return ans
116+
```
104117

105118

animation-simulation/数组篇/leetcode1两数之和.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Solution {
6969

7070
**题目代码:**
7171

72+
Java Code:
73+
7274
```java
7375
class Solution {
7476
public int[] twoSum(int[] nums, int target) {
@@ -88,5 +90,37 @@ class Solution {
8890
}
8991
```
9092

93+
C++ Code:
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<int> twoSum(vector<int>& nums, int target) {
99+
unordered_map<int, int> m;
100+
for (int i = 0; i < nums.size(); ++i) {
101+
int t = target - nums[i];
102+
if (m.count(t)) return { m[t], i };
103+
m[nums[i]] = i;
104+
}
105+
return {};
106+
}
107+
};
108+
```
109+
110+
JS Code:
111+
112+
```js
113+
const twoSum = function (nums, target) {
114+
const map = new Map();
115+
for (let i = 0; i < nums.length; i++) {
116+
const diff = target - nums[i];
117+
if (map.has(diff)) {
118+
return [map.get(diff), i];
119+
}
120+
map.set(nums[i], i);
121+
}
122+
};
123+
```
124+
91125

92126

animation-simulation/数组篇/leetcode27移除元素.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class Solution {
8585

8686
**题目代码:**
8787

88+
Java Code:
89+
8890
```java
8991
class Solution {
9092
public int removeElement(int[] nums, int val) {
@@ -106,3 +108,16 @@ class Solution {
106108
}
107109
```
108110

111+
Python3 Code:
112+
113+
```py
114+
class Solution:
115+
def removeElement(self, nums: List[int], val: int) -> int:
116+
i = 0
117+
for j in range(len(nums)):
118+
if nums[j] != val:
119+
nums[i] = nums[j]
120+
i += 1
121+
return i
122+
```
123+

animation-simulation/数组篇/leetcode41缺失的第一个正数.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class Solution {
8383

8484
题目代码:
8585

86+
Java Code:
87+
8688
```java
8789
class Solution {
8890
public int firstMissingPositive(int[] nums) {
@@ -115,3 +117,29 @@ class Solution {
115117
}
116118
```
117119

120+
Python3 Code:
121+
122+
```py
123+
class Solution:
124+
def firstMissingPositive(self, nums: List[int]) -> int:
125+
n = len(nums)
126+
127+
def swap(nums, a, b):
128+
temp = nums[a]
129+
nums[a] = nums[b]
130+
nums[b] = temp
131+
i = 0
132+
while i < n:
133+
num = nums[i]
134+
# 已经就位
135+
if num <= 0 or num >= n or num == i + 1 or nums[num - 1] == num:
136+
i += 1
137+
# 可以交换
138+
else:
139+
swap(nums, i, num - 1)
140+
for i in range(n):
141+
if nums[i] != i + 1:
142+
return i + 1
143+
return n + 1
144+
```
145+

animation-simulation/数组篇/leetcode485最大连续1的个数.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class Solution {
6666

6767
好啦,下面我们直接看代码吧。
6868

69+
Java Code:
70+
6971
```java
7072
class Solution {
7173
public int findMaxConsecutiveOnes(int[] nums) {
@@ -88,3 +90,20 @@ class Solution {
8890
}
8991
```
9092

93+
Python3 Code:
94+
95+
96+
```py
97+
class Solution:
98+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
99+
ans = i = t = 0
100+
for j in range(len(nums)):
101+
if nums[j] == 1:
102+
t += 1
103+
ans = max(ans, t)
104+
else:
105+
i = j + 1
106+
t = 0
107+
return ans
108+
```
109+

0 commit comments

Comments
 (0)