#Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}        for num in nums:            numsDic[num]=numsDic[nums]+1 if num in numsDic else 0            if numsDic[num]>len(nums)/2:                return…
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Ou…
#-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        """        :type nums: List[int]        :type val: int        :rtype: int        """        for i in range(len(nums)-1,-1,-1):      …
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var hash = {}; var y=-1,z; //注意这里的方括号,利用变量访问对象属性时要用方括号 for(var i=0;i<=nums.length-1;i++){ if(hash[nums[i]]){ hash[nums[i]]++; }else{ hash[…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore Voting 位运算统计位数 相似题目 参考资料 日期 题目地址:https://leetcode.com/problems/majority-element/ Total Accepted: 110538 Total Submissions: 268289 Difficulty: Easy 题目…
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array…
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Solution 1: 使用map计数 class So…
#-*- 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…
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 题意: 给定一个二分搜索树,返回第K小的结点 思路: 只要明白BST树的原理,只要中序遍历一遍BST树即可.求第K小的,只需遍历前K个结点就OK. C++: /**…
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's…
#-*- 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        …