LeetCode 71~75

前言

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

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

正文

幕布

幕布链接

71. 简化路径

题解

Java 10-lines solution with stack​

一点两点特殊处理

import scala.collection.mutable.ListBuffer

object 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,算法)