leetcode 29 两数相除 30 串联所有单位的子串

leetcode 29 两数相除 30 串联所有单位的子串

给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

返回被除数 dividend 除以除数 divisor 得到的商。

整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

示例 1:

输入: dividend = 10, divisor = 3
输出: 3
解释: 10/3 = truncate(3.33333…) = truncate(3) = 3
示例 2:

输入: dividend = 7, divisor = -3
输出: -2
解释: 7/-3 = truncate(-2.33333…) = -2

提示:

被除数和除数均为 32 位有符号整数。
除数不为 0。
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。

class Solution(object):
    def divide(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        min = 2**31
        ans = str(dividend / divisor)
        ans2 = 0
        f = True
        for i in range(len(ans)):
            if ans[i] == "-":
                f = False
                continue
            if ans[i] == ".":
                break
            ans2 = ans2*10 + int(ans[i])
            if f and ans2 >= min:
                ans2 = min -1
                break
            if not f and ans2 < -min:
                ans2 = min
                break
        return ans2 if f else -ans2

    def divide2(self, dividend, divisor):
        """
        :type dividend: int
        :type divisor: int
        :rtype: int
        """
        min = 2 ** 31
        ans = int(dividend / divisor)
        if ans < -min:
            return -min
        elif ans >= min:
            return min - 1
        return ans
30. 串联所有单词的子串

难度困难496收藏分享切换为英文接收动态反馈

给定一个字符串 s 和一些 长度相同 的单词 words **。**找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:
从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
输出的顺序不重要, [9,0] 也是有效答案。

示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]

示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]

提示:

  • 1 <= s.length <= 104
  • s 由小写英文字母组成
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 30
  • words[i] 由小写英文字母组成
#coding=utf-8
import copy
from typing import List


class Solution:
    def findSubstring(self, s: str, words: List[str]) -> List[int]:
        slen = len(s)
        words_keys = {}
        for i in words:
            if i[0] not in words_keys:
                words_keys[i[0]] = [i]
            else:
                words_keys[i[0]].append(i)
        words_len = sum([len(i) for i in words])
        ans = []
        i = 0
        while i < slen:
            if i + words_len > slen:
                break
            j = i
            ls_words_keys = copy.deepcopy(words_keys)
            flag = True
            while j < slen and s[j] in ls_words_keys:
                for k, w in enumerate(ls_words_keys[s[j]]):
                    wlen = len(w)
                    if s[j + wlen - 1] == w[-1:]:
                        # 后缀相同
                        pass
                        jl = j + wlen - 2
                        wl = wlen - 2
                        while wl > 0 and w[wl] == s[jl]:
                            wl -= 1
                            jl -= 1
                        if wl > 0:
                            flag = False
                            continue
                        ls_words_keys[s[j]].pop(k)
                        if len(ls_words_keys[s[j]]) <= 0: del ls_words_keys[s[j]]
                        j += wlen
                        flag = True
                        break
                    else:
                        if k + 1 == len(ls_words_keys[s[j]]):
                            flag = False
                if not flag:
                    break
            if j - i == words_len:
                ans.append(i)
            i += 1
        return ans

    def findSubstring2(self, s: str, words: List[str]) -> List[int]:
        from collections import Counter
        if not s or not words: return []
        one_word = len(words[0])
        word_num = len(words)
        n = len(s)
        if n < one_word: return []
        words = Counter(words)
        res = []
        for i in range(0, one_word):
            cur_cnt = 0
            left = i
            right = i
            cur_Counter = Counter()
            while right + one_word <= n:
                w = s[right:right + one_word]
                right += one_word
                if w not in words:
                    left = right
                    cur_Counter.clear()
                    cur_cnt = 0
                else:
                    cur_Counter[w] += 1
                    cur_cnt += 1
                    while cur_Counter[w] > words[w]:
                        left_w = s[left:left + one_word]
                        left += one_word
                        cur_Counter[left_w] -= 1
                        cur_cnt -= 1
                    if cur_cnt == word_num:
                        res.append(left)
        return res

if __name__ == '__main__':
    # a = ["w","g","b","w"]
    # print("w" not in a)
    s = "wordgoodgoodgoodbestword"
    words = ["word", "good", "best", "word"]
    # s = "barfoothefoobarman"
    # words = ["foo", "bar"]
    # s = "barfoofoobarthefoobarman"
    # words = ["bar", "foo", "the"]

    # s = "foobarfoobar"
    # words = ["foo", "bar"]

    # s = "aaaaaaaaaaaaaa"
    # words = ["aa", "aa"]

    # s = "bccbccb"
    # words = ["bc", "cc", "cb"]

    # a = {1:[1,2],2:[2,3]}
    # print(a)
    # print(a[1].pop(a[1].index(2)))
    # print(a)
    s = "lingmindraboofooowingdingbarrwingmonkeypoundcake"
    words = ["fooo", "barr", "wing", "ding", "wing"]
    # s = "lingmindraboofooowingdingbarrwingmonkeypoundcake"
    # words = ["fooo", "barr", "wing", "ding", "wing"]

    s = "abaababbaba"
    words = ["ab", "ba", "ab", "ba"]

    # s = "dbaaacbcbbabdcabdacabadcbcadcadbbacbcdadaaadcabbadbbdbdadddcdcdbcbcaacda"
    # words = ["aad", "bcd", "ada"]

    # s = "abbaccaaabcabbbccbabbccabbacabcacbbaabbbbbaaabaccaacbccabcbababbbabccabacbbcabbaacaccccbaabcabaabaaaabcaabcacabaa"
    # print(s[92:-1])
    # words = ["cac", "aaa", "aba", "aab", "abc"]
    # s = "acbcdef"
    # words = ["bcd","ef"]
    print(Solution().findSubstring(s,words))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matianlongg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值