💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
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; } }; ```