prader6

arts打卡第二周

Algorithm

 旋转数组

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

Example 1:

[1,2,3,4,5,6,7]
[5,6,7,1,2,3,4]
[7,1,2,3,4,5,6]
[6,7,1,2,3,4,5]
[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

posted on   prader6  阅读(172)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

导航

统计

点击右上角即可分享
微信分享提示