前言

本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构请见LeetCode 刷题汇总

正文

幕布

幕布链接

71. 简化路径

题解

Java 10-lines solution with stack​

一点两点特殊处理

import scala.collection.mutable.ListBufferobject Solution {def simplifyPath(path: String): String = {val paths = path.split("/")val res = ListBuffer[String]()for (p <- paths if p.nonEmpty && !p.equals(".")) {if (p.equals("..") && res.nonEmpty) res.remove(res.size - 1)else if (!p.equals("..")) res += p}"/" + res.mkString("/")}
}

栈+skip set

class Solution {public String simplifyPath(String path) {Deque<String> stack = new ArrayDeque<>();Set<String> skip = new HashSet<>(Arrays.asList("..",".",""));for (String dir : path.split("/")) {if (dir.equals("..") && !stack.isEmpty()) stack.pop();else if (!skip.contains(dir)) stack.push(dir);}String res = "";for (String dir : stack) res = "/" + dir + res;return res.isEmpty() ? "/" : res;}
}

72. 编辑距离

题解

官方题解​

动态规划

class Solution {public int minDistance(String word1, String word2) {int m = word1.length(), n = word2.length();if(m + n == 0) return 0;int[][] dp = new int[m + 1][n + 1];for(int i = 1; i <= m; i++){dp[i][0] = i;}for(int j = 1; j <= n; j++){dp[0][j] = j;}for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){if(word1.charAt(i) == word2.charAt(j)){dp[i + 1][j + 1] = dp[i][j];}else{int insert = dp[i + 1][j];int delete = dp[i][j + 1];int replace = dp[i][j];dp[i + 1][j + 1] = Math.min(Math.min(insert, delete), replace) + 1;}}}return dp[m][n];}
}

73. 矩阵置零

题解

My AC java O(1) solution (easy to read)​

first row + first col

public class Solution {public void setZeroes(int[][] matrix) {boolean fr = false,fc = false;for(int i = 0; i < matrix.length; i++) {for(int j = 0; j < matrix[0].length; j++) {if(matrix[i][j] == 0) {if(i == 0) fr = true;if(j == 0) fc = true;matrix[0][j] = 0;matrix[i][0] = 0;}}}for(int i = 1; i < matrix.length; i++) {for(int j = 1; j < matrix[0].length; j++) {if(matrix[i][0] == 0 || matrix[0][j] == 0) {matrix[i][j] = 0;}}}if(fr) {for(int j = 0; j < matrix[0].length; j++) {matrix[0][j] = 0;}}if(fc) {for(int i = 0; i < matrix.length; i++) {matrix[i][0] = 0;}}}
}

74. 搜索二维矩阵

题解

官方题解​

二分查找

public class Solution {public boolean searchMatrix(int[][] matrix, int target) {if (matrix == null || matrix.length == 0) {return false;}int start = 0, rows = matrix.length, cols = matrix[0].length;int end = rows * cols - 1;while (start <= end) {int mid = (start + end) / 2;if (matrix[mid / cols][mid % cols] == target) {return true;} if (matrix[mid / cols][mid % cols] < target) {start = mid + 1;} else {end = mid - 1;}}return false;}
}

75. 颜色分类

题解

官方题解

swap ,先左后右剩下中

class Solution {public void sortColors(int[] A) {if(A==null || A.length<2) return;int low = 0; int high = A.length-1;for(int i = low; i<=high;) {if(A[i]==0) {// swap A[i] and A[low] and i,low both ++int temp = A[i];A[i] = A[low];A[low]=temp;i++;low++;}else if(A[i]==2) {//swap A[i] and A[high] and high--;int temp = A[i];A[i] = A[high];A[high]=temp;high--;}else {i++;}}}
}

LeetCode 71~75相关推荐

  1. LeetCode+ 71 - 75

    简化路径 算法标签:栈.字符串 给我们一个路径,要求把文件路径化简,给定的路径一定是合法的 Linux 路径,一个合法的 Linux 路径一般从 / 开始,/ 表示根目录,有很多的子目录 home.y ...

  2. LeetCode 71. Simplify Path

    LeetCode 71. Simplify Path 本博客转载自:https://blog.csdn.net/makuiyu/article/details/44497901 Solution1:没 ...

  3. LeetCode 71. 简化路径(栈)

    1. 题目 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (..) 表示将目录 ...

  4. LeetCode —— 71.简化路径(Python3)

    以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (-) 表示将目录切换到上一级( ...

  5. 【leetcode】75.颜色分类(多种解法,超详细图文解析)

    75. 颜色分类 难度中等 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,**原地**对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 ...

  6. 【leetcode】75. Sort Colors

    题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object):def s ...

  7. [LeetCode]--71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  8. [Leetcode][第75题][JAVA][颜色分类][双(三)指针][计数排序]

    [问题描述][中等] [解答思路] 1. 三指针 时间复杂度:O(N) 空间复杂度:O(1) class Solution {public void sortColors(int[] nums) {i ...

  9. 2022-3-19 Leetcode 71. 简化路径

    第一版 class Solution {public:string simplifyPath(string path) {vector<string> mysk;string ret;in ...

最新文章

  1. wds和dhcp分开做需要注意问题
  2. 每天学点Python之collections
  3. 第一次使用 Blog
  4. python暂停和恢复游戏_pygame游戏之旅 添加游戏暂停功能
  5. mysql 下载到其他盘中_MYSQL 如果把数据文件保存到其他磁盘里
  6. 低欲望社会有多可怕?仅94万!日本去年新生人口数创历史新低,空房子如瘟疫般蔓延...
  7. Netty与mina的比较
  8. 强化学习的数学基础2---PPO算法
  9. 多种方式创建 Entity Framework Core 上下文
  10. 使用ClassLoader加载资源详解
  11. 酒店管理系统web版/Java酒店管理系统
  12. win10不用密码登录及不显示“要使用本计算机,用户必须输入用户名和密码”的解决办法
  13. Ubuntu下安装Qt
  14. 3Dmax与BIM模型的区别
  15. 60秒Dapp快讯 | 全球公有链技术评估:以太坊在应用性上排第一;蚂蚁金服区块链试水医疗电子票据
  16. 怎样进行MySQL的配置
  17. oracle创建索引和删除索引
  18. python求斐波那契数列第n项
  19. sda、sdb、sda1、sda2的意思
  20. 在Linux环境下 nginx 部署vue打包项目

热门文章

  1. NLP算法-关键词提取之Gensim算法库
  2. 视频转gif(二):后端,云函数nodejs实现多图转gif
  3. Ultimaker不断为你解锁全新3D打印应用
  4. 电脑是如何进行工作的(一)?
  5. 关于区块链自我主权身份(SSI)的三个主要问题
  6. cesium 水位模拟_SuperMap iClient3D for WebGL教程- 淹没分析
  7. Neo4j 应用案例——保险欺诈
  8. 在线表单设计器都有哪些优秀的功能?
  9. html 打印预览跟实际不一样,打印预览和打印出来的不一样(总结自己遇到的问题和解决方法)...
  10. AMD黑苹果遇到的问题-机械键盘输入异常