ARTS第一周

开始进行的第一周。

1.Algorithm:每周至少做一个 leetcode 的算法题
2.Review:阅读并点评至少一篇英文技术文章
3.Tip:学习至少一个技术技巧
4.Share:分享一篇有观点和思考的技术文章

以下是各项的情况:

Algorithm

这周有点赶项目,偷懒做了一个最基本的题目。

链接:[LeetCode-01]-Two Sum

  Java:

public int[] twoSum(int[] nums, int target) {
    Map<Integer,Integer> map = new HashMap<>();
    for(int i=0 ; i < nums.length ; i++){
        map.put(nums[i],i);
    }
    for (int i = 0;i < nums.length; i++){
        int complement = target - nums[i];
    if (map.containsKey(complement)) {
        return new int [] { i , map.get(complement)};
        }
    }
    throw new IllegalArgumentException("No two sum solution");
}                

 

  JS:

const twoSum = function(nums, target) {
    const comp = {};
    for(let i=0; i<nums.length; i++){
        if(comp[nums[i] ]>=0){
            return [ comp[nums[i] ] , i]
        }
        comp[target-nums[i]] = i
    }
};

 

Review

分享   写伪代码来帮助理清思路  https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/

   如何写一个伪代码?

  伪代码是经常用于编程和基于算法的字段的术语。它是一种允许程序员表示算法实现的方法。简单地说,我们可以说它是算法的显性表示。通常,算法在伪代码的帮助下表示,因为无论什么编程背景或知识等级,程序员都可以解释它们。顾名思义,伪代码是一种假代码或代码的表示,即使是具有一些学校级编程知识的外行也可以理解。

        算法它是有组织的逻辑序列或针对特定问题的方法。程序员实现一种算法来解决问题。算法使用自然语言但有些是用注释来表达。

  伪代码:它只是以简单英语编写的注释和信息文本形式的算法实现。它没有任何编程语言的语法,因此无法由计算机编译或解释。

  伪代码的优点

  • 提高任何方法的可读性。这是开始实现算法的最佳方法之一。
  • 充当程序与算法或流程图之间的桥梁。也可以作为一个粗略的文档,因此当写出伪代码时,可以很容易地理解一个开发人员的程序。在行业中,文档的方法是必不可少的。这就是伪代码证明至关重要的地方。
  • 伪代码的主要目标是解释程序的每一行应该做什么,从而使程序员更容易构建代码的构建阶段。

  如何写一个伪代码?

  1. 安排任务序列并相应地编写伪代码。

  2. 从伪代码的声明开始,该伪代码确定主要目标或目标。

  Example:

  This program will allow the user to check
  the number whether it's even or odd. 

  3. if-else,for,while循环在程序中缩进的方式,同样地缩进语句,因为它有助于理解决策控制和执行机制。它们还在很大程度上提高了可读性。

Example:

if "1"
    print response
        "I am case 1"

if "2"
    print response
        "I am case 2"

  4. 使用适当的命名约定。人类倾向遵循我们所看到的方法。如果程序员通过伪代码来表达,他的方法将与它相同,因此命名必须简单明了。

  5. 使用适当的命名方式,例如驼峰式用于方法,大写用于常量,小写用于变量。

  6. 详细说明实际代码中将要发生的一切。不要将伪代码抽象化。

  7. 使用标准编程结构,例如'if-then','for','while','cases',就像我们在编程中使用它一样。

  8. 检查伪代码的所有部分是否完整,有限且清晰,以便理解和理解。

  9. 不要以完整的编程方式编写伪代码。即使对于外行或客户而言,也必须易于理解,因此不包含太多技术术语。 

             

 

  例:

    我们来看看这段代码吧

// This program calculates the Lowest Common multiple 
// for excessively long input values 
  
import java.util.*; 
  
public class LowestCommonMultiple { 
  
    private static long
    lcmNaive(long numberOne, long numberTwo) 
    { 
  
        long lowestCommonMultiple; 
  
        lowestCommonMultiple 
            = (numberOne * numberTwo) 
              / greatestCommonDivisor(numberOne, 
                                      numberTwo); 
  
        return lowestCommonMultiple; 
    } 
  
    private static long
    greatestCommonDivisor(long numberOne, long numberTwo) 
    { 
  
        if (numberTwo == 0) 
            return numberOne; 
  
        return greatestCommonDivisor(numberTwo, 
                                     numberOne % numberTwo); 
    } 
    public static void main(String args[]) 
    { 
  
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Enter the inputs"); 
        long numberOne = scanner.nextInt(); 
        long numberTwo = scanner.nextInt(); 
  
        System.out.println(lcmNaive(numberOne, numberTwo)); 
    } 
} 

    用伪代码表示:

This program calculates the Lowest Common multiple  
for excessively long input values 
  
function lcmNaive(Argument one, Argument two){ 
  
    Calculate the lowest common variable of Argument 
    1 and Argument 2 by dividing their product by their 
    Greatest common divisor product 
  
    return lowest common multiple 
end 
} 
function greatestCommonDivisor(Argument one, Argument two){ 
    if Argument two is equal to zero 
        then return Argument one 
  
    return the greatest common divisor 
  
end 
} 
  
{ 
In the main function 
     
   print prompt "Input two numbers" 
         
   Take the first number from the user 
   Take the second number from the user 
  
   Send the first number and second number  
   to the lcmNaive function and print 
   the result to the user    
} 

 

Tip

  曾遇到一个问题: 

   简单的说: 就是 客户要求的数据库字段 其他都是首字母小写的驼峰式命名 , 但是OrderId不是,当时还问过甲方 DBA ,反正回答就是 给我的就是这么写的 不能改 。 具体传值时候发现其他都正常 而这个字段传值 null , 如果改成 orderId 就能传的到  ? 怎么解决 ? 为什么 ?

   具体的:

JSON数据:
{
    "OrderId":"T3565331",
    "userId":"W1078"
}
Controller方法:
@PostMapping
public String hehe(@RequestBody Order order){
    return null;
}
实体类:
public class Order {
    private String OrderId;
    private String userId;

    public String getOrderId() {
        return OrderId;
    }

    public void setOrderId(String orderId) {
        OrderId = orderId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.user = userId;
    }
}

  写了个测试随便测测 : (因为当时懒 userId 写的 user , 当时截图下来就是user , 当userId就是 )

 

 

  解答:

  如何解决 ?因为已经清楚  orderId 可以传值了 , 使用 @JsonProperty  重新定义 json 别名 把 OrderId 程序里改成  别名 orderId 就解决了 。

  

  但来思考个问题 实际是通过 json 找实体类的属性 还是 通过实体类找到 json 的属性?

 

   查看 springBoot 序列化 json 的源码,是使用的 jackson 处理的 。返回用 @Responsebody 自动处理 json , 而 Jackson 在解析返回的 json 字符串时 首字母 默认是小写的  。所以是通过实体类找到的,具体是: 提取 set 方法 , 去掉 set 然后后面的首字母变成小写 , 再去 json 去找 。 所以会出上面的问题 。再具体的解析 可以去看看 肥朝的  spring 讲解 ,当时忘了记下地址了 。。。(失策,现在不知道地址了没法贴了)或者自己打开 IDEA 看看 序列化 json 这一块的源码

  

  因为 上面的问题 , 所以 我推荐下看json序列化的源码

        https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/web/sbe-web-json

 

Share

  感觉现在最缺乏的就是时间管理 和 精力的管理 , 所以这周推荐

  耗子哥的 时间管理:如何利用好自己的时间

 

posted @ 2019-06-16 02:25  五行属鱼  阅读(155)  评论(0编辑  收藏  举报