Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.java
Input: 121
Output: truemysqlexample2:
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.gitexample3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.github
Coud you solve it without converting the integer to a string?算法
// NO.9 Palindrome Number
// 複雜度分析
// 時間複雜度:O(\log_{10}(n))O(log10(n)), 對於每次迭代,咱們會將輸入除以10,所以時間複雜度爲 O(\log_{10}(n))O(log10(n))。
// 空間複雜度:O(1)O(1)。
public class PalindromeNumber {
class Solution {
//轉換成字符串形式
public boolean isPalindromeI(int x) {
String str = String.valueOf(x);
char[] chars = str.toCharArray();
for (int i = 0 ; i<chars.length && i<= chars.length-1-i;i++){
if (chars[i] != chars[chars.length-1-i]){
return false;
}
}
return true;
}
//不轉換字符串
public boolean isPalindromeII(int x){
//負數不是迴文 最後一位數字是0的也不是迴文(0除外)
if (x < 0 || (x % 10 == 0 && x != 0)){
return false;
}
int revertedNum = 0 ;
//條件判斷反轉數字是否已達原數字一半
while (x > revertedNum){
revertedNum = x % 10+ revertedNum * 10;
x = x / 10;
}
//奇數位數的數字 須要/10來判斷
return revertedNum == x || revertedNum /10 ==x;
}
}
}
複製代碼
《WEB DEVELOPE-ROADMAP 2019》
spring
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="H2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<jdbcConnection driverClass="org.h2.Driver"
connectionURL="jdbc:h2:mem:testdb"
userId="sa"
password="">
</jdbcConnection>
<javaModelGenerator targetPackage="geektime.spring.data.mybatis.model"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="geektime.spring.data.mybatis.mapper"
targetProject="./src/main/resources/mapper">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="MIXEDMAPPER"
targetPackage="geektime.spring.data.mybatis.mapper"
targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="t_coffee" domainObjectName="Coffee" >
<generatedKey column="id" sqlStatement="CALL IDENTITY()" identity="true" />
<columnOverride column="price" javaType="org.joda.money.Money" jdbcType="BIGINT"
typeHandler="geektime.spring.data.mybatis.handler.MoneyTypeHandler"/>
</table>
</context>
</generatorConfiguration>
複製代碼
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
複製代碼