【每日一题0918】偶数相乘

:woman_mage:请编写一个函数,只使用一行代码返回1~100的所有偶数,和自身相乘结果组成的列表集合。
返回结果示例:[1, 4, 9......]

题目难度:简单
题目来源:面试真题

def solution() -> list:
    pass
def solution() -> list:
    return [i**2 for i in range(2,101,2)]

就和楼上的答案几乎一样

>>> def even() -> list:
...     return [i*i for i in range(2,101,2)]
...
>>> even()
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096, 4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056, 7396, 7744, 8100, 8464, 8836, 9216, 9604, 10000]
public ArrayList<Integer> solution(Integer i){
        ArrayList<Integer> result = new ArrayList<>();
        for (int j = 0; j < i; j++) {
            if (j%2 ==0){
                result.add(j*j);
            }
        }
        return result;
    }

def solution() → list:

    return [i*i for i in range(1,101,1)]
def solution() -> list:
    return [i*i for i in range(1,101) if i%2 == 0]
def solution() -> list:
    return [i*i for i in range(1,101) if i%2==0]
def solution() -> list:
    return [i**2 for i in range(2,101,2)]

def solution() -> list:
    return [i for i in range(0, 100) if i % 2 == 0]
关闭