leetcode---Power of Two---lower_bound,upper_bound,binary_search

来源:互联网 发布:环迅网络兼职是真的吗 编辑:程序博客网 时间:2024/06/05 05:55

Given an integer, write a function to determine if it is a power of two.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

class Solution {public:    vector<int> powTwo;    bool isPowerOfTwo(int n) {        vector<int> powTwo;            int i;        for(i=0; i<31; i++)        {            int a = (1 << i);            powTwo.push_back(a);        }        vector<int>::iterator l, r;        l = lower_bound(powTwo.begin(), powTwo.end(), n);        r = upper_bound(powTwo.begin(), powTwo.end(), n);        if(l == r-1)            return true;        else            return false;    }};
class Solution {public:    vector<int> powTwo;    bool isPowerOfTwo(int n) {        vector<int> powTwo;            int i;        for(i=0; i<31; i++)        {            int a = (1 << i);            powTwo.push_back(a);        }        int m = binary_search(powTwo.begin(), powTwo.end(), n);        if(m == 1)            return true;        else            return false;    }};
0 0
原创粉丝点击