jaredliw 2021-07-23 21:19:29 +08:00
commit 1b247df1fd
5 changed files with 215 additions and 30 deletions

View File

@ -4,7 +4,7 @@
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
@ -87,7 +87,7 @@
********
> n = 1004 num = 10
> n = 1004 num = 10
0 ~ 9, 10 3 1 0 1 1 1 2 ~ 9
@ -139,7 +139,7 @@
3 4
### n = 1024
### n = 1024
** 1024 ** 1
@ -187,12 +187,12 @@ class Solution {
while (high != 0 || cur != 0) {
cur = high % 10;
high /= 10;
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
if (cur == 0) count += high * num;
else if (cur == 1) count += high * num + 1 + low;
else if (cur == 1) count += high * num + 1 + low;
else count += (high + 1) * num;
//低位
low = cur * num + low;
low = cur * num + low;
num *= 10;
}
return count;
@ -209,10 +209,10 @@ class Solution {
while high != 0 || cur != 0 {
cur = high % 10
high /= 10
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
if cur == 0 {
count += high * num
} else if cur == 1 {
} else if cur == 1 {
count += high * num + 1 + low
} else {
count += (high + 1) * num
@ -227,5 +227,37 @@ class Solution {
: O(logn) O(1)
C++ Code:
```C++
class Solution
{
public:
int countDigitOne(int n)
{
// 高位, 低位, 当前位
int high = n, low = 0, cur = 0;
int count = 0, num = 1;
//数字是0的时候完全没必要继续计算
while (high != 0)
{
cur = high % 10;
high /= 10;
//这里我们可以提出 high * num 因为我们发现无论为几,都含有它
if (cur == 0)
count += (high * num);
else if (cur == 1)
count += (high * num + 1 + low);
else
count += ((high + 1) * num);
//低位
low = cur * num + low;
//提前检查剩余数字, 以免溢出
if (high != 0)
num *= 10;
}
return count;
}
};
```

View File

@ -4,7 +4,7 @@
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [1052. ](https://leetcode-cn.com/problems/grumpy-bookstore-owner/)
@ -48,7 +48,7 @@ rightsum 是窗口右区间的值,和左区间加和方式一样。那么我
leftsum grumpy[i] == 1 grumpy[i] == 0
leftsum grumpy[i] == 1 grumpy[i] == 0
rightsum grumpy[i] == 0 rightsum -= grumpy[i]
@ -72,15 +72,15 @@ class Solution {
}
}
//窗口的值
for (int i = 0; i < X; ++i) {
winsum += customers[i];
for (int i = 0; i < X; ++i) {
winsum += customers[i];
}
int leftsum = 0;
//窗口左边缘
int left = 1;
//窗口右边缘
int right = X;
int maxcustomer = winsum + leftsum + rightsum;
int maxcustomer = winsum + leftsum + rightsum;
while (right < customers.length) {
//重新计算左区间的值,也可以用 customer 值和 grumpy 值相乘获得
if (grumpy[left-1] == 0) {
@ -97,7 +97,7 @@ class Solution {
//移动窗口
left++;
right++;
}
}
return maxcustomer;
}
}
@ -151,8 +151,54 @@ class Solution {
left += 1
right += 1
}
return maxCustomer
}
}
```
C++ Code
```C++
class Solution
{
public:
int maxSatisfied(vector<int> &customers, vector<int> &grumpy, int minutes)
{
for_each(grumpy.begin(), grumpy.end(), [](auto &g){ g = !g; });
vector<int> osum(customers.size(), 0);
//先初始化第一个元素
osum[0] = customers[0] * grumpy[0];
//计算前缀和, osum是origin sum
for (int i = 1; i < osum.size(); i++)
{
osum[i] = osum[i - 1] + customers[i] * grumpy[i];
}
//计算连续minutes的和
vector<int> msum(customers.size() - minutes + 1, 0);
for (int i = 0; i < minutes; i++)
{
msum[0] += customers[i];
}
for (int i = 1; i < msum.size(); i++)
{
msum[i] = msum[i - 1] - customers[i - 1] + customers[i + minutes - 1];
}
//分成三段计算
int result = 0;
for (int i = 0; i < msum.size(); i++)
{
//左 中 右
//注意左的边界条件, 可以使用边界测试
int sum = ((i - 1 >= 0) ? osum[i - 1] : 0) + msum[i] + osum[osum.size() - 1] - osum[i + minutes - 1];
if (sum > result)
result = sum;
}
return result;
}
};
```

View File

@ -2,7 +2,7 @@
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [41. ](https://leetcode-cn.com/problems/first-missing-positive/)
@ -53,15 +53,15 @@ class Solution {
for (int x : nums) {
if (x > 0 && x < res.length) {
res[x] = x;
}
}
}
//遍历查找,发现不一样时直接返回
for (int i = 1; i < res.length; i++) {
if (res[i] != i) {
return i;
}
}
}
//缺少最后一个,例如 123此时缺少 4 细节2
//缺少最后一个,例如 123此时缺少 4 细节2
return res.length;
}
}
@ -85,7 +85,7 @@ class Solution:
for i in range(1, len(res)):
if res[i] != i:
return i
# 123 4 2
# 123 4 2
return len(res)
```
@ -145,7 +145,7 @@ class Solution {
for (int i = 0; i < len; ++i) {
//需要考虑指针移动情况大于0小于len+1不等与i+1两个交换的数相等时防止死循环
while (nums[i] > 0 && nums[i] < len + 1 && nums[i] != i+1 && nums[i] != nums[nums[i]-1]) {
swap(nums,i,nums[i] - 1);
swap(nums,i,nums[i] - 1);
}
}
//遍历寻找缺失的正整数
@ -207,10 +207,10 @@ class Solution {
for i in 0..<len {
// 需要考虑指针移动情况大于0小于len+1不等与i+1
// 两个交换的数相等时,防止死循环
while nums[i] > 0
&& nums[i] < len + 1
while nums[i] > 0
&& nums[i] < len + 1
&& nums[i] != i + 1
&& nums[i] != nums[nums[i] - 1]
&& nums[i] != nums[nums[i] - 1]
{
//nums.swapAt(i, (nums[i] - 1)) // 系统方法
self.swap(&nums, i, (nums[i] - 1)) // 自定义方法
@ -232,3 +232,44 @@ class Solution {
}
}
```
C++ Code
```C++
class Solution
{
public:
int firstMissingPositive(vector<int> &nums)
{
int size = nums.size();
//判断范围是否符合要求
auto inRange = [](auto s, auto e)
{
return [s, e](auto &n)
{
return e >= n && n >= s;
};
};
auto cusInRange = inRange(1, size);
//增加数组长度, 便于计算, 不需要再转换
nums.push_back(0);
for (int i = 0; i < size; i++)
{
//将不在正确位置的元素放到正确位置上
while (cusInRange(nums[i]) && nums[i] != i && nums[nums[i]] != nums[i])
{
swap(nums[i], nums[nums[i]]);
}
}
//找出缺失的元素
for (int i = 1; i <= size; i++)
{
if (nums[i] != i)
return i;
}
return size + 1;
}
};
```

View File

@ -2,7 +2,7 @@
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [485. 1 ](https://leetcode-cn.com/problems/max-consecutive-ones/)
@ -16,7 +16,7 @@
100% , Math.max()使 right 1 1
right - left 1 right 0 right - left + 1
right - left 1 right 0 right - left + 1
使 while 0 1 left left = right
@ -125,7 +125,7 @@ class Solution {
int count = 0;
int maxcount = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 1) {
count++;
@ -172,8 +172,43 @@ class Solution {
maxCount = max(maxCount, count)
count = 0
}
}
}
return max(maxCount, count)
}
}
```
C++ Code
```C++
class Solution
{
public:
int findMaxConsecutiveOnes(vector<int> &nums)
{
int s = 0;
int e = 0;
int result = 0;
int size = nums.size();
while (s < size && e < size)
{
while (s < size && nums[s++] == 1)
{
e = s;
while (e < size && nums[e] == 1)
{
e++;
};
//注意需要加1, 可以使用极限条件测试
int r = e - s + 1;
if (r > result)
result = r;
s = e;
}
}
return result;
}
};
```

View File

@ -2,7 +2,7 @@
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
### [leetcode560. K](https://leetcode-cn.com/problems/subarray-sum-equals-k/)
@ -114,7 +114,7 @@ class Solution {
//一次遍历
for (int i = 0; i < nums.length; ++i) {
//存在时,我们用数组得值为 key索引为 value
if (map.containsKey(target - nums[i])){
if (map.containsKey(target - nums[i])){
return new int[]{i,map.get(target-nums[i])};
}
//存入值
@ -186,3 +186,34 @@ class Solution {
}
}
```
C++ Code
```C++
class Solution
{
public:
int subarraySum(vector<int> &nums, int k)
{
unordered_map<int, int> smp;
int sum = 0;
//初始化"最外面"的0
smp[0] = 1;
int result = 0;
for(int i = 0; i < nums.size(); i++)
{
sum += nums[i];
auto mp = smp.find(sum - k);
if (mp != smp.end())
{
//map里面存的一定是在前面的元素
//可以尝试将map的value换为数组
result += mp->second;
}
smp[sum]++;
}
return result;
}
};
```