一、 缀点成线(LeetCode-5230)

1.1 题目描述

1.2 解题思路

比较简单的一题,利用公式 y = kx + b,确定好k和b就好,并且要考虑一种情况,函数 x = h。

1.3 解题代码


public boolean checkStraightLine(int[][] coordinates) {
int k = Integer.MIN_VALUE;
int x1 = coordinates[0][0];
int y1 = coordinates[0][1];
int x2 = coordinates[1][0];
int y2 = coordinates[1][1];
int dy = y2 - y1;
int dx = x2 - x1;
if (dx != 0) {
k = dy / dx;
}
int b = y1 - k * x1;
boolean onLine = true;
for (int i = 2; i < coordinates.length; i++) {
if (k == Integer.MIN_VALUE) {
if (coordinates[i][0] != x1) {
onLine = false;
break;
}
} else if (coordinates[i][1] != (k * (coordinates[i][0]) + b)) {
onLine = false;
break;
}
}
return onLine;
}

二、 删除子文件夹(LeetCode-5231)

2.1 题目描述

2.2 解题思路

使用两层for循环会导致超时,需要使用字典树解决。

2.3 解题代码


class Solution { public Tree rootNode = null; public List<String> removeSubfolders(String[] folder) { rootNode = new Tree();
rootNode.path = "/";
rootNode.nextMap = new HashMap<>(); List<String> res = new ArrayList<>();
Arrays.sort(folder, (a, b) -> {
return a.length() - b.length();
});
for (int i = 0; i < folder.length; i++) {
if (!isCover(folder[i])) {
res.add(folder[i]);
}
}
return res;
} public boolean isCover(String path) { boolean cover = false;
String[] pathArray = path.split("/");
int arrayLen = pathArray.length; Tree tmp = rootNode;
for (int i = 1; i < arrayLen; i++) {
Tree node = tmp.nextMap.get(pathArray[i]);
if (node == null) {
node = new Tree();
node.path = pathArray[i];
node.nextMap = new HashMap<>();
} else {
if (node.isRoot) {
cover = true;
}
}
if (cover == false && i == arrayLen - 1) {
node.isRoot = true;
}
tmp.nextMap.put(pathArray[i], node);
tmp = node;
}
return cover;
} } class Tree {
public String path;
//判断是否是根文件夹
//例如 /a/b 可能是根文件夹,需要在b节点,记录isRoot = true;
public boolean isRoot;
public Map<String, Tree> nextMap;
}

最新文章

  1. Photoshop将普通照片快速制作二次元漫画风格效果
  2. 例解 Linux 下 Make 命令
  3. sql server 常见问题笔记
  4. linux下驱动webcam
  5. 【MVC】 基础
  6. php配置伪静态的方法
  7. android APP是否需要缓存?+简单架构
  8. JIT动态编译器的原理与实现之Interpreter3
  9. 高性能队列Disruptor系列1--传统队列的不足
  10. shell多进程
  11. memcache基础
  12. 无法远程连接mysql,连接后也没有权限创建数据库
  13. Java多线程高并发学习笔记(一)——Thread&amp;Runnable
  14. 【MVC】会员注册/登录,普通验证,会员名是否注册Ajax验证以及会员邮件验证实现原理
  15. Maven-常用插件
  16. superset安装配置
  17. Android 关于“NetworkOnMainThreadException”出错提示的原因及解决办法
  18. SESSION机制(转)
  19. Java基础-Date类常用方法介绍
  20. 后台npm

热门文章

  1. 谷歌浏览器解决ajax跨域问题
  2. ZYNQ block design警告:[BD 41-968] AXI interface port /axi_lite4 is not associated to any clock port. It may not work correctly.
  3. 【译】Python数据结构
  4. Delphi-RzDbgrid-绘制表格式设置某行颜色或者其他格式-以及隔行换色的属性
  5. oracle命令行导出、导入dmp文件
  6. python接口自动化13-data和json参数傻傻分不清
  7. Flink源码阅读(二)——checkpoint源码分析
  8. Java注解annotation : invalid type of annotation member
  9. P2261 [CQOI2007]余数求和[整除分块]
  10. (java)selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致