leetcode_9题——Palindrome Number (数学问题)

Palindrome Number

Total Accepted: 57795 Total Submissions: 194286My Submissions

Question Solution

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Hide Tags

Math

Have you met this question in a real interview?

Yes

No

这题是简单题,但题目要求不要使用多余的内存空间,所以做时还是要想想,我是在开始是先计算出数的位数

然后每次比较是否回文时,就单独取出第i位,即先模再除就可以取出来了。

#include<iostream>
#include<string>
#include <math.h>
using namespace std;
int weishu(int x)
{
	for(int i=0;i<=11;i++)
		if((x/(int)pow(10.0,i))==0)
			return i;
}
bool isPalindrome(int x)
{
	if(x<0)
		return false;
	int len=weishu(x);
	for(int i=0;i<len/2;i++)
		if((x%(int)pow(10.0,i+1))/(int)pow(10.0,i)			!=(x%(int)pow(10.0,len-1-i+1))/(int)pow(10.0,len-1-i))
			return false;
	return true;
}
int main()
{
	cout<<isPalindrome(121)<<endl;

	int x=121;
	int len=weishu(x);
	cout<<len<<endl;

	for(int i=0;i<len/2;i++)
	{
		cout<<(x%(int)pow(10.0,i+1))/(int)pow(10.0,i)<<endl;
		cout<<(x%(int)pow(10.0,len-1-i+1))/(int)pow(10.0,len-1-i)<<endl;
	}

}

  

时间: 2024-08-02 08:19:23

leetcode_9题——Palindrome Number (数学问题)的相关文章

HDU 5062 Beautiful Palindrome Number(数学)

主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can represent as (a1a2-akak-a2a1)10 or (a1a2-ak?1akak?1-a2a1)10 of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies 0<

leetcode第九题--Palindrome Number

Problem: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of

LeetCode 第九题, Palindrome Number

题目原文: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of usi

Python算法题----Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. class Solution(object):     def numLen(self, n):         i = 1         while True:             n /= 10             if n > 0:                 i += 1             else:          

LeetCode第九题—— Palindrome Number(判断回文数)

题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From rig

LeetCode_9. Palindrome Number

9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it

leedCode练题——9. Palindrome Number

1.题目 9. Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr