I don't think toString() or matches() doesn't use loops at all. The same applies to any other library functions like log() or pow(), or even round(). Quite possibly there are some loops inside, for no one says the CPU must support FPU intrinsic instructions.
@Song_on_the_road as mentioned above:
Also you could use the following math approach to get the max 32 bit integer power of 'x' number:
pow( x,int( log(0x7FFFFFFF)/log(x) ) ).
In this case x= 3, 0x7FFFFFFF isthe maximum 32 bit integer and the change of base with log(0x7FFFFFFF)/log(x) = logx(0x7FFFFFFF) which yields the number to which you have to power 'x' to get the biggest 32 bit integer.
Note that int() returns the floor of a number passed as parameter.
If you need more info maybe my previous comments can help
1.Define the powers outside of the method, therefor it will be created only once. It should be in the constructor of the class but I'm not sure if Leetcode is creating an instance for every test or not.
2.You can use a set instead of a list to improve the search performance.
powers = set([pow(3, i) for i in xrange(200)])
def isPowerOfThree(self, num):
return num in powers