LeetCode

15三数之和

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:[ [-1, 0, 1], [-1, -1, 2]]

思路:因为是时间复杂度要求,考虑二分法

代码:

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ans = []
        nums.sort()
        for index in range(len(nums) - 1):
            if index == 0 or nums[index- 1] < nums[index]:
                left = index + 1
                right = len(nums) - 1
                while left < right:
                    sum = nums[left] + nums[right] + nums[index]
                    if sum < 0:
                        left += 1
                    elif sum > 0:
                        right -= 1
                    else:
                        ans.append([nums[left],nums[right],nums[index]])
                        left += 1
                        right -= 1
                        while left < right and nums[left - 1] == nums[left]:
                            left += 1
                        while right > left and nums[right] == nums[right + 1]:
                            right -= 1
        return ans


3.最长无重复子串

给定一个字符串,找出不含有重复字符的最长子串的长度。

示例:

给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3。

给定 "bbbbb" ,最长的子串就是 "b" ,长度是1。

给定 "pwwkew" ,最长子串是 "wke" ,长度是3。请注意答案必须是一个子串"pwke" 是 子序列  而不是子串。

思路:

    记录每个字符最近一次出现的位置,当再次遇到重复字符时,从初始位置到重复字符位置 截断为最长子串,并更新新的子串初始位置(重复字符的下一个位置);

代码:

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        last = [-1] * 250            #给可能出现的字符赋值  空间O(1)
        start = 0
        maxLen = 0
        
        for i in range(len(s)):                                                #时间 O(n)
            if last[ord(s[i]) - ord('a')] >= start:                            #出现重复数字
                maxLen = maxLen if maxLen > (i - start) else (i - start)        # 三目运算符
                start = last[ord(s[i]) - ord('a')] + 1                        #更新新子串的开头位置

            last[ord(s[i]) - ord('a')] = i                            # Python不支持字符直接相减得到数字,经过ord()转换

        return maxLen if maxLen > len(s) - start else len(s) - start


28实现strStr()

实现 strStr() 函数。

给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回  -1

示例 1:

输入:haystack = "hello", needle = "ll"

输出:2


示例 2:

输入:haystack = "aaaaa", needle = "bba"

输出:-1


说明:

当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。

对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。

 

思路:模式匹配问题,参考经典算法思想 KMP算法


参考博客: 从头到尾彻底理解KMP https://blog.csdn.net/v_july_v/article/details/7041827


代码:

class Solution(object):
    def strStr(self, haystack, needle):
        """
        :type haystack: str
        :type needle: str
        :rtype: int
        """
        if not needle:
            return 0
        i,j,m,n = -1,0,len(haystack),len(needle)
        next = [-1] * n
        while j < n -1:
            if i == -1 or needle[i] == needle[j]:
                i,j = i + 1, j + 1
                next[j] = i
            else:
                i = next[i]
                
        i = j = 0
        while i < m and j < n:
            if j == -1 or haystack[i] == needle[j]:
                i, j = i + 1, j + 1
            else:
                j = next[j]
        if j == n:
            return i - j
        return -1
        


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值