左耳听风ARTS第四周

Algorithm

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. 

Solution 1:自己的思路—保存一份给定整数,然后将给定整数进行反转,思路和reverse Integer的算法类似,然后与保存的整数进行比对。

class Solution {
    public boolean isPalindrome(int x) {
        int remain = 0;
        int x1 = x;
        while(x > 0){
            int temp = x % 10;
            int result = remain * 10 + temp;
            if((result - temp)/10 != remain){
                return false;
            }
            x = x/10;
            remain = result;
        }
        if(remain == x1){
            return true;
        }
        return false;
        
    }
}

Solution 2:leetcode上的思路—因为回数的特性,所以只需要匹配一半整数就行,所以不需要考虑溢出的情况,比较巧妙。

class Solution {
    public boolean isPalindrome(int x) {
    if (x<0 || (x!=0 && x%10==0)) return false;
    int rev = 0;
    while (x>rev){
    	rev = rev*10 + x%10;
    	x = x/10;
    }
    return (x==rev || x==rev/10);
    }
}

Review

之前对单元测试没有一个很系统的概念,看完Junit Best Practices后清晰了很多,做个简单的总结:
1、确保单元测试只在内存中运行,不要有http请求或者与数据库、文件服务器进行交互,太慢或者太不可靠。
2、确保测试类和生产环境下的类在同样的包下面
3、使用@before和@after,不要在测试类构造器中初始化
4、单元测试中不要catch异常,也不要抛出具体异常,比如IOException。

Tips

计算机网络协议基础知识普及:

  1. 网络协议第一层:物理层。物理层电脑之间通过集线器(hub)进行连接。集线器采取的是广播模式,会将自己收到的每个字节都复制到其他端口上去。
  2. 网络协议第二层:数据链路层(MAC层)。MAC层电脑之间通过交换机进行连接。交换机具有MAC地址学习能力,学完了就知道谁在哪儿,不用广播了。 第二层主要解决以下几个问题:
    a、这个包是发给谁的?谁应该接收?
    解决第一个问题需要用到一个物理地址,即链路层地址,常被称为MAC地址。
    b、大家都在发,会不会产生混乱?有没有谁先发、谁后发的规则?
    解决第二个问题需要用到多路访问协议。多路访问协议分为信道划分协议、轮流协议、随机接入协议。
    c、如果发送的时候出现了错误,怎么办?
    解决第三个问题需要用到CRC算法,即循环冗余检测。通过XOR异或的算法,来计算整个包是否在发送的过程中出现了错误。

Share

对异常以及异常处理框架探析对异常的处理做了比较好的分析和总结,在此做个简单分享。

猜你喜欢

转载自blog.csdn.net/wuweiwoshishei/article/details/84819395