#回文数#Method1:将整数转置和原数比较,一样就是回文数:负数不是回文数#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        if x<0:return False    …
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0# def guess(num):#binary searchclass Solution(object):    def guessNumber(self, n…
#-*- 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 -*- class Solution(object):    hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',\            10:'a',            11:'b',            12:'c',            13:'d',            14:'e',            15:'f'}            def t…
class Solution(object):    def isUgly(self, num):        if num<=0:return False        comlist=[2,3,5];modflag=0                while True:            if num==1:break            for value in comlist:                tuple=divmod(num,value)            …
#-*- 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 -*- 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 -*- #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 -*- from collections import Counterclass Solution(object):    def longestPalindrome(self, s):        """        :type s: str        :rtype: int        """        lopa=0        lopa1=0        slist=list(s)  …
#-*- coding: UTF-8 -*- class Solution(object):    def hammingWeight(self, n):        if n<=0:return n        mid=[]        while True:            if n==0:break            n,mod=divmod(n,2)            mid.append(mod)        mid.reverse()        return…
#-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string.##参数是一个ascii字符,返回值是对应的十进制整数class Solution(object):    def titleToNumber(self, s):        columns=0        n=len(s)        s=s.upper()[::-1]        for c…
Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would simplify the problem: only alphanumerci characters considered ignoring cases Given a string, we check if it is a valid palindrome by following rules: A…
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个子串都是回文串 [思路] 求一个字符串的所有子串是否是回文串,O(n^2) dp从后往前推 vector<vector<bool> > p(len,vector<bool>(len)); ;i<len-;i++){ ;j<len-;j++){ if(j!=i)…
#-*- 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…
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you…
#-*- 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 -*- #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 -*- #题意:大海捞刀,在长字符串中找出短字符串#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…