做出来三个
第一题:
Leetcode 1012. Complement of Base 10 Integer
题目:
Every non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation.
The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of “101” in binary is “010” in binary.
For a given number N in base-10, return the complement of it’s binary representation as a base-10 integer.
题意:就是给一个数求它的异或。
解析:得知道一个数有多少位,logn即可,所以复杂度O(logn)
代码:
class Solution
{
public:
int bitwiseComplement(int N)
{
if(N==0) return 1;
int temp = N;
int cnt = 0;
while(temp)
{
temp >>= 1;
++cnt;
}
int ans = pow(2,cnt)-1;
return ans^N;
}
};
第二题:
Leetcode 1013. Pairs of Songs With Total Durations Divisible by 60
In a list of songs, the i-th song has a duration of time[i] seconds.
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
Example 1:
Input: [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
Example 2:
Input: [60,60,60]
Output: 3
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
Note:
1 <= time.length <= 60000
1 <= time[i] <= 500
题意:就是找多少对可以凑足60的倍数。
解析:
其实可以哈希,存储每个数多少个,然后在存储60的倍数,然后两个循环去试即可。
代码:
class Solution
{
public:
int numPairsDivisibleBy60(vector<int>& time)
{
vector<int> vec;
int ans = 0;
int size1 = time.size();
sort(time.begin(), time.end());
map<int,int> mp;
for(int i = 0 ;i<size1;i++)
{
int val =time[i];
mp[val]++;
}
for(int i = 60; i<=1000;i+=60) vec.push_back(i);
int size2 = vec.size();
for(int i = 0; i < size1; i++)
{
for(int j=0;j < size2; j++)
{
int l = time[i];
int r = vec[j]-l;
if(r<=0||l>r) continue;
else if(l==r&&((2*l)%60==0))
{
for(int k = i+1; k<size1;k++)
{
if(time[k]==time[i])
{
// cout << i <<" " << k <<endl;
++ans;
}
}
}
else
if(mp[r]) ans += mp[r];
}
}
return ans;
}
};
Leetcode 1014. Capacity To Ship Packages Within D Days
题目:
A conveyor belt has packages that must be shipped from one port to another within D days.
The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
Example 1:
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
Output: 15
Explanation:
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
Example 2:
Input: weights = [3,2,2,4,1,4], D = 3
Output: 6
Explanation:
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4
Example 3:
Input: weights = [1,2,3,1,1], D = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1
Note:
1 <= D <= weights.length <= 50000
1 <= weights[i] <= 500
题意:就是有n个袋子,然后得依靠传送带传送,求传送带至少承受重量为多少才可以在D天运完所有袋子。
其实就是二分,左边界为袋子的最大值,右边界为所有的重量。记得应该在右边界保存答案。
代码:
class Solution
{
public:
bool Judge(vector<int> &weights, int D, int mid)
{
int cnt = 1;
int pre = 0;
for(int i = 0; i<weights.size();)
{
pre += weights[i];
if(pre>mid)
{
cnt++;
pre = 0;
i--;
}
i++;
}
if(cnt<=D) return true;
return false;
}
int shipWithinDays(vector<int>& weights, int D)
{
// sort(weights.begin(),weights.end());
int size = weights.size();
int l,r,mid;
r = 0;
l = -1;
for(int i = 0; i < size; i++)
{
l = max(l,weights[i]);
r += weights[i];
}
int ans;
while(l<=r)
{
int mid = (l+r)>>1;
if(Judge(weights,D,mid))
{
ans = mid;
r = mid-1;
}
else l = mid+1;
}
return ans;
}
};