mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
parent
592965595e
commit
70dead5cd0
@ -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;
|
||||
|
@ -2432,22 +2432,13 @@ $$
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("打乱后的数组为 " + Arrays.toString(nums));
|
||||
System.out.println("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -2471,25 +2462,13 @@ $$
|
||||
/* 查找数组 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;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n数组 [ 1, 2, ..., n ] 被打乱后 = ";
|
||||
PrintUtil::printVector(nums);
|
||||
cout << "数字 1 的索引为 " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -2506,18 +2485,11 @@ $$
|
||||
""" 查找数组 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
|
||||
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
for i in range(10):
|
||||
n = 100
|
||||
nums = random_numbers(n)
|
||||
index = find_one(nums)
|
||||
print("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
||||
print("数字 1 的索引为", index)
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -2540,23 +2512,14 @@ $$
|
||||
/* 查找数组 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
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func main() {
|
||||
for i := 0; i < 10; i++ {
|
||||
n := 100
|
||||
nums := randomNumbers(n)
|
||||
index := findOne(nums)
|
||||
fmt.Println("\n数组 [ 1, 2, ..., n ] 被打乱后 =", nums)
|
||||
fmt.Println("数字 1 的索引为", index)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -2582,25 +2545,14 @@ $$
|
||||
/* 查找数组 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;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
function main() {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let n = 100;
|
||||
let nums = randomNumbers(n);
|
||||
let index = findOne(nums);
|
||||
console.log(
|
||||
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
|
||||
);
|
||||
console.log("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "TypeScript"
|
||||
@ -2626,25 +2578,14 @@ $$
|
||||
/* 查找数组 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;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
function main(): void {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let n = 100;
|
||||
let nums = randomNumbers(n);
|
||||
let index = findOne(nums);
|
||||
console.log(
|
||||
"\n数组 [ 1, 2, ..., n ] 被打乱后 = [" + nums.join(", ") + "]"
|
||||
);
|
||||
console.log("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C"
|
||||
@ -2671,31 +2612,12 @@ $$
|
||||
/* 查找数组 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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main(int argc, char *argv[]) {
|
||||
// 初始化随机数种子
|
||||
srand((unsigned int)time(NULL));
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int *nums = randomNumbers(n);
|
||||
int index = findOne(nums, n);
|
||||
printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = ");
|
||||
printArray(nums, n);
|
||||
printf("数字 1 的索引为 %d\n", index);
|
||||
// 释放堆区内存
|
||||
if (nums != NULL) {
|
||||
free(nums);
|
||||
nums = NULL;
|
||||
}
|
||||
}
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@ -2728,24 +2650,13 @@ $$
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void main(String[] args)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
Console.WriteLine("\n数组 [ 1, 2, ..., n ] 被打乱后 = " + string.Join(",", nums));
|
||||
Console.WriteLine("数字 1 的索引为 " + index);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@ -2763,23 +2674,14 @@ $$
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums: [Int]) -> Int {
|
||||
for i in nums.indices {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func main() {
|
||||
for _ in 0 ..< 10 {
|
||||
let n = 100
|
||||
let nums = randomNumbers(n: n)
|
||||
let index = findOne(nums: nums)
|
||||
print("数组 [ 1, 2, ..., n ] 被打乱后 = \(nums)")
|
||||
print("数字 1 的索引为 \(index)")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
Loading…
Reference in New Issue
Block a user