본문 바로가기
python-algorithm

leetcode 2396. Strictly Palindromic Number

by 무적김두칠 2023. 1. 27.

https://leetcode.com/problems/strictly-palindromic-number/description/

 

Strictly Palindromic Number - LeetCode

Strictly Palindromic Number - An integer n is strictly palindromic if, for every base b between 2 and n - 2 (inclusive), the string representation of the integer n in base b is palindromic. Given an integer n, return true if n is strictly palindromic and f

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def base_n(number, base):
    based_number = ''
    while number > 0:
        based_number += str(number % base)
        number //= base
    if based_number == based_number[::-1]:
        return True
    else:
        return False
 
 
class Solution:
    def isStrictlyPalindromic(self, n: int-> bool:
        answer = True
        for base in range(2, n - 1):
            if not base_n(n, base):
                answer = False
                break
        return answer
 
cs

 

진법 변환하는 함수를 만들고, 그 결과가 palindromic 한지 출력하는 문제입니다

It is a problem to create a function that converts base and outputs whether the result is palindromic.

반응형

댓글