mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@ -8,6 +8,7 @@ package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
@ -22,6 +23,7 @@ class SolutionBruteForce {
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
|
||||
@ -29,6 +29,8 @@ public class worst_best_time_complexity {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user