2019-03-08 21:41:45 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
* [10.1 斐波那契数列](#101-斐波那契数列)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [10.2 矩形覆盖](#102-矩形覆盖)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [10.3 跳台阶](#103-跳台阶)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [10.4 变态跳台阶](#104-变态跳台阶)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [动态规划](#动态规划)
|
|
|
|
|
* [数学推导](#数学推导)
|
|
|
|
|
* [11. 旋转数组的最小数字](#11-旋转数组的最小数字)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [12. 矩阵中的路径](#12-矩阵中的路径)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [13. 机器人的运动范围](#13-机器人的运动范围)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [14. 剪绳子](#14-剪绳子)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [贪心](#贪心)
|
|
|
|
|
* [动态规划](#动态规划)
|
|
|
|
|
* [15. 二进制中 1 的个数](#15-二进制中-1-的个数)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [n&(n-1)](#n&n-1)
|
|
|
|
|
* [Integer.bitCount()](#integerbitcount)
|
|
|
|
|
* [16. 数值的整数次方](#16-数值的整数次方)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [17. 打印从 1 到最大的 n 位数](#17-打印从-1-到最大的-n-位数)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [18.1 在 O(1) 时间内删除链表节点](#181-在-o1-时间内删除链表节点)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
* [18.2 删除链表中重复的结点](#182-删除链表中重复的结点)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题描述](#解题描述)
|
|
|
|
|
* [19. 正则表达式匹配](#19-正则表达式匹配)
|
|
|
|
|
* [题目描述](#题目描述)
|
|
|
|
|
* [解题思路](#解题思路)
|
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 10.1 斐波那契数列
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
求斐波那契数列的第 n 项,n <= 39。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<!--<div align="center"><img src="https://latex.codecogs.com/gif.latex?f(n)=\left\{\begin{array}{rcl}0&&{n=0}\\1&&{n=1}\\f(n-1)+f(n-2)&&{n>1}\end{array}\right."/></div> <br> -->
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/45be9587-6069-4ab7-b9ac-840db1a53744.jpg"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
如果使用递归求解,会重复计算一些子问题。例如,计算 f(10) 需要计算 f(9) 和 f(8),计算 f(9) 需要计算 f(8) 和 f(7),可以看到 f(8) 被重复计算了。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/_u6590_u6CE2_u90A3_u5951_u6570_u5217.gif" width="400"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
递归是将一个问题划分成多个子问题求解,动态规划也是如此,但是动态规划会把子问题的解缓存起来,从而避免重复求解子问题。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int Fibonacci(int n) {
|
|
|
|
|
if (n <= 1)
|
|
|
|
|
return n;
|
|
|
|
|
int[] fib = new int[n + 1];
|
|
|
|
|
fib[1] = 1;
|
|
|
|
|
for (int i = 2; i <= n; i++)
|
|
|
|
|
fib[i] = fib[i - 1] + fib[i - 2];
|
|
|
|
|
return fib[n];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
考虑到第 i 项只与第 i-1 和第 i-2 项有关,因此只需要存储前两项的值就能求解第 i 项,从而将空间复杂度由 O(N) 降低为 O(1)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int Fibonacci(int n) {
|
|
|
|
|
if (n <= 1)
|
|
|
|
|
return n;
|
|
|
|
|
int pre2 = 0, pre1 = 1;
|
|
|
|
|
int fib = 0;
|
|
|
|
|
for (int i = 2; i <= n; i++) {
|
|
|
|
|
fib = pre2 + pre1;
|
|
|
|
|
pre2 = pre1;
|
|
|
|
|
pre1 = fib;
|
|
|
|
|
}
|
|
|
|
|
return fib;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
由于待求解的 n 小于 40,因此可以将前 40 项的结果先进行计算,之后就能以 O(1) 时间复杂度得到第 n 项的值了。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public class Solution {
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private int[] fib = new int[40];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public Solution() {
|
|
|
|
|
fib[1] = 1;
|
|
|
|
|
fib[2] = 2;
|
|
|
|
|
for (int i = 2; i < fib.length; i++)
|
|
|
|
|
fib[i] = fib[i - 1] + fib[i - 2];
|
|
|
|
|
}
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int Fibonacci(int n) {
|
|
|
|
|
return fib[n];
|
|
|
|
|
}
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 10.2 矩形覆盖
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
我们可以用 2\*1 的小矩形横着或者竖着去覆盖更大的矩形。请问用 n 个 2\*1 的小矩形无重叠地覆盖一个 2\*n 的大矩形,总共有多少种方法?
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/d1ed87eb-da5a-4728-b0dc-e3705aa028ea.gif"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int RectCover(int n) {
|
|
|
|
|
if (n <= 2)
|
|
|
|
|
return n;
|
|
|
|
|
int pre2 = 1, pre1 = 2;
|
|
|
|
|
int result = 0;
|
|
|
|
|
for (int i = 3; i <= n; i++) {
|
|
|
|
|
result = pre2 + pre1;
|
|
|
|
|
pre2 = pre1;
|
|
|
|
|
pre1 = result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 10.3 跳台阶
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/8c82a5b80378478f9484d87d1c5f12a4?tpId=13&tqId=11161&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/a0e90bd3-747d-4c3a-8fa0-179c59eeded0_200.png"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int JumpFloor(int n) {
|
|
|
|
|
if (n <= 2)
|
|
|
|
|
return n;
|
|
|
|
|
int pre2 = 1, pre1 = 2;
|
|
|
|
|
int result = 1;
|
|
|
|
|
for (int i = 2; i < n; i++) {
|
|
|
|
|
result = pre2 + pre1;
|
|
|
|
|
pre2 = pre1;
|
|
|
|
|
pre1 = result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 10.4 变态跳台阶
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/22243d016f6b47f2a6928b4313c85387?tpId=13&tqId=11162&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级... 它也可以跳上 n 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/cbd5f6f6-18de-4711-9e01-0f94e66f81b8_200.png"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### 动态规划
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int JumpFloorII(int target) {
|
|
|
|
|
int[] dp = new int[target];
|
|
|
|
|
Arrays.fill(dp, 1);
|
|
|
|
|
for (int i = 1; i < target; i++)
|
|
|
|
|
for (int j = 0; j < i; j++)
|
|
|
|
|
dp[i] += dp[j];
|
|
|
|
|
return dp[target - 1];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### 数学推导
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
跳上 n-1 级台阶,可以从 n-2 级跳 1 级上去,也可以从 n-3 级跳 2 级上去...,那么
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```
|
2019-03-08 21:41:45 +08:00
|
|
|
|
f(n-1) = f(n-2) + f(n-3) + ... + f(0)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
同样,跳上 n 级台阶,可以从 n-1 级跳 1 级上去,也可以从 n-2 级跳 2 级上去... ,那么
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```
|
2019-03-08 21:41:45 +08:00
|
|
|
|
f(n) = f(n-1) + f(n-2) + ... + f(0)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
综上可得
|
|
|
|
|
|
|
|
|
|
```
|
2019-03-08 21:41:45 +08:00
|
|
|
|
f(n) - f(n-1) = f(n-1)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
即
|
|
|
|
|
|
|
|
|
|
```
|
2019-03-08 21:41:45 +08:00
|
|
|
|
f(n) = 2*f(n-1)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
所以 f(n) 是一个等比数列
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```source-java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int JumpFloorII(int target) {
|
|
|
|
|
return (int) Math.pow(2, target - 1);
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 11. 旋转数组的最小数字
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
例如数组 {3, 4, 5, 1, 2} 为 {1, 2, 3, 4, 5} 的一个旋转,该数组的最小值为 1。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
在一个有序数组中查找一个元素可以用二分查找,二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度都为 O(logN)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
本题可以修改二分查找算法进行求解:
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
- 当 nums[m] <= nums[h] 的情况下,说明解在 [l, m] 之间,此时令 h = m;
|
|
|
|
|
- 否则解在 [m + 1, h] 之间,令 l = m + 1。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int minNumberInRotateArray(int[] nums) {
|
|
|
|
|
if (nums.length == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[m] <= nums[h])
|
|
|
|
|
h = m;
|
|
|
|
|
else
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
return nums[l];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
如果数组元素允许重复的话,那么就会出现一个特殊的情况:nums[l] == nums[m] == nums[h],那么此时无法确定解在哪个区间,需要切换到顺序查找。例如对于数组 {1,1,1,0,1},l、m 和 h 指向的数都为 1,此时无法知道最小数字 0 在哪个区间。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int minNumberInRotateArray(int[] nums) {
|
|
|
|
|
if (nums.length == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
int l = 0, h = nums.length - 1;
|
|
|
|
|
while (l < h) {
|
|
|
|
|
int m = l + (h - l) / 2;
|
|
|
|
|
if (nums[l] == nums[m] && nums[m] == nums[h])
|
|
|
|
|
return minNumber(nums, l, h);
|
|
|
|
|
else if (nums[m] <= nums[h])
|
|
|
|
|
h = m;
|
|
|
|
|
else
|
|
|
|
|
l = m + 1;
|
|
|
|
|
}
|
|
|
|
|
return nums[l];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private int minNumber(int[] nums, int l, int h) {
|
|
|
|
|
for (int i = l; i < h; i++)
|
|
|
|
|
if (nums[i] > nums[i + 1])
|
|
|
|
|
return nums[i + 1];
|
|
|
|
|
return nums[l];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 12. 矩阵中的路径
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&tqId=11218&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
例如下面的矩阵包含了一条 bfce 路径。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/2_2001550466182933.png"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
|
|
|
|
|
private int rows;
|
|
|
|
|
private int cols;
|
|
|
|
|
|
|
|
|
|
public boolean hasPath(char[] array, int rows, int cols, char[] str) {
|
|
|
|
|
if (rows == 0 || cols == 0)
|
|
|
|
|
return false;
|
|
|
|
|
this.rows = rows;
|
|
|
|
|
this.cols = cols;
|
|
|
|
|
boolean[][] marked = new boolean[rows][cols];
|
|
|
|
|
char[][] matrix = buildMatrix(array);
|
|
|
|
|
for (int i = 0; i < rows; i++)
|
|
|
|
|
for (int j = 0; j < cols; j++)
|
|
|
|
|
if (backtracking(matrix, str, marked, 0, i, j))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private boolean backtracking(char[][] matrix, char[] str, boolean[][] marked, int pathLen, int r, int c) {
|
|
|
|
|
if (pathLen == str.length)
|
|
|
|
|
return true;
|
|
|
|
|
if (r < 0 || r >= rows || c < 0 || c >= cols || matrix[r][c] != str[pathLen] || marked[r][c])
|
|
|
|
|
return false;
|
|
|
|
|
marked[r][c] = true;
|
|
|
|
|
for (int[] n : next)
|
|
|
|
|
if (backtracking(matrix, str, marked, pathLen + 1, r + n[0], c + n[1]))
|
|
|
|
|
return true;
|
|
|
|
|
marked[r][c] = false;
|
|
|
|
|
return false;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private char[][] buildMatrix(char[] array) {
|
|
|
|
|
char[][] matrix = new char[rows][cols];
|
|
|
|
|
for (int i = 0, idx = 0; i < rows; i++)
|
|
|
|
|
for (int j = 0; j < cols; j++)
|
|
|
|
|
matrix[i][j] = array[idx++];
|
|
|
|
|
return matrix;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 13. 机器人的运动范围
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&tqId=11219&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
地上有一个 m 行和 n 列的方格。一个机器人从坐标 (0, 0) 的格子开始移动,每一次只能向左右上下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于 k 的格子。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
例如,当 k 为 18 时,机器人能够进入方格 (35,37),因为 3+5+3+7=18。但是,它不能进入方格 (35,38),因为 3+5+3+8=19。请问该机器人能够达到多少个格子?
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private static final int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
|
|
|
|
|
private int cnt = 0;
|
|
|
|
|
private int rows;
|
|
|
|
|
private int cols;
|
|
|
|
|
private int threshold;
|
|
|
|
|
private int[][] digitSum;
|
|
|
|
|
|
|
|
|
|
public int movingCount(int threshold, int rows, int cols) {
|
|
|
|
|
this.rows = rows;
|
|
|
|
|
this.cols = cols;
|
|
|
|
|
this.threshold = threshold;
|
|
|
|
|
initDigitSum();
|
|
|
|
|
boolean[][] marked = new boolean[rows][cols];
|
|
|
|
|
dfs(marked, 0, 0);
|
|
|
|
|
return cnt;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private void dfs(boolean[][] marked, int r, int c) {
|
|
|
|
|
if (r < 0 || r >= rows || c < 0 || c >= cols || marked[r][c])
|
|
|
|
|
return;
|
|
|
|
|
marked[r][c] = true;
|
|
|
|
|
if (this.digitSum[r][c] > this.threshold)
|
|
|
|
|
return;
|
|
|
|
|
cnt++;
|
|
|
|
|
for (int[] n : next)
|
|
|
|
|
dfs(marked, r + n[0], c + n[1]);
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private void initDigitSum() {
|
|
|
|
|
int[] digitSumOne = new int[Math.max(rows, cols)];
|
|
|
|
|
for (int i = 0; i < digitSumOne.length; i++) {
|
|
|
|
|
int n = i;
|
|
|
|
|
while (n > 0) {
|
|
|
|
|
digitSumOne[i] += n % 10;
|
|
|
|
|
n /= 10;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.digitSum = new int[rows][cols];
|
|
|
|
|
for (int i = 0; i < this.rows; i++)
|
|
|
|
|
for (int j = 0; j < this.cols; j++)
|
|
|
|
|
this.digitSum[i][j] = digitSumOne[i] + digitSumOne[j];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 14. 剪绳子
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[Leetcode](https://leetcode.com/problems/integer-break/description/)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
把一根绳子剪成多段,并且使得每段的长度乘积最大。
|
|
|
|
|
|
|
|
|
|
```html
|
2019-03-08 21:41:45 +08:00
|
|
|
|
n = 2
|
|
|
|
|
return 1 (2 = 1 + 1)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
n = 10
|
|
|
|
|
return 36 (10 = 3 + 3 + 4)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### 贪心
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
尽可能多剪长度为 3 的绳子,并且不允许有长度为 1 的绳子出现。如果出现了,就从已经切好长度为 3 的绳子中拿出一段与长度为 1 的绳子重新组合,把它们切成两段长度为 2 的绳子。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
证明:当 n >= 5 时,3(n - 3) - n = 2n - 9 > 0,且 2(n - 2) - n = n - 4 > 0。因此在 n >= 5 的情况下,将绳子剪成一段为 2 或者 3,得到的乘积会更大。又因为 3(n - 3) - 2(n - 2) = n - 5 >= 0,所以剪成一段长度为 3 比长度为 2 得到的乘积更大。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int integerBreak(int n) {
|
|
|
|
|
if (n < 2)
|
|
|
|
|
return 0;
|
|
|
|
|
if (n == 2)
|
|
|
|
|
return 1;
|
|
|
|
|
if (n == 3)
|
|
|
|
|
return 2;
|
|
|
|
|
int timesOf3 = n / 3;
|
|
|
|
|
if (n - timesOf3 * 3 == 1)
|
|
|
|
|
timesOf3--;
|
|
|
|
|
int timesOf2 = (n - timesOf3 * 3) / 2;
|
|
|
|
|
return (int) (Math.pow(3, timesOf3)) * (int) (Math.pow(2, timesOf2));
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### 动态规划
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int integerBreak(int n) {
|
|
|
|
|
int[] dp = new int[n + 1];
|
|
|
|
|
dp[1] = 1;
|
|
|
|
|
for (int i = 2; i <= n; i++)
|
|
|
|
|
for (int j = 1; j < i; j++)
|
|
|
|
|
dp[i] = Math.max(dp[i], Math.max(j * (i - j), dp[j] * (i - j)));
|
|
|
|
|
return dp[n];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 15. 二进制中 1 的个数
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=13&tqId=11164&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
输入一个整数,输出该数二进制表示中 1 的个数。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### n&(n-1)
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
该位运算去除 n 的位级表示中最低的那一位。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```
|
2019-03-08 21:41:45 +08:00
|
|
|
|
n : 10110100
|
|
|
|
|
n-1 : 10110011
|
|
|
|
|
n&(n-1) : 10110000
|
2019-03-08 21:29:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
时间复杂度:O(M),其中 M 表示 1 的个数。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int NumberOf1(int n) {
|
|
|
|
|
int cnt = 0;
|
|
|
|
|
while (n != 0) {
|
|
|
|
|
cnt++;
|
|
|
|
|
n &= (n - 1);
|
|
|
|
|
}
|
|
|
|
|
return cnt;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
### Integer.bitCount()
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public int NumberOf1(int n) {
|
|
|
|
|
return Integer.bitCount(n);
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 16. 数值的整数次方
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
给定一个 double 类型的浮点数 base 和 int 类型的整数 exponent,求 base 的 exponent 次方。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
下面的讨论中 x 代表 base,n 代表 exponent。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<!--<div align="center"><img src="https://latex.codecogs.com/gif.latex?x^n=\left\{\begin{array}{rcl}(x*x)^{n/2}&&{n\%2=0}\\x*(x*x)^{n/2}&&{n\%2=1}\end{array}\right."/></div> <br>-->
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/48b1d459-8832-4e92-938a-728aae730739.jpg"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
因为 (x\*x)<sup>n/2</sup> 可以通过递归求解,并且每次递归 n 都减小一半,因此整个算法的时间复杂度为 O(logN)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public double Power(double base, int exponent) {
|
|
|
|
|
if (exponent == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
if (exponent == 1)
|
|
|
|
|
return base;
|
|
|
|
|
boolean isNegative = false;
|
|
|
|
|
if (exponent < 0) {
|
|
|
|
|
exponent = -exponent;
|
|
|
|
|
isNegative = true;
|
|
|
|
|
}
|
|
|
|
|
double pow = Power(base * base, exponent / 2);
|
|
|
|
|
if (exponent % 2 != 0)
|
|
|
|
|
pow = pow * base;
|
|
|
|
|
return isNegative ? 1 / pow : pow;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 17. 打印从 1 到最大的 n 位数
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数即 999。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
由于 n 可能会非常大,因此不能直接用 int 表示数字,而是用 char 数组进行存储。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
使用回溯法得到所有的数。
|
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public void print1ToMaxOfNDigits(int n) {
|
|
|
|
|
if (n <= 0)
|
|
|
|
|
return;
|
|
|
|
|
char[] number = new char[n];
|
|
|
|
|
print1ToMaxOfNDigits(number, 0);
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private void print1ToMaxOfNDigits(char[] number, int digit) {
|
|
|
|
|
if (digit == number.length) {
|
|
|
|
|
printNumber(number);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
|
number[digit] = (char) (i + '0');
|
|
|
|
|
print1ToMaxOfNDigits(number, digit + 1);
|
|
|
|
|
}
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
private void printNumber(char[] number) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (index < number.length && number[index] == '0')
|
|
|
|
|
index++;
|
|
|
|
|
while (index < number.length)
|
|
|
|
|
System.out.print(number[index++]);
|
|
|
|
|
System.out.println();
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 18.1 在 O(1) 时间内删除链表节点
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
① 如果该节点不是尾节点,那么可以直接将下一个节点的值赋给该节点,然后令该节点指向下下个节点,再删除下一个节点,时间复杂度为 O(1)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/27ff9548-edb6-4465-92c8-7e6386e0b185.png" width="600"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
② 如果链表只有一个节点,那么直接
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
② 否则,就需要先遍历链表,找到节点的前一个节点,然后让前一个节点指向 null,时间复杂度为 O(N)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/280f7728-594f-4811-a03a-fa8d32c013da.png" width="600"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:56:42 +08:00
|
|
|
|
综上,如果进行 N 次操作,那么大约需要操作节点的次数为 N-1+N=2N-1,其中 N-1 表示 N-1 个不是尾节点的每个节点以 O(1) 的时间复杂度操作节点的总次数,N 表示 1 个尾节点以 O(N) 的时间复杂度操作节点的总次数。(2N-1)/N \~ 2,因此该算法的平均时间复杂度为 O(1)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public ListNode deleteNode(ListNode head, ListNode tobeDelete) {
|
|
|
|
|
if (head == null || tobeDelete == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (tobeDelete.next != null) {
|
|
|
|
|
// 要删除的节点不是尾节点
|
|
|
|
|
ListNode next = tobeDelete.next;
|
|
|
|
|
tobeDelete.val = next.val;
|
|
|
|
|
tobeDelete.next = next.next;
|
|
|
|
|
} else {
|
|
|
|
|
if (head == tobeDelete)
|
|
|
|
|
// 只有一个节点
|
|
|
|
|
head = null;
|
|
|
|
|
else {
|
|
|
|
|
ListNode cur = head;
|
|
|
|
|
while (cur.next != tobeDelete)
|
|
|
|
|
cur = cur.next;
|
|
|
|
|
cur.next = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return head;
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 18.2 删除链表中重复的结点
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&tqId=11209&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
<div align="center"> <img src="pics/8433fbb2-c35c-45ef-831d-e3ca42aebd51.png" width="500"/> </div><br>
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public ListNode deleteDuplication(ListNode pHead) {
|
|
|
|
|
if (pHead == null || pHead.next == null)
|
|
|
|
|
return pHead;
|
|
|
|
|
ListNode next = pHead.next;
|
|
|
|
|
if (pHead.val == next.val) {
|
|
|
|
|
while (next != null && pHead.val == next.val)
|
|
|
|
|
next = next.next;
|
|
|
|
|
return deleteDuplication(next);
|
|
|
|
|
} else {
|
|
|
|
|
pHead.next = deleteDuplication(pHead.next);
|
|
|
|
|
return pHead;
|
|
|
|
|
}
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
# 19. 正则表达式匹配
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 题目描述
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
请实现一个函数用来匹配包括 '.' 和 '\*' 的正则表达式。模式中的字符 '.' 表示任意一个字符,而 '\*' 表示它前面的字符可以出现任意次(包含 0 次)。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串 "aaa" 与模式 "a.a" 和 "ab\*ac\*a" 匹配,但是与 "aa.a" 和 "ab\*a" 均不匹配。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
## 解题思路
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
2019-03-08 21:41:45 +08:00
|
|
|
|
应该注意到,'.' 是用来当做一个任意字符,而 '\*' 是用来重复前面的字符。这两个的作用不同,不能把 '.' 的作用和 '\*' 进行类比,从而把它当成重复前面字符一次。
|
2019-03-08 21:29:22 +08:00
|
|
|
|
|
|
|
|
|
```java
|
2019-03-08 21:41:45 +08:00
|
|
|
|
public boolean match(char[] str, char[] pattern) {
|
|
|
|
|
|
|
|
|
|
int m = str.length, n = pattern.length;
|
|
|
|
|
boolean[][] dp = new boolean[m + 1][n + 1];
|
|
|
|
|
|
|
|
|
|
dp[0][0] = true;
|
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
|
|
if (pattern[i - 1] == '*')
|
|
|
|
|
dp[0][i] = dp[0][i - 2];
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= m; i++)
|
|
|
|
|
for (int j = 1; j <= n; j++)
|
|
|
|
|
if (str[i - 1] == pattern[j - 1] || pattern[j - 1] == '.')
|
|
|
|
|
dp[i][j] = dp[i - 1][j - 1];
|
|
|
|
|
else if (pattern[j - 1] == '*')
|
|
|
|
|
if (pattern[j - 2] == str[i - 1] || pattern[j - 2] == '.') {
|
|
|
|
|
dp[i][j] |= dp[i][j - 1]; // a* counts as single a
|
|
|
|
|
dp[i][j] |= dp[i - 1][j]; // a* counts as multiple a
|
|
|
|
|
dp[i][j] |= dp[i][j - 2]; // a* counts as empty
|
|
|
|
|
} else
|
|
|
|
|
dp[i][j] = dp[i][j - 2]; // a* only counts as empty
|
|
|
|
|
|
|
|
|
|
return dp[m][n];
|
2019-03-08 21:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2019-03-09 23:55:18 +08:00
|
|
|
|
<div align="center">欢迎关注公众号,获取最新文章!</div></br>
|
2019-03-09 23:50:23 +08:00
|
|
|
|
<div align="center"><img width="150px" src="https://cyc-1256109796.cos.ap-guangzhou.myqcloud.com/%E5%85%AC%E4%BC%97%E5%8F%B7.jpg"></img></div>
|