【每日一题0926】寻找镜像数字

:woman_mage:镜像数字是指一个数字倒过来还是自身,例如数字101。前20个镜像数字分别是:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101。我们的任务是找出第n个镜像数字,默认所有的镜像数字默认从0开始。

示例:
输入:n=1,返回:0。因为第1个镜像数字是0。
输入:n=10,返回:9。

题目难度:中等
题目来源:codewars

def find_mirror_num(n: int) -> int:
    pass

assert find_mirror_num(2) == 1
assert find_mirror_num(10) == 9
assert find_mirror_num(100) == 909
def find_mirror_num(n: int) -> int:
    result = count = 0
    while True:
        if str(result)[::-1] == str(result):
            count = count + 1
            if count == n:
                return result
        result = result + 1

assert find_mirror_num(2) == 1
assert find_mirror_num(10) == 9
assert find_mirror_num(100) == 909

参考题解:

def find_mirror_num(n: int) -> int:

    def gen_mirrors(x: int):
        
        i, count = 0, 1
        
        while True:
            if count > x:
                return
            if str(i) == str(i)[::-1]:
                yield i
                count += 1
            i += 1

    return list(gen_mirrors(n))[-1]

assert find_mirror_num(2) == 1
assert find_mirror_num(10) == 9
assert find_mirror_num(100) == 909

思路:使用生成器generator。内部定义一个生成器函数gen_mirrors,接收一个参数x,表示需要生成多少个符合镜像条件的元素,然后转成包含n个元素的列表后,的末尾那个就是要找的第n个元素了。

比如我想找「第2个」镜像数字,那么生成器就从0开始遍历判断,一共找2个符合条件的,分别是0和1,那么第2个镜像数字就是1。

def find_mirror_num(n: int) -> int:
    num_dict=dict()
    num=0
    while n:
        if str(num)[::-1] == str(num):
            num_dict[n]=num
            n=n-1
        num = num + 1
    return num_dict[1]
public Integer findMirrorNum(Integer integer) {
        int step = 0;
        int start = 0;
        String res = null;
        StringBuilder sb;
        while (step != integer) {
            res = Integer.toString(start);
            sb = new StringBuilder(res).reverse();
            if (res.equals(sb.toString())) {
                step++;
            }
            start++;
        }
        return Integer.valueOf(res);
    }

    @Test
    public void testFindMirrorNum() {
        assert findMirrorNum(2).equals(1);
        assert findMirrorNum(10).equals(9);
        assert findMirrorNum(100).equals(909);
    }
def find_mirror_num(n: int) -> int:
    m,s = 0,0
    while True:
        if str(m) == str(m)[::-1]:
            s += 1
        if s == n:
            return m
        m += 1
def find_mirror_num(n: int) -> int:
    def get_mirror_list(x: int):
        count = 1
        result = 0
        while count <= x:
            if ''.join(reversed(str(result))) == str(result):
                yield result
                count += 1
            result += 1

    return list(get_mirror_list(n))[-1]


assert find_mirror_num(2) == 1
assert find_mirror_num(10) == 9
assert find_mirror_num(100) == 909
def find_mirror_num(n: int) -> int:
    num ,count = 0,0

    while (1):
        nList = list(str(num))
        nList.reverse()
        reverseNum = ''.join(nList)
        if num == int(reverseNum):
            if count+1 == n:
                break
            else:
                count += 1
        num +=1

    return num

def find_mirror_num(n: int) -> int:
    num = 0
    mirror_index = 0
    while True:
        for i in range(len(str(num))//2):
            if str(num)[i-1] == str(num)[-i]:
                ...
            else:
                break
        else:
            mirror_index += 1
            if mirror_index == n:
                return num
        num += 1


assert find_mirror_num(2) == 1
assert find_mirror_num(10) == 9
assert find_mirror_num(100) == 909
关闭