Leetcode【231】Power

image description

Leetcode【231】Power

写在最前面:

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

Example 1:

Input: 1 Output: true Explanation: 20 = 1 Example 2:

Input: 16 Output: true Explanation: 24 = 16 Example 3:

Input: 218 Output: false

   
class Solution { public boolean isPowerOfTwo(int n) { return n > 0 && ((n & (n - 1)) == 0 ); } }

通过&判断,&是指两位判断,如果都为1则为1,否则为0。

    ArithmeticJia         0         2764         Java         123    

Related Posts

You may like these post too

image description

leetcode【53】最大子序和

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 来源:力扣(LeetCode)

image description

leetcode【59】螺旋矩阵

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 来源:力扣(LeetCode) 语言:java

Comments on this post

0 comments

Leave a comment

it’s easy to post a comment

image description
image description
image description
image description
image description
image description
image description
image description
image description

Copyright © 2019.Company name All rights reserved.苏ICP备19007197号