Arts Everyweek-01

497 阅读3分钟

Arts Everyweek

1. Leetcode Algorithm Single Number Algorithm

  • Single Number

    Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

    Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory?

  • Example1

    Input: nums = [2,2,1]
    Output: 1
    
  • Example2

    Input: nums = [4,1,2,1,2]
    Output: 4
    
  • Example3

    Input: nums = [1]
    Output: 1
    

    Constraints:

    • 1 <= nums.length <= 3 * 104
    • -3 * 104 <= nums[i] <= 3 * 104
    • Each element in the array appears twice except for one element which appears only once.

实现:

  1. 找出数组中没有成对出现的数字num
  2. 不考虑使用其他方法库的情况下,需要循环遍历数组
  3. 需要找出数组长度为1或者遍历最后一个数字时的特殊情况
  4. 进行双重遍历,避免双重便利是index相同的num
  5. 初始化boolean变量为true,只有遍历找到相同才变为false

code:

public class ArraySingleNum {
public int singleNumber(int[] nums){
        int num = 0; // 初始化返回num为0
  			// 外层遍历,或者nums[i]
        for(int i = 0;i < nums.length; i ++){
            // 遍历到最后一个num时候,赋值返回
            if(i + 1 == nums.length){
                num = nums[i];
                break;
            }
          
            // 默认找出单独数字为True
            boolean single = true;
          
            // 进行二层遍历
            for(int j = 0; j < nums.length; j ++){
                // 过滤出index相同的num
                if(i == j){
                    continue;
                }
                // 如果相同,那么将状态改为false
                if(nums[i] == nums[j]){
                    single = false;
                    continue;
                }
            }
            // 如果状态依然是true,说明找出了single数字,跳槽循环
            if(single == true) {
                num = nums[i];
                break;
            }
        }
        return num;
    }

public static void main(String[] args) {

        int[] nums1 = {1, 2, 2};
        if(new ArraySingleNum().singleNumber(nums1) == 1){
            System.out.println("Successed!");
        }else{
            System.out.println("failed!");
        }

        int[] nums2 = {2, 2, 1};
        if(new ArraySingleNum().singleNumber(nums2) == 1){
            System.out.println("Successed!");
        }else{
            System.out.println("failed!");
        }
        int[] nums3 = {2, 1, 2};
        if(new ArraySingleNum().singleNumber(nums3) == 1){
            System.out.println("Successed!");
        }else{
            System.out.println("failed!");
        }
        int[] nums4 = {2, 1, 2,5,6,9,9,6,5};
        if(new ArraySingleNum().singleNumber(nums4) == 1){
            System.out.println("Successed!");
        }else{
            System.out.println("failed!");
        }
    }
}

2. Read the english tech article

Spring Boot + RESTful + JUnit + Mockito + Hamcrest + EclEmma

这是一篇使用spring boot + spring data jpa构建出restful风格接口的技术文章

并且集成了Junit和Mockito进行controller层和service层进行单元测试

3. New tech Skill

spring 注解@ControllerAdvise用来统一处理controller的exception

相比较于传统的Try catch捕捉处理异常,spring提供了@ControllerAdvise注解 用来统一进行异常处理,在controller层只需主动抛出相对应的异常,使用@ExceptionHandler来捕捉不同类型的异常进行处理

使用@ControllerAdvise可以避免很多重复的try catch代码块,减少代码冗余,代码看起来更加简洁,容易读

4. One week Summary

周二时候,订阅了耗子哥的专栏,无奈平时工作太累,没有及时的完成arts,一直拖到这个周末,不管怎样,迈开了第一步,那么第二步,第三步,也就走出去了,加油!

虽然比较多的东西都是第一次接触,英文不是那么的差,似乎在做的过程中没有遇到过多因为英文上的问题,更多的还是技术上对基础把握不牢固,造成的一些理解和应用的上偏差的问题,所以基础还是需要掌握牢固,才能将技术水平提高更高一些,那么以后的问题,也就水到渠成,自然而然的解决了。