C# 写 LeetCode easy #9 Palindrome Number
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 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?
代码:
static void Main(string[] args)
{
int num = ;
bool result = Palindromenumber(num);
Console.WriteLine(result);
Console.ReadKey();
} private static bool Palindromenumber(int num)
{
if (num < ) return false;
var arr = num.ToString().ToCharArray();
Array.Reverse(arr);
return num.ToString() == new string(arr);
}
解析:
输入:整数
输出:bool类型,true或者false
思想:
首先,对于负数判断结果一定为false,对于正数,将其转变为字符数组的格式。
其次,利用数组Array的方法Reverse()将字符数组进行反转。
最后,将原来的整数和反转后的字符数组都转换为字符串类型进行比较。返回true或false。
复杂度:O(n)
C# 写 LeetCode easy #9 Palindrome Number的更多相关文章
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- 【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- LeetCode(9) - Palindrome Number
题目要求判断一个整数是不是回文数,假设输入是1234321,就返回true,输入的是123421,就返回false.题目要求in-place,思路其实很简单,在LeetCode(7)里面我们刚好做了r ...
- 【一天一道LeetCode】#9. Palindrome Number
一天一道LeetCode系列 (一)题目 Determine whether an integer is a palindrome. Do this without extra space. Some ...
随机推荐
- [java,maven] 使用 maven 来搭建简单的 netty 开发环境
大致过程是: 首先, 使用 mvn 命令在指定路径下面创建一套简单的 java 文件包. 然后, 使用 JIdea 导入 maven 项目的方式将创建好的文件包加载到 IDE 环境中.‘ 接下来, ...
- yum安装软件出错解决方法
造成yum下载安装时语法出错, 一般是由于python多个版本共存的原因.所以,只需将yum 设置文件固定python 版本,也就是python2 下面的操作能解决版本冲突问题. 1.sudo vim ...
- 2018年东北农业大学春季校赛 E wyh的阶乘 【数学】
题目链接 https://www.nowcoder.com/acm/contest/93/E 思路 其实就是找阶乘的项中5的个数 末尾为什么会出现0 因为存在5的倍数和偶数相乘 有0存在 借鉴 htt ...
- 借助node.js + mysql 学习基础ajax~
很多小白不知道ajax怎么学,所以就弄了个node后台模拟下基本的ajax请求. 环境要求是安装node~ 先上linkMysql.js var mysql = require('mysql') va ...
- 常见的CSS命名
1:header(头部)logo topbar lang search topmenu banner nav headbox active(活动的) selectselectTop selectLi ...
- hadoop —— teragen & terasort
这两个类所在目录: hadoop-examples-0.20.2-cdh3u6.jar 中: 代码: TeraGen.java: /** * Licensed to the Apache Softwa ...
- js中得~~是什么意思/JS按位非(~)运算符与~~运算符的理解分析
其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1 ~~false == 0 ~~"" == 0 ~~[] == 0 ~~undefined ==0 ~~!u ...
- 修改织梦DedeCms文章页默认title的方法
<title>{dede:field.title/}_{dede:field name='typeid' runphp='yes'}$id=@me;global $dsql;$sql=&q ...
- Matlab的publish功能和cell功能
Matlab的publish功能能够让写的代码变成优美的文档.类似为知笔记的markdown语言. cell功能配合publish使用,可以形成不同的功能块.而且调试的时候,可以按section调试. ...
- ps炫光素材
炫光闪电笔刷,炫光闪电笔刷,雷电笔刷,自然闪电Photoshop笔刷下载ps炫光素材 素材下载:http://www.huiyi8.com/sc/8695.html