mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 21:06:17 +00:00
Merge branch 'main' of https://github.com/chefyuan/algorithm-base into main
This commit is contained in:
commit
b470e3d12f
@ -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