每日算法 – day 10

每日算法

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

原文链接: https://www.cnblogs.com/wlw-x/p/12340699.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    每日算法 - day 10

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/330884

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午5:47
下一篇 2023年3月1日 下午5:48

相关推荐