【每日一题1220】Double Trouble

:woman_mage: 已知一个数字列表nums和一个目标数字target,请编写一个函数,从左往右,找出其中任意两个连续且和值刚好等于target的数字,一旦找到,需要从移除其中的第二个数字。以此类推,最终返回处理后的列表。

示例:
输入:nums=[1, 2, 3, 4, 5],target=3。
返回:[1, 3, 4, 5]。
解释:因为1+2=3,所以移除2;最终剩下[1,3,4,5]。

题目难度:简单
题目来源:CodeWars:Double Trouble

def solution(nums: list) -> list:
    # your code

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
def solution(nums: list, target) -> list:
    new_list = []
    for i in range(1, len(nums)):
        for j in range(1, i + 1):
            if nums[i - j]:
                pre_n = nums[i - j]
                break
        else:
            pre_n = None
        if pre_n and nums[i] + pre_n == target:
            nums[i] = None
        else:
            new_list.append(nums[i])
    return nums[0:1] + new_list


assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]

很笨的方法

def solution(nums: list,target) -> list:
    l=[]
    for i in range(len(nums)):
        l.append(nums[i])
        if len(l)>1:
            if l[-1] + l[-2] == target:
                l.pop()
    return l

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
2 Likes
def solution(nums_list:list, target_number):
    num1 = nums_list[0]
    res_list = [num1,]
    for i,v in enumerate(nums_list):
        if i>0:
            num2 = v
            if (num1+num2) == target_number:
                continue
            else:
                num1 = nums_list[i]
                res_list.append(num2)
    return res_list

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
def solution(nums:list,target):
    i=0
    while True:
        sum1=nums[i]+nums[i+1]
        # 判断连续两个值是否等于目标值
        if sum1==target:
            # 相等的话就删除该值
            nums.pop(i+1)
            # 判断列表的长度-1是否与循环次数相等,相等则整个列表已经遍历一遍,终止循环
            if i==len(nums)-1:
                break
            # 跳出当前循环,继续下一次循环
            continue
        # 连续两个值不等于目标值
        else:
            i+=1
            # 判断列表的长度-1是否与循环次数相等,相等则整个列表已经遍历一遍,终止循环
            if i==len(nums)-1:
                break
    return nums

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
def solution(nums: list, target) -> list:
    length = len(nums)
    i = 0
    while i < length - 1:
        if nums[i] + nums[i + 1] == target:
            nums.pop(i + 1)
            length = length - 1
        else:
            i += 1
    return nums

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
def solution(nums: list, target: int) -> list:
    # your code
    index = 0
    while index < len(nums)-1:
        if nums[index] + nums[index+1] == target:
            nums.pop(index+1)
            continue
        else:
            index += 1
    return nums


assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
    public List<Integer> solution(List<Integer> list, int target) {
        int i = 0;
        while (i < list.size() - 1) {
            if (list.get(i) + list.get(i + 1) == target) {
                list.remove(i + 1);
                continue;
            } else {
                i++;
            }
        }
        return list;
    }
def double_trouble(nums:list,target:int)->list:
    left = 0
    right = 1
    new_nums = [nums[left]]
    while right< len(nums):
        if nums[left]+nums[right] == target:
            right+=1
        else:
            new_nums.append(nums[right])
            left= right
            right+=1
    return new_nums

后面咱们做个系统,让大家自动练习冲榜吧

1 Like
def solution(nums: list,target) -> list:
    i = 0
    while i < len(nums)-1:
        sum1 = nums[i]+nums[i+1]
        if sum1 == target:
            nums.pop(i+1)
            continue
        else:
            i = i+1
    return nums

assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
def solution(nums: list, target: int) -> list:
    newlist = [nums[0]]
    for i in range(1,len(nums),1):
        if nums[i] + newlist[len(newlist)-1] != target:
            newlist.append(nums[i])
    return newlist
def solution(nums: list, num: int) -> list:
    # your code
    for j in range(len(nums)):
        for i in range(len(nums) - 1):
            n = nums[i] + nums[i + 1]
            if n == num:
                del nums[i + 1]
                break
    return nums


assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
public static List<Integer> solution(List<Integer> lis, int num){
        int delIndex = 0;
        for (int i = 0; i < lis.size()-1; i++) {
            int a = lis.get(i)+lis.get(i+1);
            if (num==a){
                delIndex=i+1;
                break;
            }
        }

        if (delIndex==0){
            System.out.println("未找到需要删除元素");
            return lis;
        }
        lis.remove(delIndex);
        return lis;
    }
def solution(nums: list, target: int) -> list:
    i = 0
    while True:
        if nums[i] + nums[i + 1] == target:
            nums.pop(i + 1)
            if i == len(nums) - 1:
                break
            else:
                continue
        else:
            i += 1
            if i == len(nums) - 1:
                break
    return nums


assert solution([1, 3, 5, 6, 7, 4, 3], 7) == [1, 3, 5, 6, 7, 4]
assert solution([4, 1, 1, 1, 4], 2) == [4, 1, 4]
assert solution([2, 6, 2], 8) == [2, 2]
assert solution([2, 2, 2, 2, 2, 2], 4) == [2]
关闭