zoukankan      html  css  js  c++  java
  • 每日算法

    每日算法

    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;
        }
    };
    
  • 相关阅读:
    大二软件工程概论自主学习(第十一周)
    课堂练习1
    数据库的学习
    界面作业
    怎样连接连接数据库
    异常处理
    继承与多态
    String课后作业
    请查看String.equals()方法的实现代码,注意学习其实现方法。
    课后作业
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12340699.html
Copyright © 2011-2022 走看看