摘要: #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作。下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100;#2. 用and(&)操作得到所有位上的进位carry=0100;#3. 用xor(^)操作找到a 阅读全文
posted @ 2016-10-12 17:19 火金队长 阅读(224) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def canConstruct(self, ransomNote, magazine): ransomNote=list(ransomNote) magezine=list(magazine) for r 阅读全文
posted @ 2016-10-12 17:18 火金队长 阅读(171) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.rig 阅读全文
posted @ 2016-10-12 17:18 火金队长 阅读(241) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,#把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤。最终一次性返回过滤后的结果。class Solution(object):# def filterDemo 阅读全文
posted @ 2016-10-12 17:17 火金队长 阅读(238) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- #求两个集合的交集class Solution(object): def intersection(self, nums1, nums2): resultList=list(set(nums1).intersection(set(nums2))) ret 阅读全文
posted @ 2016-10-12 17:16 火金队长 阅读(161) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.rig 阅读全文
posted @ 2016-10-12 17:16 火金队长 阅读(117) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def findTheDifference(self, s, t): s=sorted(list(s)) t=sorted(list(t)) for st in s: p=t.index(st) del t 阅读全文
posted @ 2016-10-12 17:15 火金队长 阅读(220) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- from itertools import combinationsclass Solution(object): hourList=[8,4,2,1] minList=[32,16,8,4,2,1] def selectHour(self,hourNu 阅读全文
posted @ 2016-10-12 17:14 火金队长 阅读(466) 评论(0) 推荐(0)
摘要: #-*- 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', 1 阅读全文
posted @ 2016-10-12 17:14 火金队长 阅读(345) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solu 阅读全文
posted @ 2016-10-12 17:13 火金队长 阅读(182) 评论(0) 推荐(0)
摘要: #Method1:动态规划##当有n个台阶时,可供选择的走法可以分两类:###1,先跨一阶再跨完剩下n-1阶;###2,先跨2阶再跨完剩下n-2阶。###所以n阶的不同走法的数目是n-1阶和n-2阶的走法数的和class Solution(object): def climbStairs(self, 阅读全文
posted @ 2016-10-12 17:12 火金队长 阅读(98) 评论(0) 推荐(0)
摘要: class Solution: # @param digits, a list of integer digits # @return a list of integer digits def plusOne(self, digits): carry=1 for i in range(len(dig 阅读全文
posted @ 2016-10-12 17:12 火金队长 阅读(159) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- from collections import Counterclass Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: int """ lopa=0 阅读全文
posted @ 2016-10-12 17:11 火金队长 阅读(262) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def firstUniqChar(self, s): s=s.lower() sList=list(s) numCdic={} for c in s: numCdic[c]=numCdic[c]+1 if 阅读全文
posted @ 2016-10-12 17:10 火金队长 阅读(232) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def intersect(self, nums1, nums2): if len(nums1)<len(nums2): tmp=nums1 nums1=nums2 nums2=tmp tmpdic={} 阅读全文
posted @ 2016-10-12 17:09 火金队长 阅读(122) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def isPowerOfFour(self, num): """ :type num: int :rtype: bool """ if num<=0:return False while True: if 阅读全文
posted @ 2016-10-12 17:08 火金队长 阅读(175) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ for i in r 阅读全文
posted @ 2016-10-12 17:07 火金队长 阅读(153) 评论(0) 推荐(0)
摘要: #-*- 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=divmo 阅读全文
posted @ 2016-10-12 17:07 火金队长 阅读(156) 评论(0) 推荐(0)
摘要: 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: tupl 阅读全文
posted @ 2016-10-12 17:05 火金队长 阅读(163) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums)<=1:return len(n 阅读全文
posted @ 2016-10-12 17:05 火金队长 阅读(224) 评论(0) 推荐(0)
摘要: class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list(t.lower())): return True return Falsesol=Solution()print so 阅读全文
posted @ 2016-10-12 17:03 火金队长 阅读(166) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solu 阅读全文
posted @ 2016-10-12 17:03 火金队长 阅读(125) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solu 阅读全文
posted @ 2016-10-12 17:02 火金队长 阅读(165) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#双栈法class Queue(object): def __init__(self): """ initialize your data structure here. """ self.inStack=[] self.outStack=[] def p 阅读全文
posted @ 2016-10-12 17:01 火金队长 阅读(210) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.rig 阅读全文
posted @ 2016-10-12 17:01 火金队长 阅读(195) 评论(0) 推荐(0)
摘要: #-*- 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) i 阅读全文
posted @ 2016-10-12 17:00 火金队长 阅读(167) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- class Solution(object): def containsDuplicate(self, nums): numsdic={} for num in nums: numsdic[num]=numsdic[num]+1 if num in nu 阅读全文
posted @ 2016-10-12 16:59 火金队长 阅读(180) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.rig 阅读全文
posted @ 2016-10-12 16:59 火金队长 阅读(115) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = None#Method1cl 阅读全文
posted @ 2016-10-12 16:58 火金队长 阅读(116) 评论(0) 推荐(0)
摘要: # Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def rever 阅读全文
posted @ 2016-10-12 16:57 火金队长 阅读(177) 评论(0) 推荐(0)
摘要: #-*- 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.app 阅读全文
posted @ 2016-10-12 16:56 火金队长 阅读(164) 评论(0) 推荐(0)
摘要: class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """ n=len(nums) if n==0: return 0 if n==1: return nums[0] i=2 maxin 阅读全文
posted @ 2016-10-12 16:56 火金队长 阅读(206) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0。#所有的尾部的0可以看做都是2*5得来的,所以通过计算所有的因子中2和5的个数就可以知道尾部0的个数。#实际上,2的个数肯定是足够的,所以只需计算 阅读全文
posted @ 2016-10-12 16:55 火金队长 阅读(96) 评论(0) 推荐(0)
摘要: #Method 1import math class Solution(object): def majorityElement(self, nums): numsDic={} for num in nums: numsDic[num]=numsDic[nums]+1 if num in numsD 阅读全文
posted @ 2016-10-12 16:54 火金队长 阅读(203) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # ord(c) -> integer##Return the integer ordinal of a one-character string.##参数是一个ascii字符,返回值是对应的十进制整数class Solution(object): de 阅读全文
posted @ 2016-10-12 16:54 火金队长 阅读(164) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- #Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针# Definition for singly-linked list.# class ListNode(object):# def __init__(se 阅读全文
posted @ 2016-10-12 16:52 火金队长 阅读(177) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数。反之,减去前一个数的两倍然后加上该数### 技术规则 # ##1、相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3##2、小的数字在大的数字右边,所表示的数等于这些数字相 阅读全文
posted @ 2016-10-12 16:51 火金队长 阅读(121) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- #Method1 :超时#class Solution(object):# def maxProfit(self, prices):# # maxprofit=0# for i in xrange(1,len(prices)):# tmprofit=pr 阅读全文
posted @ 2016-10-12 16:50 火金队长 阅读(257) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#杨辉三角返回给定行#方法:自上而下考虑问题,从给定的一行计算出下一行,则给定行数之后,计算的最后一行就是求解的最后一行class Solution(object): def getRow(self, rowIndex): """ :type rowInd 阅读全文
posted @ 2016-10-12 16:49 火金队长 阅读(123) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#杨辉三角class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ result=[];row=1 whi 阅读全文
posted @ 2016-10-12 16:48 火金队长 阅读(131) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# sel 阅读全文
posted @ 2016-10-12 16:46 火金队长 阅读(209) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.rig 阅读全文
posted @ 2016-10-12 16:45 火金队长 阅读(319) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# se 阅读全文
posted @ 2016-10-12 16:44 火金队长 阅读(136) 评论(0) 推荐(0)
摘要: #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.righ 阅读全文
posted @ 2016-10-12 16:43 火金队长 阅读(218) 评论(0) 推荐(0)