【leetcode❤python】326. Power of Three】的更多相关文章

#-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfThree(self, n):        if n<=0:            return False        while True:            if n==3 or n==1:                return True            tuple=divmod(n,3)            if tuple[1]!=0:  …
#-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfFour(self, num):        """        :type num: int        :rtype: bool        """        if num<=0:return False        while True:            if num==1:return T…
#-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfTwo(self, n):        if(n<=0):            return False        if(n==1):            return True        while True:            tuple=divmod(n,2)            if tuple[1]!=0:                re…
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100:#2. 用and(&)操作得到所有位上的进位carry=0100;#3. 用xor(^)操作找到a和b不同的位,赋值给a,a=0001:#4. 将进位carry左移一位,赋值给b,b=1000:#5. 循环直到进位carry为0,此时得到a=1001,即最后的sum.#!!!!!!关于负数的运算.…
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):        """        :type nums: List[int]        :type target: int        :rtype: List[int]        """         for i in xrange(…
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):    def lengthOfLastWord(self, s):        """        :type s: str        :rtype: int        """        if s==None:return 0     …
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489'#3.1和2和空格混合形式[顺序只能是正负号-0,空格位置可以随意]的:'+00121515'#4.正数小于2147483647,负数大于-2147483648的数字#其他的情况都是返回0,因此在判断 是把上述可能出现的情况列出来,其他的返回0#AC源码如下class Solution(object…
#-*- coding: UTF-8 -*-class Solution(object):    def compareVersion(self, version1, version2):        """        :type version1: str        :type version2: str        :rtype: int        """        versionl1=version1.split('.'…
class Solution(object):    def convertToTitle(self, n):        """        :type n: int        :rtype: str        """        res=''        while n>0:            tmp=n            n=(n-1)/26            res+=chr(65+(tmp-1)%26)…
#-*- coding: UTF-8 -*-#2147483648#在32位操作系统中,由于是二进制,#其能最大存储的数据是1111111111111111111111111111111.#正因为此,体现在windows或其他可视系统中的十进制应该为2147483647.#32位数的范围是 -2147483648~2147483648class Solution(object):    def reverse(self, x):        """        :type…
#-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予给nums[:],才能够修改原始列表class Solution(object):    def rotate(self, nums, k):        """        :type nums: List[int]        :type k: int        :…
#-*- coding: UTF-8 -*-# The isBadVersion API is already defined for you.# @param version, an integer# @return a bool# def isBadVersion(version):class Solution(object):    def firstBadVersion(self, n):        """        :type n: int        :…
#-*- coding: UTF-8 -*- class MinStack(object):    def __init__(self):        """        initialize your data structure here.        """        self.Stack=[]        self.minStack=[]            def push(self, x):        "&…
#-*- coding: UTF-8 -*- class Solution(object):    def isPalindrome(self, s):        """        :type s: str        :rtype: bool        """        s=s.lower()        if s==None:return False        isPalindrome1=[]        isPal…
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRows):        """        :type s: str        :type numRows: int        :rtype: str        """        if numRows==1:return s     …
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object):    sums=[]    def __init__(self, nums):        """        initialize your data structure here.        :type nums: List[int]        "&…
#-*- coding: UTF-8 -*- #Hint1:#数字i,i的倍数一定不是质数,因此去掉i的倍数,例如5,5*1,5*2,5*3,5*4,5*5都不是质数,应该去掉#5*1,5*2,5*3,5*4 在数字1,2,3,4的时候都已经剔除过,因此数字5,应该从5*5开始#Hint2:#例:#2 × 6 = 12#3 × 4 = 12#4 × 3 = 12#6 × 2 = 12#显然4 × 3 = 12,和6 × 2 = 12不应该分析,因为在前两式中已经知道12不是质数,因此如果数字i是…
#-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,reverse=False),reverse=True)class Solution(object):    def thirdMax(self, nums):        """        :type nums: List[int]        :rtype: int  …
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(self, hayStack, needle):        """        :type haystack: str        :type needle: str        :rtype: int        """        if…
#-*- coding: UTF-8 -*- class Solution:    # @param n, an integer    # @return an integer    def reverseBits(self, n):        tmp=str(bin(n))[2:][::-1]                for i in xrange(32-len(tmp)):            tmp+='0'               return int(tmp,base=…
class Solution(object):    def addBinary(self, a, b):        """        :type a: str        :type b: str        :rtype: str        """        resA=int(a,base=2)        resB=int(b,base=2)                sumAB=bin(resA+resB)   …
#-*- coding: UTF-8 -*- #超时#        lenA=len(A)#        maxSum=[]#        count=0#        while count<lenA:#            tmpSum=0#            for i in xrange(lenA):#                tmpSum+=i*A[i-count]#            maxSum.append(tmpSum)#            coun…
#-*- coding: UTF-8 -*- class Solution(object):    def findNthDigit(self, n):        """        :type n: int        :rtype: int        """        res=n        if n>9:            index=1            mul=9            res=0    …
#-*- coding: UTF-8 -*- #两种方法#方法1:#计算出A和B两个链表的长度分别为m.n;#长度长的链表先走m-n步,之后再一次遍历寻找#方法2:#先走到一个链表的尾部,从尾部开始走:#跳到另一个链表的头部#如果相遇,则相遇点为重合节点class Solution(object):    def getLinkLenth(self,head):        lenth=0        while head!=None:            lenth+=1        …
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def removeElements(self, head, val):        """      …
#-*- coding: UTF-8 -*-class Stack(object):    def __init__(self):        """        initialize your data structure here.        """        self.inQueue=[]        self.outQueue=[]    def push(self, x):        """…
#-*- coding: UTF-8 -*-class Solution(object):    def merge(self, nums1, m, nums2, n):        """        :type nums1: List[int]        :type m: int        :type nums2: List[int]        :type n: int        :rtype: void Do not return anything,…
#-*- coding: UTF-8 -*-class Solution(object):    def isPalindrome(self, head):        """        :type head: ListNode        :rtype: bool        """        if head==None or head.next==None:return True        resList=[]       …
#-*- coding: UTF-8 -*-#利用栈的思想#如果输入的左测扩则入栈,如果输入为右侧扩,判断其是否与当前栈顶配对,如果匹配则删除这一对,否则return False#'(', ')', '{', '}', '[' and ']'class Solution(object):    def isValid(self, s):        """        :type s: str        :rtype: bool        ""…
#-*- coding: UTF-8 -*-#遍历所有元素,将元素值当做键.元素下标当做值#存放在一个字典中.遍历的时候,#如果发现重复元素,则比较其下标的差值是否小于k,#如果小于则可直接返回True,否则更新字典中该键的值为新的下标class Solution(object):    def containsNearbyDuplicate(self, nums, k):        """        :type nums: List[int]        :typ…