leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数。但是有几点需要考虑:

负数应该没有回文数,要加判断!要注意额外的空间申请问题。判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题。实现的代码如下:

  1. #include<stdio.h>
    
    bool isPalindrom(int x)
    {
    	if(x<0)
    		return false;
    	else
    	{
    		int tmp=x;
    		int sum=0;
    		while(tmp)
    		{
    			sum=sum*10+tmp%10;
    			tmp/=10;
    		}
    		if(x==sum)
    			return true;
    		else
    			return false;
    	}
    }
    int main()
    {
    	int x;
    	while(scanf("%d",&x)!=EOF)
    	{
    		if(isPalindrom(x))
    			printf("yes\n");
    		else
    			printf("no\n");
    	}
    	return 0;
    }
时间: 2024-08-24 06:15:48

leetcode第9题-Palindrom Number的相关文章

leetcode第136题-Single Number

题目要求:给出一个数组,只有一个数字出现一次,其他的都出现两次,找出那出现一次的数字,要求用线性的时间解出题目! 分析:因为题目要求的是用线性时间,所以类似于那种暴力解决的方法会超时,如下面这种: int singleNumber2(int *nums,int n) { int i,j; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(nums[i]==nums[j]) break; else continue; } if(j==n) return num

leetcode第202题-Happy Number

说实话,如果不看别人的解答的话,这道题我也是没有思路,不知道该循环几次,也不知道循环的终止条件,后来才知道,[2-6]这个范围内的数字都不是happy number 所以就有了终止条件,n>6就是终止条件,当n跳进这个范围内的时候就终止循环,最后就能判断是否是happy number了 #include<stdio.h> #include<stdlib.h> bool isHappy(int n) { while(n>6) { int sum=0; while(n) {

leetcode第137题-Single Number II

#include<stdio.h> #include<stdlib.h> int singleNumber(int* nums, int numsSize) { int count[32]={0}; int i,j,number=0; for(i=0;i<numsSize;i++) { for(j=0;j<32;j++) count[j]+=((nums[i]&(1<<j))!=0); } for(i=0;i<32;i++) { if(coun

LeetCode 第 263 题 (Ugly Number)

LeetCode 第 263 题 (Ugly Number) Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another pr

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

LeetCode 第 204 题 (Count Primes)

LeetCode 第 204 题 (Count Primes) Description: Count the number of prime numbers less than a non-negative number, n. 计算小于 N 的素数的个数.这道题目比较简单.但是想提高计算效率与需要费点脑筋. 判断一个数字 n 是不是素数的简单方法是 用 n 去除 2,3,4,-,n?1,如果都不能整除就说明这个数是素数. 按照这个思路可以写个简单的函数. bool isPrime(int n)

LeetCode 第 292 题 (Nim Game)

LeetCode 第 292 题 (Nim Game) You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take

【Leetcode长征系列】Single Number II

原题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除