前言
本文隸屬于專欄《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++;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/353371.html
標籤:java
上一篇:LeetCode 76~80
下一篇:java虛擬機面經總結
