arts打卡第二周

Algorithm

 旋转数组

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation: 
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

思路:想了好长时间,没想到,就去参考比人的答案了。 

   解题思路:数组为 1 2 3 4 5 6 7  k = 3  就相当于先反转为

    4 3 2  1 7 6 5

  再把整个数组再次进行翻转 5 6 7 1 2 3 4 

 所以先定义一个反转数组的方法, 以nums.length - k 为中间,左边的数组进行一次反转,然后右边的再次进行反转。 再次整体进行反转

class Solution {
    public void rotate(int[] nums, int k) {
        if(nums == null||nums.length == 0|| k % nums.length ==0 ){
            return;
        }
        int turns = k % nums.length;
        int middle = nums.length - turns;
        
        reverse(nums, 0, middle -1);
        reverse(nums, middle, nums.length - 1);
        reverse(nums, 0, nums.length-1);
    }
    
    public void reverse(int [] nums, int start, int end){
        while(start < end){
            int temp = nums[start];
            nums[start++] = nums[end];
            nums[end--] = temp;
        }
    }
}

Review

  这篇文章是, 使用RESTful Web服务  ,参考文章:https://spring.io/guides/gs/consuming-rest/

  知识点:

   1 在新建的实体类上@JsonIgnoreProperties 注解,标志着,这个实体类中,任何没有绑定值的属性都可以忽略。。关于这一点,自己是这样理解的,用这个实体类接收前端传过来的属性,可以少几个属性也是 可以的。

  2  @SpringBootApplication 注解可以把一个main()方法,变成,springBoot的启动类。

3  RestTemplateBuilder被自动注入到spring中,所以一个请求过来时,是先经过spring的请求工厂的。

 4 可以通过@Bean的方式,自己配置RestTemplate。

Tip  

 freemaker + java代码导出word ,包含图片。

这个主要是工作的内容,其中的图片为echart图,导出到word中的时候 用的base64的格式的字符串。

把流转化为base64格式。

public class Test2 {
    public static String get() throws IOException {

        InputStream resourceAsStream = Test2.class.getResourceAsStream("/image/image_1.png");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(resourceAsStream);
        byte[] bytes = new byte[bufferedInputStream.available()];
        bufferedInputStream.read(bytes);
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String encode = base64Encoder.encode(bytes);
        System.out.println(encode);
        bufferedInputStream.close();
        return encode;

    }
}

  这个导入图片的时候,在word中自己建的图标,比如折线图算图片的,需要另存为,图片重新放回word 中才算是图片的。

这篇导出word 的文章,我还没有汇总,汇总后会重新发一篇博客。

Share:  

  这里分享一篇阿里工程师,对程序员学习的建议吧。刚看完,因为自己最近也有类似的困惑,所以觉得挺实用的。感觉书中最好的一点是,用碎片化的时间系统学习。。看书,不是就要记住。

参考链接:https://zhuanlan.zhihu.com/p/35557474

原文地址:https://www.cnblogs.com/prader6/p/10700093.html