끄적끄적

[알고리즘] Palindrome Number(java) 본문

Computer Science/Algorithm

[알고리즘] Palindrome Number(java)

mashko 2020. 4. 25. 20:26
반응형

회문 알고리즘 - 숫자

class Solution {
    public boolean isPalindrome(int x) {
        if ( x < 0 ) return false;
        String str = Integer.toString(x);
        int middle = str.length()/2;
        for (int i = 0; i < middle; i++) {
            if ( str.charAt(i) != str.charAt(str.length()-i-1) ) { 
                return false; 
            } 
        } 
        return true;
    }
}

runtime - 7ms
memory - 38.9 mb

반응형
Comments