mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08:00
Add build script for JS and TS codes.
This commit is contained in:
@@ -7,38 +7,32 @@
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return { i, j };
|
||||
}
|
||||
vector<int> twoSumBruteForce(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
for (int j = i + 1; j < size; j++) {
|
||||
if (nums[i] + nums[j] == target)
|
||||
return { i, j };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
return {};
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap {
|
||||
public:
|
||||
vector<int> twoSum(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return { dic[target - nums[i]], i };
|
||||
}
|
||||
dic.emplace(nums[i], i);
|
||||
vector<int> twoSumHashTable(vector<int>& nums, int target) {
|
||||
int size = nums.size();
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// 单层循环,时间复杂度 O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return { dic[target - nums[i]], i };
|
||||
}
|
||||
return {};
|
||||
dic.emplace(nums[i], i);
|
||||
}
|
||||
};
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
@@ -48,19 +42,13 @@ int main() {
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
SolutionBruteForce* slt1 = new SolutionBruteForce();
|
||||
vector<int> res = slt1->twoSum(nums, target);
|
||||
vector<int> res = twoSumBruteForce(nums, target);
|
||||
cout << "方法一 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
// 方法二
|
||||
SolutionHashMap* slt2 = new SolutionHashMap();
|
||||
res = slt2->twoSum(nums, target);
|
||||
res = twoSumHashTable(nums, target);
|
||||
cout << "方法二 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
// 释放内存
|
||||
delete slt1;
|
||||
delete slt2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_computational_complexity
|
||||
{
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce
|
||||
public class leetcode_two_sum
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
/* 方法一:暴力枚举 */
|
||||
public static int[] twoSumBruteForce(int[] nums, int target)
|
||||
{
|
||||
int size = nums.Length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
@@ -25,12 +25,9 @@ namespace hello_algo.chapter_computational_complexity
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap
|
||||
{
|
||||
public int[] twoSum(int[] nums, int target)
|
||||
/* 方法二:辅助哈希表 */
|
||||
public static int[] twoSumHashTable(int[] nums, int target)
|
||||
{
|
||||
int size = nums.Length;
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
@@ -46,10 +43,7 @@ namespace hello_algo.chapter_computational_complexity
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
public class leetcode_two_sum
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
@@ -59,13 +53,11 @@ namespace hello_algo.chapter_computational_complexity
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
SolutionBruteForce slt1 = new SolutionBruteForce();
|
||||
int[] res = slt1.twoSum(nums, target);
|
||||
int[] res = twoSumBruteForce(nums, target);
|
||||
Console.WriteLine("方法一 res = " + string.Join(",", res));
|
||||
// 方法二
|
||||
SolutionHashMap slt2 = new SolutionHashMap();
|
||||
res = slt2.twoSum(nums, target);
|
||||
res = twoSumHashTable(nums, target);
|
||||
Console.WriteLine("方法二 res = " + string.Join(",", res));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,10 @@ package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
class SolutionBruteForce {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
|
||||
public class leetcode_two_sum {
|
||||
/* 方法一:暴力枚举 */
|
||||
static int[] twoSumBruteForce(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for (int i = 0; i < size - 1; i++) {
|
||||
@@ -21,11 +22,9 @@ class SolutionBruteForce {
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
class SolutionHashMap {
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
/* 方法二:辅助哈希表 */
|
||||
static int[] twoSumHashTable(int[] nums, int target) {
|
||||
int size = nums.length;
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
Map<Integer, Integer> dic = new HashMap<>();
|
||||
@@ -38,9 +37,7 @@ class SolutionHashMap {
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
}
|
||||
|
||||
public class leetcode_two_sum {
|
||||
public static void main(String[] args) {
|
||||
// ======= Test Case =======
|
||||
int[] nums = { 2,7,11,15 };
|
||||
@@ -48,12 +45,10 @@ public class leetcode_two_sum {
|
||||
|
||||
// ====== Driver Code ======
|
||||
// 方法一
|
||||
SolutionBruteForce slt1 = new SolutionBruteForce();
|
||||
int[] res = slt1.twoSum(nums, target);
|
||||
int[] res = twoSumBruteForce(nums, target);
|
||||
System.out.println("方法一 res = " + Arrays.toString(res));
|
||||
// 方法二
|
||||
SolutionHashMap slt2 = new SolutionHashMap();
|
||||
res = slt2.twoSum(nums, target);
|
||||
res = twoSumHashTable(nums, target);
|
||||
System.out.println("方法二 res = " + Arrays.toString(res));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
/* 随机访问元素 */
|
||||
/* 随机返回一个数组元素 */
|
||||
function randomAccess(nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
const random_index = Math.floor(Math.random() * nums.length);
|
||||
|
||||
@@ -44,7 +44,7 @@ class LinkedListStack {
|
||||
/* 访问栈顶元素 */
|
||||
peek() {
|
||||
if (!this.#stackPeek)
|
||||
throw new Error("栈为空!");
|
||||
throw new Error("栈为空");
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,26 +9,24 @@ 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]:
|
||||
# 两层循环,时间复杂度 O(n^2)
|
||||
for i in range(len(nums) - 1):
|
||||
for j in range(i + 1, len(nums)):
|
||||
if nums[i] + nums[j] == target:
|
||||
return i, j
|
||||
return []
|
||||
def two_sum_brute_force(nums: List[int], target: int) -> List[int]:
|
||||
# 两层循环,时间复杂度 O(n^2)
|
||||
for i in range(len(nums) - 1):
|
||||
for j in range(i + 1, len(nums)):
|
||||
if nums[i] + nums[j] == target:
|
||||
return i, j
|
||||
return []
|
||||
|
||||
""" 方法二:辅助哈希表 """
|
||||
class SolutionHashMap:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
# 辅助哈希表,空间复杂度 O(n)
|
||||
dic = {}
|
||||
# 单层循环,时间复杂度 O(n)
|
||||
for i in range(len(nums)):
|
||||
if target - nums[i] in dic:
|
||||
return dic[target - nums[i]], i
|
||||
dic[nums[i]] = i
|
||||
return []
|
||||
def two_sum_hash_table(nums: List[int], target: int) -> List[int]:
|
||||
# 辅助哈希表,空间复杂度 O(n)
|
||||
dic = {}
|
||||
# 单层循环,时间复杂度 O(n)
|
||||
for i in range(len(nums)):
|
||||
if target - nums[i] in dic:
|
||||
return dic[target - nums[i]], i
|
||||
dic[nums[i]] = i
|
||||
return []
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
@@ -39,8 +37,8 @@ if __name__ == '__main__':
|
||||
|
||||
# ====== Driver Code ======
|
||||
# 方法一
|
||||
res = SolutionBruteForce().twoSum(nums, target)
|
||||
res = two_sum_brute_force(nums, target)
|
||||
print("方法一 res =", res)
|
||||
# 方法二
|
||||
res = SolutionHashMap().twoSum(nums, target)
|
||||
res = two_sum_hash_table(nums, target)
|
||||
print("方法二 res =", res)
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
const binarySearch = function (nums: number[], target: number): number {
|
||||
function binarySearch(nums: number[], target: number): number {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
let i = 0, j = nums.length - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
@@ -23,7 +23,7 @@ const binarySearch = function (nums: number[], target: number): number {
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
const binarySearch1 = function (nums: number[], target: number): number {
|
||||
function binarySearch1(nums: number[], target: number): number {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0, j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
|
||||
Reference in New Issue
Block a user