【Power of Two】cpp

题目:

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

代码:

class Solution {
public:
    bool isPowerOfTwo(int n) {
            if ( n<=0 ) return false;
            int countOne = 0;
            for ( int i=0; i<sizeof(n)*8 && countOne<=1; ++i )
            {
                countOne += (n>>i) & 1;
            }
            return countOne==1;
    }
};

tips:

首先的思路是bit-munipulation

检查int中有多少个1

学习了一个更简洁的(https://leetcode.com/discuss/45017/5-lines-o-1-space%26time-c-solution-no-hash

自己优化了一下 一行代码AC。

class Solution {
public:
    bool isPowerOfTwo(int n) {
            return  n<=0 ? false : !(n & (n-1));
    }
};
时间: 2024-08-28 06:58:15

【Power of Two】cpp的相关文章

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f

【Longest Consecutive Sequence】cpp

题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run

【Linked List Cycle】cpp

题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 代码: 用hashmap版 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next

【Spiral Matrix II】cpp

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 代码: class Solution { public: vector<vector<

【Minimum Path Sum】cpp

题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 代码: class Solution { public:

【Restore IP Addresses 】cpp

题目: Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 代码: class So

【First Missing Positive】cpp

题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 代码: class Solution { public: int firstMissingPos