Leetcode Diary

74. Search a 2D Matrix

Leetcode Diary

Problem Description Thoughts 将二维数组转换成一个一维数组,然后进行binary search即可 (x,y) <=> index(m表示行数,n表示列数): \(index = x * n + y \tag{1}\) \(x = index / n \\ \tag{2} y = index \% n\) public boolea...

73. Set Matrix Zeroes

Leetcode Diary

Problem Description Thoughts in-place 所以只能利用第一行和第一列作为需要置为0的标示 遍历matrix找到所有0的坐标,并且标示它所在的行和列 从1开始遍历行和列,把所有需要置为0的都设置好 最后设置首行和首列 public void setZeroes(int[][] matrix) { int m = matrix....

72. Edit Distance

Leetcode Diary

Problem Description Thoughts Dynamic Programming dp[i][j]表示word1的第i个字符和word2的第j个字符适配的edit distance是多少 则有动态规划方程: \[dp[i][j] = \begin{cases} Math.min(dp[i-1][j-1]+1,dp[i-1][j]+1,dp[i][j-1...

71. Simplify Path

Leetcode Diary

Problem Description Thoughts 把path split 成一个个子路径 使用一个stack 遍历所有的路径 当子路径为.和空时,不进栈 当子路径为..时,pop出上一层路径 最后再从bottom到top遍历栈,把路径拼装回去 public String simplifyPath(String p...

68. Text Justification

Leetcode Diary

https://leetcode.com/problems/text-justification/ Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right)...

70. Climbing Stairs

Leetcode Diary

You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Example Input: n = 2 Outpu...

69. Sqrt(x)

Leetcode Diary

Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncate...

67. Add Binary

Leetcode Diary

Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Constraints: Each string consists only of ‘...

66. Plus One

Leetcode Diary

Given a non-empty array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and...

65. Valid Number

Leetcode Diary

Validate if a given string can be interpreted as a decimal number. Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implement...