当前位置: 首页 > news >正文

LeetCode-- Palindrome Number

题目描述:


Determine whether an integer is a palindrome. Do this without extra space.




Some hints:
Could negative integers be palindromes? (ie, -1)


If you are thinking of converting the integer to string, note the restriction of using extra space.


You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?


There is a more generic way of solving this problem.


本题就是判断一个整数是否为回文。
特殊要求是不能转化为string,其实这个限制我认为不是很有意义,因为现实应用中很少会有要求。


思路:
1. 一次遍历,不断/10,得到x的位数 : digits
2. 遍历digits[0, len/2], 使用n1,n2分别表示x从右边“拿掉”1位和x从左“拿掉”一位之后的数,拿掉的数分别为x1,x2
x1 = n1%10;
n1/=10;
x2 = n2 / Math.Pow(len(digits - i))
n2 -= x2 * Math.Pow(len(digits - i)),其中i∈[0, len/2]




实现代码:


public class Solution {
    public bool IsPalindrome(int x) {
        if(x < 0){
		return false;
    	}
    	
    	if(x == 1){
    		return true;
    	}
    	
    	// first loop , to know how many digit
    	var digits = 0;
    	var n = x;
    	while(n != 0){
    		n/=10;
    		digits++;
    	}
    	
    	int n1 = x; // digits from left to right
    	int n2 = x; // digits from right to left
    	for(var i = 1;i <= digits/2; i++){
    		var p = Math.Pow(10, digits-i);
    		int x1 = n1 % 10;
    		int x2 = (int) (n2 / p);
    		
    		if(x1 != x2){
    			return false;
    		}
    		
    		n1 /= 10;
    		n2 -= (int)(x2 * p);
    	}
    	
    	return true;
    }
}


相关文章:

  • Crystal Report 水晶报表如何实现类似表格单元格合并的功能
  • LeetCode-- Reverse Linked List II
  • 老牛破车般的CSDN博客
  • LeetCode- Two Sum
  • cognos8 关于密钥的问题
  • LeetCode --Word Break
  • LeetCode--H-Index
  • Leetcode--Lowest Common Ancestor of a Binary Search Tree
  • 参加Tibco的SOA应用及2009 IT架构趋势研讨会记
  • Leet 题目整理归类 - 快速通道 (持续更新)
  • 设计模式的阴谋论
  • c# mongodb driver 插入重复记录
  • 中国移动通信信息资源站实体与互联网短消息网关(ISMG)
  • MongoDB C# Driver 使用示例 (2.2)
  • C# GetHashCode 的实现方式
  • [译]Python中的类属性与实例属性的区别
  • 【5+】跨webview多页面 触发事件(二)
  • 【前端学习】-粗谈选择器
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • Android系统模拟器绘制实现概述
  • If…else
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • Java比较器对数组,集合排序
  • Odoo domain写法及运用
  • React+TypeScript入门
  • Redis学习笔记 - pipline(流水线、管道)
  • 官方解决所有 npm 全局安装权限问题
  • 如何学习JavaEE,项目又该如何做?
  • 我建了一个叫Hello World的项目
  • 一个JAVA程序员成长之路分享
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 与 ConTeXt MkIV 官方文档的接驳
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #Linux(帮助手册)
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • $jQuery 重写Alert样式方法
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (C++17) optional的使用
  • (Java数据结构)ArrayList
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (搬运以学习)flask 上下文的实现
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (九十四)函数和二维数组
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .gitignore文件---让git自动忽略指定文件
  • .net core webapi 大文件上传到wwwroot文件夹
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .Net 应用中使用dot trace进行性能诊断