ARTS是什么?
Algorithm:每周至少做一个LeetCode的算法题 Review:阅读并点评至少一篇英文文章 Tip:学习至少一个技术技巧 Share:分享一篇有观点和思考的技术文章
Algorithm
/**
* @className: LeetCode09
* @description: 给定二叉搜索树的根结点 root,返回值位于范围 [low, high] 之间的所有结点的值的和。
* @author: Miluo
* @date: 2021/4/26
**/
class TreeNode1 {
int val;
TreeNode1 left;
TreeNode1 right;
TreeNode1() {}
TreeNode1(int val) { this.val = val; }
TreeNode1(int val, TreeNode1 left, TreeNode1 right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class LeetCode09 {
public int rangeSumBST(TreeNode1 root, int low, int high) {
if (root.val < low && root.right != null) {
return rangeSumBST(root.right,low,high);
}else if(root.val > high && root.left != null) {
return rangeSumBST(root.left,low,high);
}
if(root.val >= low && root.val <= high){
if (root.left != null && root.right == null){
return root.val + rangeSumBST(root.left,low,high);
}else if (root.left == null && root.right != null){
return root.val + rangeSumBST(root.right,low,high);
}else if (root.left != null){
return root.val + rangeSumBST(root.left,low,high) + rangeSumBST(root.right,low,high);
}
return root.val;
}
return 0;
}
//官方题解:
public int rangeSumBST1(TreeNode1 root, int low, int high) {
if (root == null) {
return 0;
}
if (root.val > high) {
return rangeSumBST(root.left, low, high);
}
if (root.val < low) {
return rangeSumBST(root.right, low, high);
}
return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
}
}
Review
Why the Serverless Revolution Has Stalled
有以下因素限制了发展:
- 有编程语言的限制,虽然无服务器公司都提供了包装器来运行不支持的语言,但是会影响性能
- 无服务器公司没有统一的标准,使得迁移成本高
- 性能难以衡量,公司会隐藏相关信息。且代码从未运行或一段时间未运行需要初始化,那么重要的代码需要在间隔时间运行,而用户就需要支付运行时间的费用
- 通常不建议运行完整的应用。可以运行完整的应用,但是不值得
Tip
Java8 Date Time API
在Java 8之前,所有关于时间和日期的API都存在各种使用方面的缺陷,主要有:
- Java的java.util.Date和java.util.Calendar类易用性差,不支持时区,而且他们都不是线程安全的;
- 用于格式化日期的类DateFormat被放在java.text包中,它是一个抽象类,所以我们需要实例化一个SimpleDateFormat对象来处理日期格式化,并且DateFormat也是非线程安全,这意味着如果你在多线程程序中调用同一个DateFormat对象,会得到意想不到的结果。
- 对日期的计算方式繁琐,而且容易出错,因为月份是从0开始的,从Calendar中获取的月份需要加一才能表示当前月份。
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* @author Miluo
* @className LocalDate
* @description 日期
* @date 2021/4/25
**/
public class LocalDateTest {
public static void main(String[] args) {
//初始化
LocalDate localDate = LocalDate.of(2021, 4, 25);//2021-04-25
LocalDate localDate1 = LocalDate.now();//2021-04-25
LocalDate parse = LocalDate.parse("2021-04-25");//2021-04-25
//年
int year = localDate.getYear();
System.out.println(year);//2021
int dayOfYear = localDate.getDayOfYear();
System.out.println(dayOfYear);//115
//月
Month month = localDate.getMonth();
System.out.println(month);//APRIL
int dayOfMonth = localDate.getDayOfMonth();
System.out.println(dayOfMonth);//25
//星期
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(dayOfWeek);//SUNDAY
//格式
String format = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(format);//20210425
}
}
import java.time.LocalTime;
/**
* @author Miluo
* @className LocalTimeTest
* @description 时间,与LocalDate相似
* @date 2021/4/25
**/
public class LocalTimeTest {
public static void main(String[] args) {
//初始化
LocalTime now = LocalTime.now();
LocalTime parse = LocalTime.parse("16:46:32");
LocalTime of = LocalTime.of(16, 46, 32);
//时
int hour = now.getHour();
//分
int minute = now.getMinute();
//秒
int second = now.getSecond();
}
}
/**
* @author Miluo
* @className InstantTest
* @description 时间戳:可精确到纳秒
* @date 2021/4/25
**/
public class InstantTest {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);//2021-04-25T08:54:31.094Z
}
}
/**
* @author Miluo
* @className Duration
* @description 时间段
* @date 2021/4/25
**/
public class DurationTest {
public static void main(String[] args) {
LocalDateTime from = LocalDateTime.of(2021, Month.JANUARY, 5, 10, 7, 0); // 2017-01-05 10:07:00
LocalDateTime to = LocalDateTime.of(2021, Month.FEBRUARY, 5, 10, 7, 0); // 2017-02-05 10:07:00
Duration duration = Duration.between(from, to); // 表示从 2017-01-05 10:07:00 到 2017-02-05 10:07:00 这段时间
long days = duration.toDays(); // 这段时间的总天数
long hours = duration.toHours(); // 这段时间的小时数
long minutes = duration.toMinutes(); // 这段时间的分钟数
long seconds = duration.getSeconds(); // 这段时间的秒数
long milliSeconds = duration.toMillis(); // 这段时间的毫秒数
long nanoSeconds = duration.toNanos(); // 这段时间的纳秒数
}
}
/**
* @author Miluo
* @className PeriodTest
* @description 时间段:用年月日表示,因此只接收LocalDate
* @date 2021/4/25
**/
public class PeriodTest {
public static void main(String[] args) {
//初始化
Period period = Period.of(2, 3, 6);
Period period1 = Period.between(
LocalDate.of(2021, 1, 5),
LocalDate.of(2021, 4, 5)
);
}
}