leetcode6

Implement strStr()

#coding=utf-8
class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        l1=len(haystack)
        l2=len(needle)
        if needle not in haystack:
            return -1
        else:
            for i in range(l1-l2+1):
                if haystack[i:i+l2]==needle:
                    return i

Divide Two Integers

被除数一个个的减除数,无法通过,因为时间复杂度为O(n),比如一个很大的数除1,就很慢。用二分查找的思路,可以说又是二分查找的变种。
-2147483648会溢出,所以dividend==-2147483648 and divisor==-1:应该返回的是2147483647

#coding=utf-8
class Solution(object):
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        if (dividend<0 and divisor>0) or (dividend>0 and divisor<0):
            if abs(dividend)<abs(divisor):
                return 0
        a=abs(dividend);b=abs(divisor)
        temp=0;count=0;result=0
        while a>=b:
            temp=b
            count=1
            while temp+temp<=a:
                temp+=temp
                count+=count
            a-=temp
            result+=count
        if (dividend<0 and divisor>0) or (dividend>0 and divisor<0):
            result=-result
        return result

可以使用位移运算,对于正数和负数,左移以为都相当于乘以2的1次方,左移n位,就相当于乘以2的n次方。右移除以2的n次方。

while dividend>=divisor:
    temp=divisor
    i=1
    while temp<=dividend:
        temp<<=1
        i+=1
    res += (1 << (i-2))
    dividend-=(divisor<<(i-2))

Substring with Concatenation of All Words

#coding=utf-8
class Solution(object):
    def findSubstring(self, s, words):
        """
        :type s: str
        :type words: List[str]
        :rtype: List[int]
        """
        d_words={}
        l_w=len(words)
        for i in words:
            if i not in d_words:
                d_words[i]=1
            else:
                d_words[i]+=1
        l_pw=len(words[0])
        result=[]
        for i in range(len(s)+1-l_pw*l_w):
            curr={}
            j=0
            while j<l_w:
                word=s[i+j*l_pw:i+j*l_pw+l_pw]
                if word not in d_words:
                    break
                if word not in curr:
                    curr[word]=1
                else:
                    curr[word]+=1
                if curr[word]>d_words[word]:
                    break
                j+=1
            if j==l_w:
                result.append(i)
        return result

Next Permutation

逆序查找到前一个比后一个小的位置,将该位置后面比该位置数大的最小值与该位置交换,再将位置后面的数排序

#coding=utf-8
class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        temp=[]
        for i in nums:
            temp.append(i)
        temp.sort()
        temp.reverse()
        if nums==temp:
            nums.sort()
        else:
            for i in range(len(nums)-1,-1,-1):
                if nums[i-1]<nums[i]:
                    for h in range(len(nums)-1,i-1,-1):
                        if nums[h]>nums[i-1]:
                            nums[i-1],nums[h]=nums[h],nums[i-1]
                            break
                    break
            for j in range(i+1,len(nums)):
                key=nums[j]
                k=j-1
                while nums[k]>key and k>i-1:
                    nums[k+1]=nums[k]
                    k-=1
                nums[k+1]=key
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值