zoukankan      html  css  js  c++  java
  • 每日练习(1)

    使用排序比较的方式

    class Solution:
        def findRepeatNumber(self, nums: List[int]) -> int:
            nums.sort()
            pre = nums[0]
            for i in range(1,len(nums)):
                if pre == nums[i]:
                    return pre
                pre=nums[i]
    View Code

    使用集合的唯一性

    class Solution:
        def findRepeatNumber(self, nums: List[int]) -> int:
            newset={}
            for num in nums:
                if num not in newset:
                    newset[num]=1
                else:
                    return num
    View Code

     使用的方法都是从右上角往左下角进行处理。

    class Solution:
        def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
            if len(matrix)==0:
                return False
            row = 0
            col = len(matrix[0])-1
            while row<len(matrix) and col>=0:
                if matrix[row][col]==target:
                    return True
                elif target > matrix[row][col]:
                    row += 1
                else:
                    col -=1
            return False
    View Code

     直接使用Python的替换方法

    class Solution:
        def replaceSpace(self, s: str) -> str:
            return s.replace(" ", "%20")
    View Code

    也可以使用查询,替换的方法

    class Solution:
        def replaceSpace(self, s: str) -> str:
            if len(s)==0:
                return ""
            newList=list(s)
            for i in range(len(s)):
                if newList[i]==" ":
                    newList[i]="%20"
            return ''.join(newList)    
    View Code
    class Solution:
        def findRepeatNumber(self, nums: List[int]) -> int:
            nums.sort()
            pre = nums[0]
            for i in range(1,len(nums)):
                if pre == nums[i]:
                    return pre
                pre=nums[i]
  • 相关阅读:
    poj 3304 直线与线段相交
    poj 2318 叉积+二分
    AC自动机
    MySQL报错:Packets larger than max_allowed_packet are not allowed 的解决方案
    SCOPE_IDENTITY的作用
    Truncate table、Delete与Drop table的区别
    .Net Attribute特性
    vs2010 调试快捷键
    TFS和VSS的简单对比
    做网站用UTF-8还是GB2312 & 各国语言对应字符集
  • 原文地址:https://www.cnblogs.com/topass123/p/12551280.html
Copyright © 2011-2022 走看看