mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-27 13:33:43 +00:00
Update 算法 - 算法分析.md
三重循环执行次数的公式补上除以 6;倍率实验的结论与前文 ~N^3/6 保持一致;文字校对。
This commit is contained in:
+3
-3
@@ -72,7 +72,7 @@ public interface ThreeSum {
|
||||
|
||||
### 1. ThreeSumSlow
|
||||
|
||||
该算法的内循环为 `if (nums[i] + nums[j] + nums[k] == 0)` 语句,总共执行的次数为 N(N-1)(N-2) = N<sup>3</sup>/6-N<sup>2</sup>/2+N/3,因此它的近似执行次数为 \~N<sup>3</sup>/6,增长数量级为 O(N<sup>3</sup>)。
|
||||
该算法的内循环为 `if (nums[i] + nums[j] + nums[k] == 0)` 语句,总共执行的次数为 N(N-1)(N-2)/6 = N<sup>3</sup>/6-N<sup>2</sup>/2+N/3,因此它的近似执行次数为 \~N<sup>3</sup>/6,增长数量级为 O(N<sup>3</sup>)。
|
||||
|
||||
```java
|
||||
public class ThreeSumSlow implements ThreeSum {
|
||||
@@ -149,7 +149,7 @@ public class BinarySearch {
|
||||
|
||||
更有效的方法是先将数组排序,然后使用双指针进行查找,时间复杂度为 O(N<sup>2</sup>)。
|
||||
|
||||
同样不适用与数组存在重复元素的情况。
|
||||
同样不适用于数组存在重复元素的情况。
|
||||
|
||||
```java
|
||||
public class ThreeSumTwoPointer implements ThreeSum {
|
||||
@@ -194,7 +194,7 @@ public class ThreeSumTwoPointer implements ThreeSum {
|
||||
| 8000 | 33575 | 8.2 |
|
||||
| 16000 | 268909 | 8.0 |
|
||||
|
||||
可以看到,T(2N)/T(N) \~ 2<sup>3</sup>,因此可以确定 T(N) \~ aN<sup>3</sup>logN。
|
||||
可以看到,T(2N)/T(N) \~ 2<sup>3</sup>,因此可以确定 T(N) \~ aN<sup>3</sup>。
|
||||
|
||||
```java
|
||||
public class RatioTest {
|
||||
|
||||
Reference in New Issue
Block a user