LeetCode 9. Palindrome Number(c语言版)

news/2024/7/3 21:13:05 标签: c/c++, 数据结构与算法

题目:

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 right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

 

代码:

bool isPalindrome(int x) {
    if(x<0) return false;
    if(x==0) return true;
    int a[100],i,j,sum;
    for(i=0;x!=0;i++)
    {
        a[i]=x%10;
        x/=10;
    }
    sum=i;
    i--;
    if(a[0]==0) return false;
    for(j=0;j<sum/2;j++,i--)
        if(a[j]!=a[i]) return false;
    return true;
}

 

转载于:https://www.cnblogs.com/y1040511302/p/10178132.html


http://www.niftyadmin.cn/n/1153073.html

相关文章

生产者--消费者模式

生产者--消费者模式 1、示例&#xff1a; class Resource{private String name;private int count 1;private Boolean flag false;public synchronized void set(String name){if(flag)try {this.wait();//this代表调用函数线程} catch (InterruptedException e) {}this.name…

C++中的预定义宏

C/C&#xff0b;&#xff0b;宏大全(转载,原贴地址http://www.cnblogs.com/sevencat/archive/2004/06/10/14872.html)一、标准预定义宏The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that impl…

PHP 7.0 中各种 Hash 速度比较

概述 最近需要对一些很长的 msyql 字段做索引优化。讨论下来有几种解决方案带确定&#xff0c;其中一个就是对现有字符做 hash&#xff0c;然后对此hash和原始字符做联合索引。就此有了 hash 效率比较的需求&#xff0c;文中使用 php 对一段字符做 200 万次 hash&#xff0c;并…

NPOI兼容 excel2003,2007版本

根据项目需要&#xff0c;需要对excel进行导入导出&#xff0c;所以选择NPOI&#xff0c;优点在这里就不详细介绍了&#xff0c;下面进入正题。 1 public int Import(string path)2 {3 IList<Student> list new List<Student>();4 5 …

解决 Zend Studio For Linux 乱码和UBUNTU下不显示(白屏)问题

不显示界面问题&#xff1a;打开后不能正常显示&#xff0c;只有标题栏正常解决&#xff1a;用vi打开Zend_Development_Environment&#xff08;和你选择安装的路径有关&#xff0c;找下&#xff09;&#xff0c;打开后输入/set nu在输入1693在其附近会有类似下面的代码&#x…

CCRD_TOC_2008年第3期

中信国健临床通讯 2008年第3期 目 录 银屑病和银屑病关节炎 1. 国际皮肤病专家呼吁重视生物制剂治疗银屑病 原文: http://pharmatimes.com/forums/forums/t/855.aspx 2. 案例报道&#xff1a;依那西普有效治疗泛发性脓疱型银屑病 Esposito M, et al. Dermato…

Ubuntu无线上网

sudo apt-get install bcm43xx-fwcutter&#xff0c;不但自动安装了fwcutter工具&#xff08;这个工具本来是从windows driver .sys文件中分离出firmware的&#xff09;&#xff0c;而且还主动提示我要不要直接从网上下载firmware本身。回答yes就一切完成&#xff08;自动下载了…

Linux 之secureCRT连接SSH

1.登陆linux系统&#xff0c;打开终端命令。输入 rpm -qa |grep ssh 查找当前系统是否已经安装。 2.如果没有安装SSH软件包&#xff0c;可以通过yum 或rpm安装包进行安装。 .3.安装好了之后&#xff0c;就开启ssh服务。Ssh服务一般叫做 sshd&#xff0c;命令行输入 service s…