mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 02:21:30 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 暴力解法 */
|
||||
/* 方法一:暴力枚举 */
|
||||
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
|
||||
for (int i = 0; i < numsSize; ++i) {
|
||||
for (int j = i + 1; j < numsSize; ++j) {
|
||||
@@ -49,7 +49,7 @@ void insert(hashTable *h, int key, int val) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
||||
hashTable *hashtable = NULL;
|
||||
for (int i = 0; i < numsSize; i++) {
|
||||
|
||||
@@ -27,6 +27,8 @@ int *randomNumbers(int n) {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(int *nums, int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1) return i;
|
||||
}
|
||||
return -1;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
@@ -21,6 +22,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
|
||||
@@ -23,6 +23,8 @@ vector<int> randomNumbers(int n) {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(vector<int>& nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_computational_complexity
|
||||
{
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
@@ -26,6 +27,7 @@ namespace hello_algo.chapter_computational_complexity
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
|
||||
@@ -37,6 +37,8 @@ namespace hello_algo.chapter_computational_complexity
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
// twoSumBruteForce
|
||||
/* 方法一:暴力枚举 */
|
||||
func twoSumBruteForce(nums []int, target int) []int {
|
||||
size := len(nums)
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
@@ -18,7 +18,7 @@ func twoSumBruteForce(nums []int, target int) []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
// twoSumHashTable
|
||||
/* 方法二:辅助哈希表 */
|
||||
func twoSumHashTable(nums []int, target int) []int {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
hashTable := map[int]int{}
|
||||
|
||||
@@ -26,6 +26,8 @@ func randomNumbers(n int) []int {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums []int) int {
|
||||
for i := 0; i < len(nums); i++ {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: gyt95 (gytkwan@gmail.com)
|
||||
*/
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
function twoSumBruteForce(nums, target) {
|
||||
const n = nums.length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
@@ -17,6 +18,7 @@ function twoSumBruteForce(nums, target) {
|
||||
return [];
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
function twoSumHashTable(nums, target) {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let m = {};
|
||||
|
||||
@@ -24,6 +24,8 @@ function randomNumbers(n) {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
function findOne(nums) {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] === 1) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
||||
""" 方法一:暴力枚举 """
|
||||
class SolutionBruteForce:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
for i in range(len(nums) - 1):
|
||||
@@ -16,6 +17,7 @@ class SolutionBruteForce:
|
||||
return i, j
|
||||
return []
|
||||
|
||||
""" 方法二:辅助哈希表 """
|
||||
class SolutionHashMap:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
dic = {}
|
||||
|
||||
@@ -19,6 +19,8 @@ def random_numbers(n):
|
||||
""" 查找数组 nums 中数字 1 所在索引 """
|
||||
def find_one(nums):
|
||||
for i in range(len(nums)):
|
||||
# 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
# 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1:
|
||||
return i
|
||||
return -1
|
||||
|
||||
@@ -8,7 +8,7 @@ import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
||||
|
||||
""" AVL 树 """
|
||||
class AVLTree:
|
||||
def __init__(self, root: Optional[TreeNode] = None):
|
||||
self.root = root
|
||||
|
||||
@@ -8,6 +8,7 @@ use std::collections::HashMap;
|
||||
struct SolutionBruteForce;
|
||||
struct SolutionHashMap;
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
impl SolutionBruteForce {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
for i in 0..nums.len() - 1 {
|
||||
@@ -21,6 +22,7 @@ impl SolutionBruteForce {
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
impl SolutionHashMap {
|
||||
pub fn two_sum(nums: &Vec<i32>, target: i32) -> Vec<i32> {
|
||||
let mut hm = HashMap::new();
|
||||
|
||||
@@ -19,6 +19,8 @@ fn random_numbers(n: i32) -> Vec<i32> {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
fn find_one(nums: &[i32]) -> Option<usize> {
|
||||
for i in 0..nums.len() {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return Some(i);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for i in nums.indices.dropLast() {
|
||||
@@ -16,6 +17,7 @@ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
return [0]
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
var dic: [Int: Int] = [:]
|
||||
|
||||
@@ -16,6 +16,8 @@ func randomNumbers(n: Int) -> [Int] {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums: [Int]) -> Int {
|
||||
for i in nums.indices {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: gyt95 (gytkwan@gmail.com)
|
||||
*/
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
function twoSumBruteForce(nums: number[], target: number): number[] {
|
||||
const n = nums.length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
@@ -17,6 +18,7 @@ function twoSumBruteForce(nums: number[], target: number): number[] {
|
||||
return [];
|
||||
};
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
function twoSumHashTable(nums: number[], target: number): number[] {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
let m: Map<number, number> = new Map();
|
||||
|
||||
@@ -24,6 +24,8 @@ function randomNumbers(n: number): number[] {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
function findOne(nums: number[]): number {
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] === 1) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
|
||||
// 方法一:暴力枚举
|
||||
const SolutionBruteForce = struct {
|
||||
pub fn twoSum(self: *SolutionBruteForce, nums: []i32, target: i32) [2]i32 {
|
||||
_ = self;
|
||||
@@ -23,6 +24,7 @@ const SolutionBruteForce = struct {
|
||||
}
|
||||
};
|
||||
|
||||
// 方法二:辅助哈希表
|
||||
const SolutionHashMap = struct {
|
||||
pub fn twoSum(self: *SolutionHashMap, nums: []i32, target: i32) ![2]i32 {
|
||||
_ = self;
|
||||
|
||||
@@ -21,6 +21,8 @@ pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
// 查找数组 nums 中数字 1 所在索引
|
||||
pub fn findOne(nums: []i32) i32 {
|
||||
for (nums) |num, i| {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (num == 1) return @intCast(i32, i);
|
||||
}
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user