AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
Given an integer, write a function to determine if it is a power of three. Example 1 ``` Example 1: Input: 27 Output: true ``` Example 2: ``` Input: 0 Output: false ``` Example 3: ``` Input: 9 Output: true ``` Example 4: ``` Input: 45 Output: false ``` ``` var isPowerOfThree = function(n) { if(n == 1){ return true; } else{ while(n>=3){ n = n/3; } if(n == 1){ return true; } else{ return false; } } }; ``` ``` //先排除那些对3求模不为0的数字 var isPowerOfThree = function(n) { if(n===1){ return true; } while(n>0){ if(n%3===0){ if(n/3===1){ return true; } else{ n/=3; } } else{ return false; } } if(n===0||n<0){ return false; } }; ```