C#LeetCode刷题之#326-3的幂(Power of Three)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3867 访问。
给定一个整数,写一个函数来判断它是否是 3 的幂次方。
输入: 27
输出: true
输入: 0
输出: false
输入: 9
输出: true
输入: 45
输出: false
进阶:你能不使用循环或者递归来完成本题吗?
Given an integer, write a function to determine if it is a power of three.
Input: 27
Output: true
Input: 0
Output: false
Input: 9
Output: true
Input: 45
Output: false
Follow up:Could you do it without using any loop / recursion?
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3867 访问。
public class Program {public static void Main(string[] args) {var n = 25;var res = IsPowerOfThree(n);Console.WriteLine(res);n = 16;res = IsPowerOfThree2(n);Console.WriteLine(res);n = 27;res = IsPowerOfThree3(n);Console.WriteLine(res);Console.ReadKey();}private static bool IsPowerOfThree(int n) {//先看原值是否能被3整除//若不能整除,不是3的幂;//若能整除,继续往下,直接<=1时为止//最后判断值是否为1即可while(n % 3 == 0 && (n /= 3) > 1) { }return n == 1;}private static bool IsPowerOfThree2(int n) {//负数肯定不满足题意if(n <= 0) return false;//找到整型范围内最大的3的幂//var maxPower = (int)Math.Pow(3, (int)(Math.Log10(int.MaxValue) / Math.Log10(3)));var maxPower = 1162261467;//这个值是否能被n整除return maxPower % n == 0;}private static bool IsPowerOfThree3(int n) {//基于高中数学的一些技巧var x = Math.Log10(n) / Math.Log10(3);//这种解法有点赖皮,仅给大家一些思路吧//IDE给出的警告请无视return (int)x - x == 0;}}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3867 访问。
False
False
True
分析:
显而易见,IsPowerOfThree 的时间复杂度为: ,IsPowerOfThree2 和IsPowerOfThree3 的时间复杂度为: 。
C#LeetCode刷题之#326-3的幂(Power of Three)相关推荐
- leetcode刷题笔记342 4的幂
题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例: 当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...
- LeetCode刷题笔记汇总
LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...
- 一个算法笨蛋的12月leetCode刷题日记
类似文章 一个算法笨蛋的2021年11月leetCode刷题日记 一个算法笨蛋的2021年12月leetCode刷题日记 一个算法笨蛋的2022年1月leetCode刷题日记 一个算法笨蛋的2022年 ...
- 个人LeetCode刷题记录(带题目链接及解答)持续更新
Leetcode 刷题 注:~[完成]代表还有一些方法没看,最后再看 一.一些需要重刷的典型题: 1.快速排序,归并排序,堆排序(递归的思想) 2.链表中的回文链表,其中的快慢指针,多看,多练 3.链 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
- LeetCode刷题记录13——705. Design HashSet(easy)
LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode刷题记录11——290. Word Pattern(easy)
LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...
最新文章
- .NET Framework 4.0的新特性
- http1.0和http1.1和http2.0有什么区别
- Apollo进阶课程㊸丨Apollo实战——障碍物感知和路径规划能力实战
- 洛谷 P1656 炸铁路
- [SQL Server]用于压力测试和性能分析的两个支持实用工具[转]
- 经典排序算法-MFC实现之2:问题
- 异常的捕获 try...catch java
- Mysql替换字段中的内容
- 开机时出现:reboot and select proper boot 、关于IDE与AHCI
- springmvc执行原理(基于组件)
- 平面标定(Homography变换)
- “拖延症”的良方——对于追求完美,自制力差,情绪化的人很受用。
- 快速实现微信公众号支付功能
- 如何修改 gitlab 的项目名称
- fremaker遍历list_Freemarker中如何遍历List
- 贝尔商道赚钱思维36道第08道:聪公移山
- 煤炭超临界水气化与超临界燃烧传热耦合Fluent模拟(调试记录)
- Linux和Apple
- k8s sa role rolebinding secret
- 饥荒联机版Mod开发——modmain(五)