public static String lowest_product(String str){
if(str.length()<4){
return "Number is too small";
}
String[] s = str.split("");
List ins = new ArrayList<Integer>();
for (int i = 0; i < s.length - 3; i++) {
ins.add( tansferInt(s[i]) * tansferInt(s[i+1]) * tansferInt(s[i+2]) * tansferInt(s[i+3]));
}
return (String) Collections.min(ins);
}
public static int tansferInt (String s){
return Integer.parseInt(s);
}
def lowest_product(input: str) -> int:
# your code here
from functools import reduce
res = []
num_list = list(input)
if len(num_list) < 4:
return "Number is too small"
for i in range(len(num_list)-3):
res.append(reduce(lambda x,y:x*y,[int(i) for i in num_list[i:i+4]]))
return min(res)
assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3
# 使用滑动窗口
def lowest_product(input):
if len(input) < 4:
return "Number is too small"
if "0" in input:
return 0
left = 0
right = 0
import math
res = math.inf
cur_res = 1
while(right < len(input)):
cur = int(input[right])
right += 1
cur_res *= cur
if right - left == 4:
res = min(cur_res,res)
cur_res /= int(input[left])
left += 1
return res
def lowest_product(input: str) -> int:
def accumulate_product(left,right):
return reduce(lambda x, y: int(x) * int(y), input[left:right])
if len(input)<4:
return "Number is too small"
else:
left = 0
right = 4
min_product = accumulate_product(left,right)
while right < len(input):
left += 1
right += 1
prod=accumulate_product(left,right)
if prod<min_product:
min_product=prod
return min_product
def lowest_product(input: str):
res_list=[]
if len(input)<4:
return "Number is too small"
else:
word_list=[input[i:i+4] for i in range(len(input)-3)]
for word in word_list:
s=1
for i in range(len(word)):
s*=int(word[i])
res_list.append(s)
return min(res_list)
assert lowest_product("123456789") == 24
assert lowest_product("987654321") == 24
assert lowest_product("234567899") == 120
assert lowest_product("2345611117899") == 1
assert lowest_product("2305611117899") == 0
assert lowest_product("9999998999999") == 5832
assert lowest_product("333") == "Number is too small"
assert lowest_product("1234111321") == 3