【算法練習】連續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")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章