ARTS 打卡计划第三周 05/05

ARTS 打卡计划第三周 05/05

Algorithm

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

example1:

Input: 121
Output: true

example2:

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.

example3:

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?

// 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;
        }
    }
}

提交结果

转换为字符串
转换为字符串

不转换为字符串
不转换为字符串

Review

《WEB DEVELOPE-ROADMAP 2019》

Back-End Roadmap
Back-End Roadmap

Tips

  • 利用generatorConfiguration根据数据库表反向生成model层实例和对应mapper
<?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>


  • pom依赖
            <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>

Share

查找算法:斐波那契查找 Searching Algorithms: Fibonacci Search