leetcode 练习(三)

字符串

 https://leetcode.com/problems/longest-palindrome/description/

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        flag=0
        max_length=0
        char_map={}
        n=len(s)
        for i in s:
            if i in char_map:
                char_map[i]+=1
            else:
                char_map[i]=1
        for i in char_map.keys():
            if char_map[i]%2==0:
                max_length+=char_map[i]
            else:
                max_length+=char_map[i]-1
                flag=1
        return max_length+flag

数组和矩阵

  https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/ 

class Solution(object):
    def kthSmallest(self, matrix, k):
        """
        :type matrix: List[List[int]]
        :type k: int
        :rtype: int
        """
        left,right=matrix[0][0],matrix[-1][-1]
        while left<right:
            mid=(left+right)/2
            if sum(bisect.bisect(row,mid) for row in matrix)<k:
                left=mid+1
            else:
                right=mid
        return left

 

 位运算

 https://leetcode.com/problems/single-number-iii/description/

 

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        tmp=0
        for i in nums:
            tmp^=i
        j=0
        while tmp&1==0:
            tmp>>=1
            j+=1
        tmp=1<<j
        first,second=0,0
        for num in nums:
            if num&tmp:
                first^=num
            else:
                second^=num
        return [first,second]

 数学进制转换 

 https://leetcode.com/problems/base-7/description/

class Solution(object):
    def convertToBase7(self, num):
        """
        :type num: int
        :rtype: str
        """
        if num<0:
            return '-'+self.convertToBase7(abs(num))
        elif num <7:
            return str(num)
        else:
            return self.convertToBase7(num//7)+str(num%7)

 

 相遇问题

 https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/

class Solution(object):
    def minMoves2(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        n=len(nums)
        mid=(n-1)//2
        count=0
        for i in range(n):
            count+=abs(nums[i]-nums[mid])
        return count

 多数投票问题

 https://leetcode.com/problems/majority-element/description/ 

class Solution(object):
    def majorityElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        dic={}
        for num in nums:
            if num not in dic:
                dic[num]=1
            if dic[num]>len(nums)//2:
                return num
            else:
                dic[num]+=1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值