326. 3 的幂(javascript)326. Power of Three

news/2025/2/22 16:23:14/

342. 4的幂(javascript)342. Power of Four

给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。

整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3^x

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3^x.

示例 1:

输入:n = 27
输出:true

示例 2:

输入:n = 0
输出:false

示例 3:

输入:n = 9
输出:true

示例 4:

输入:n = 45
输出:false

提示:

  • -231 <= n <= 231 - 1
var isPowerOfThree = function(n) {//0是特殊情况if(n===0)return falsewhile(n%3===0){n/=3}return n==1
};
var isPowerOfThree = function(n) {while(n!=0&&n%3===0){n/=3}return n==1
};

leetcode:https://leetcode-cn.com/problems/power-of-three/


http://www.ppmy.cn/news/545253.html

相关文章

Leetcode_单周赛_326

6278. 统计能整除数字的位数 代码 class Solution {public int countDigits(int num) {int ans 0;for (int i num; i > 0; i / 10) {if (num % (i % 10) 0) ans;}return ans;} }6279. 数组乘积中的不同质因数数目 代码1 因为单个数字最大是 1000&#xff0c;所以我们求…

周报326

美好的一周学习回顾 课程上&#xff1a; 这周是电装实习周&#xff0c;认真详细的学习了电烙铁的使用&#xff0c;测电表笔的使用&#xff0c;焊接最小系统单片机&#xff0c;同时又学习了电路实验&#xff0c;学习了面包板的使用&#xff0c;万用表的使用&#xff0c;使用这…

LeetCode_326. 3 的幂

目录 题目链接 思路分析 我的题解 题目链接 326. 3 的幂 思路分析 思路1&#xff1a;试除法 通过判断当前n是否是3的倍数来决定是否继续循环&#xff0c;如果不是&#xff0c;说明这个数一定不是3的幂&#xff1b;如果是&#xff0c;那么将此数除以3再继续循环&#xff0…

2021.3.26

现在有一个长方体材料&#xff0c;已经知道它的长a米、宽b米、高h米 和 密度 p千克/立方米&#xff0c;请计算这个长方体的质量&#xff08;千克&#xff09;。 输入格式: 输入a, b, h 和 p&#xff0c;空格分隔&#xff0c;数据保证输入的每个数都不超过100。 输出格式: 输出…

2020-3-26

选择题错两个 判断题错两个 最后一题错 一、选择题&#xff08;30分&#xff0c;每题2分&#xff09; 1、java的编程思想是什么&#xff1f;&#xff08;B&#xff09; A、 面向过程 B、 面向对象 C、 面向接口 D、 面向程序 2、如果我想编写java程序&#xff0c;那么我至少…

LC 326:3的幂

判断是否是3的幂次方 问题描述&#xff1a; 给定一个整数&#xff0c;写一个函数来判断它是否是 3 的幂次方。 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输入: 45 输出: false 解题思路&#xff1a; 找出数字 n 是否是数字…

326存储过程ld

-----------存储过程&#xff1a;将之前学的这些操作封装起来&#xff0c;作为用户的对象存储在数据库中&#xff0c;便于多次调用 --创建存储过程的语法结构 create or replace procedure 存储过程名[(参数1 [参数类型] 数据类型[,参数2 参数类型 数据类型,.....])] is/* / as…

LeetCode OJ 326. Power of Three

326. Power of Three Question Total Accepted: 1159 Total Submissions: 3275 Difficulty: Easy 判断给定整数是否是3的某次方。 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 / recursi…