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.lang.UnsupportedClassVersionError: org/openqa/selenium/WebDriver : Unsupported major.minor version 51.0
周一上班,正常打开myeclipse,随便写了一个main方法执行.发现报错了... 问题提示如下: java.lang.UnsupportedClassVersionError: org/openq ...
- OSGI简介(转)
原文地址 目前,业内关于OSGI技术的学习资源或者技术文档还是很少的.我在某宝网搜索了一下“OSGI”的书籍,结果倒是有,但是种类少的可怜,而且几乎没有人购买.因为工作的原因我需要学习OSGI,所以我 ...
- Maximum Subsequence Sum 【DP】
Given a sequence of K integers { N1, N2, -, NK }. A continuous subsequence is defined to be ...
- IOS UITableView reload 刷新某一个cell 或 section
通常刷新整个列表 我们都使用[self.tableView reloadData]; 有的时候,有变化更新的只是某一行 row 或者是某个section 所以只更新这一行就好了 //一个section ...
- 4.1 《锋利的jQuery》jQuery中的事件
$(document).ready()方法和window.onload方法的区别 事件绑定 合成事件 事件冒泡 事件对象的属性 tip1:停止事件冒泡和阻止默认行为都可以用return false替代 ...
- 2015 年最热门的国人开发开源软件 TOP 50
开源中国在 2015 年得到了快速的发展,单开源软件收藏量就接近 40000 款,其中不乏优质的国产开源项目.本文从软件的收藏.下载.访问等多角度挑选出了 2015 年最热门的国产开源软件前五十名,让 ...
- Java 吃货联盟
import java.util.Scanner; public class Shao { private static final int[] dishNames = null; private ...
- bzoj3462DZY Loves Math II
数据范围:$$2 \leq S \leq 2 * 10^6$$ $$1 \leq n \leq 10^{18}$$ $$ 1 \leq q \leq 10^5$$ 数学+dp 题解写一年系列... 观 ...
- inteliji ---idea 如何创建scala程序配置
首先创建一个新的工程: #####################################################################################
- MySQL常用的数据类型及函数_20160920
1.常用数据类型 针对创建数据表时候 需要指定字段的数据类型,我整理的是工作常用的几种 可以参考看下数据类型 http://www.w3school.com.cn/sql/sql_datatypes. ...