2 Star 0 Fork 0

Mr.Z / bigsai-algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LeetCode 09回文数.md 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
张赛 提交于 2020-11-26 10:08 . update

LeetCode 09回文数

image-20201115192906733

分析: 此题比较简单,需要考虑以下几点:

  • 不能是负数,负数不满足回文数的要求
  • 考虑奇数偶数长度数字会文性

提供两种方法:第一种将数字转成字符串,从中间向两侧拓展比较。 在这里插入图片描述 实现代码为:

//11%
	 public boolean isPalindrome(int x) {
		 if(x<0)return false;
		 String va=x+"";
		 int left=0,right=0;
		 if(va.length()%2==0)
		 {
			 left=va.length()/2-1;right=left+1;
		 }
		 else {
			left=va.length()/2;right=left;
		}
		 while (left>=0) {
			if(va.charAt(left)!=va.charAt(right))
				return false;
			left--;right++;
		}
		 return true;
	 }

但很遗憾这种方法效率比较低只能打败11%的人呢,大概18ms左右。

但是可以换一种思路,使用字符串比较效率较低。可以使用数字类型计算一遍得到逆向数值然后进行比较最终值是否相同: 在这里插入图片描述

public boolean isPalindrome(int x) {
		 if(x<0)return false;
		 int team=x;
		 int va=0;
		 while (x>0) {
			va=va*10+x%10;
			x/=10;
		}
		 if(va==team)return true;
		 return false;
	 }

这样就大概9-10ms左右,9ms大概打败98%而10ms只40%多。 在这里插入图片描述

结语

原创不易,最后我请你帮两件事帮忙一下:

  1. star、follow支持一下, 您的肯定是我在平台创作的源源动力。

  2. 微信搜索「bigsai」,关注我的公众号,不仅免费送你电子书,我还会第一时间在公众号分享知识技术。加我还可拉你进力扣打卡群一起打卡LeetCode。

记得关注、咱们下次再见!

image-20201114211553660

Java
1
https://gitee.com/sillycoder/bigsai-algorithm.git
git@gitee.com:sillycoder/bigsai-algorithm.git
sillycoder
bigsai-algorithm
bigsai-algorithm
master

搜索帮助