This commit is contained in:
chefyuan 2021-04-26 15:31:39 +08:00
commit b470e3d12f
5 changed files with 109 additions and 0 deletions

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
```