Java实现 LeetCode 326 3的幂】的更多相关文章

326. 3的幂 给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输入: 45 输出: false 进阶: 你能不使用循环或者递归来完成本题吗? class Solution { public boolean isPowerOfThree(int n) { if (n == 0) { return false; } while (n % 3 ==…
给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输入: 45 输出: false 进阶: 你能不使用循环或者递归来完成本题吗? 思路 循环/3看最后是不是等于1就好了 代码 class Solution(object): def isPowerOfThree(self, n): """ :type n: int :rtype:…
342. 4的幂 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16 输出: true 示例 2: 输入: 5 输出: false 进阶: 你能不使用循环或者递归来完成本题吗? class Solution { public boolean isPowerOfFour(int num) { int x = 0x55555555; return (num > 0)&&((num&(num-1))==0)&((nu…
231. 2的幂 给定一个整数,编写一个函数来判断它是否是 2 的幂次方. 示例 1: 输入: 1 输出: true 解释: 20 = 1 示例 2: 输入: 16 输出: true 解释: 24 = 16 示例 3: 输入: 218 输出: false PS: 2的次幂和他的上一位数&的结果为0 8的二进制就是1000 7的二进制就是0111 结果========0000 class Solution { public boolean isPowerOfTwo(int n) { if(n <…
leetcode 326. Power of Three(不用循环或递归) Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? 题意是判断一个数是否是3的幂,最简单的也最容易想到的办法就是递归判断,或者循环除. 有另一种方法就是,求log以3为底n的对数.类似 如果n=9,则…
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1…
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaaa&qu…
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le…
Design a data structure that supports the following two operations: void addWord(word)bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.…
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l…