Merge pull request #36 from caibingcheng/main

[update] #3 数组篇添加C++ Code
This commit is contained in:
算法基地 2021-07-23 16:33:43 +08:00 committed by GitHub
commit 5c7f983aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 215 additions and 30 deletions

View File

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

@ -156,3 +156,49 @@ class Solution {
}
}
```
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

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

@ -177,3 +177,38 @@ class Solution {
}
}
```
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

@ -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;
}
};
```