每日算法
those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.2.20
记录下来自己做题时得思路,并不一定是最优解
167. 两数之和 II - 输入有序数组
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> ans;
for(int i = 0;i < numbers.size(); i ++)
{
int x = numbers[i];
int l = 0,r = numbers.size() - 1;
while(l < r)
{
int mid = l + r >> 1;
if(numbers[mid] + x >= target)r = mid;
else l = mid + 1;
}
if(numbers[l] + x == target && i != l){
if(l > i){
ans.push_back(i+1);ans.push_back(l+1);
}else {
ans.push_back(l+1);ans.push_back(i+1);
}
break;
}
}
return ans;
}
};
278. 第一个错误的版本
二分 找到第一个为true得数字
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
long long l = 0, r = n;
while(l < r)
{
long long mid = l + r >> 1;
if(isBadVersion(mid) == true)r = mid;
else l = mid + 1;
}
return l;
}
};