close

最近在研究Media foundation, 弄到自己整個快死快死, 不知道為啥每次在研究新東西的時候, 腦袋都會有那麼一陣子卡住, 所以就跑回去刷刷題, 讓自己的小腦動起來...., 等之後研究差不多, 我再來出幾篇有關於MF的文章吧.

 

 

Problem Statement

In our previous article we solved the string to integer problem. This is another article in the series leetcode problem solutions and this article is a solution to leetcode 9 problem.

 
 
 

Given an integer x, return true if x is a palindrome integer. An integer is a palindrome when it reads the same backward as forward. Example, 121 is a palindrome while 123 is not.

 
 
 

Constraints:

 
  • -2^31 <= x <= 2^31 - 1

 
 

Example

 
 
 

Example 1

 
Input: x = 121
Output: true
 

Example 2

 
Input: x = -121
Output: false
 

Example 3

 
Input: x = 10
Output: false

 

To reverse the given number we need to do the following steps:

 
  1. 1. Get individual digits from the given number from right to left using the mod operator.

  2. 2. Put the digit obtained in step 1 into correct position in the reversed number.

  3. 3. Discard the currently processed digit from the original number.

 

 

 

 

 

bool isPalindrome_V(int x) {

    if (x < 0 || (x > 0 && x % 10 == 0))
        return false;

    int reversedNumber = 0;
    while (x > reversedNumber) {
        reversedNumber = reversedNumber * 10 + x % 10;
        x = x/10;
    }
    
    if (x == reversedNumber || x == reversedNumber / 10)
        return true;
    else
        return false;
}


void main()
{
    int sel;
    while (1) {
        cin >> sel;
        isPalindrome_V(sel)==true? printf("true\n"): printf ("false\n");
    }
}

 

image

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Eric 的頭像
    Eric

    一個小小工程師的心情抒發天地

    Eric 發表在 痞客邦 留言(0) 人氣()