【算法练习】连续4个数字的最低乘积与无重复字符的最长子串(字符串滑动窗口相关)

codewars

题目(难度:6K)

Create a function that returns the lowest product of 4 consecutive digits in a number given as a string.

This should only work if the number has 4 digits or more. If not, return "Number is too small".

Example
lowest_product("123456789")--> 24 (1x2x3x4)
lowest_product("35") --> "Number is too small"

题目详解(中文)


连续4个数字的最低乘积
创建一个函数,该函数返回以字符串形式给出的数字中连续 4 位数字的最低乘积。

仅当数字有4位或更多数字时,此方法才有效。如果不是,则返回“数字太小”。


lowest_product("123456789")--> 24 (1x2x3x4)
lowest_product("35") --> "Number is too small"

测试用例
Test.assert_equals(lowest_product("2345611117899"),1);
Test.assert_equals(lowest_product("2305611117899"),0);
Test.assert_equals(lowest_product("333"),"Number is too small");
Test.assert_equals(lowest_product("1234111"),4,"Numbers should be consecutives");

个人思路

  • 运用列表来做,滑动字符串更新列表,关键函数是列表内各数的乘积,采用reduce()
  • 具体操作,例子"1234111",一开始我们先将前4位字符也就是[1,2,3,4]先求乘积保存到结果jieguo中,然后再从后面开始遍历字符串
  • 第一遍添加进列表a=[1,2,3,4,1],然后判断列表是否5位,我们删去最开始的a[0],变成a=[2,3,4,1],然后用中间值temp获得当前乘积结果,与结果jieguo进行比较,如果比结果小赋值给jieguo
  • 以后每次遍历跟上面步骤一样,就形成了一个滑动列表,【1,2,3,4】-》【2,3,4,1】-》【3,4,1,1】.......
  • 最后求得结果a=[4,1,1,1]为最小
#最初方案
from functools import reduce
def lowest_product(input):
    if len(input)<=3:
        return "Number is too small"
    else:
        a=list([int(x) for x in input[0:4]])
        jieguo=reduce(lambda x,y:x*y,a)
        a=[]
        for i in input:
            a.append(int(i))
            if len(a)==5:
                del a[0]
                temp=reduce(lambda x,y:x*y,a)
                if temp<jieguo:
                    jieguo=temp
                else:
                    pass
        return jieguo
    
########改进后############
from functools import reduce
def lowest_product(input):
    if len(input)<=3:
        return "Number is too small"
    else:
        a=list([int(x) for x in input[0:4]])
        jieguo=reduce(lambda x,y:x*y,a)
        for i in range(4,len(input)):
            a.append(int(input[i]))
            if len(a)==5:
                del a[0]
                temp=reduce(lambda x,y:x*y,a)
                if temp<jieguo:
                    jieguo=temp
                else:
                    pass
        return jieguo
lowest_product('11251253652545616654564545454511511564545456465454545645645332562111')

Leetcode

题目(难度:中等)

3.无重复字符的最长子串

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

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

个人思路

  • 这个也是用滑动字符串来做,当前字符存在与列表中时更新列表,更新字符串长度
  • 比如‘pwwkew’,当遍历到pww时,【p,w,w】由于w已存在列表里,我们获得它的下标,然后从它的下标切去,变成新列表【w】,更新长度,减去它的下标,也就是把下标前的字符串都减去
  • 以此继续遍历,求得无重复最长子串
def cxk(s):
    if len(s)==0:
        return 0
    ss=[]#定义的子字符串数组#
    cur_len=0
    max_len=0
    for i in range(len(s)):
        if s[i] in ss:
            j=ss.index(s[i])
            ss=ss[j+1:] #重复字符后面开始切掉,形成新字符串
            cur_len-=j   #更新子字符串的长度 
        else:
            cur_len+=1
        ss.append(s[i])
        if cur_len > max_len:
            max_len = cur_len
    return max_len
cxk("abcbcbb")
发布了41 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Cxk___/article/details/104674908
今日推荐