百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

用100道题拿下你的算法面试(001):两数之和,找出给定和的数对

ztj100 2025-06-23 23:42 12 浏览 0 评论

一、面试问题

给定一个包含 n 个整数的数组 arr[],以及一个目标值 target,任务是判断数组中是否存在一对元素,其和等于目标值。这个问题是 Two Sum(两数之和)问题的一种变体。

举例:

输入:arr[] = [0, -1, 2, -3, 1],target = -2
输出:true
解释: 存在一对数 (1, -3),它们的和等于给定目标值:1 + (-3) = -2。

输入:arr[] = [1, -2, 1, 0, 5],target = 0
输出:false
解释: 没有任何一对数的和等于目标值。

二、【朴素方法】生成所有可能的数对 — 时间复杂度 O(n^2),空间复杂度 O(1)

(一) 算法思路与步骤

最基础的方法是生成所有可能的数对,并检查其中是否有一对的和等于目标值。为了生成所有的数对,我们只需运行两个嵌套的循环。

(二) JavaScript

// 函数用于检查是否存在一对数,其和等于给定的目标值
function twoSum(arr, target) {
  let n = arr.length;

  // 遍历数组中的每一个元素
  for (let i = 0; i < n; i++) {

    // 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
    for (let j = i + 1; j < n; j++) {

      // 检查当前数对的和是否等于目标值
      if (arr[i] + arr[j] === target) {
        return true;
      }
    }
  }
  // 如果检查完所有可能的组合后仍未找到符合条件的数对
  return false;
}

// 测试代码
let arr = [0, -1, 2, -3, 1];
let target = -2;

if (twoSum(arr, target))
  console.log("true");
else 
  console.log("false");

(三) C++

#include <iostream>
#include <vector>
using namespace std;

// 函数用于检查是否存在一对数,其和等于给定的目标值
bool twoSum(vector<int> &arr, int target) {
    int n = arr.size();

    // 遍历数组中的每一个元素
    for (int i = 0; i < n; i++) {

        // 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
        for (int j = i + 1; j < n; j++) {

            // 检查当前数对的和是否等于目标值
            if (arr[i] + arr[j] == target) {
                return true;
            }
        }
    }

    // 如果检查完所有可能的组合后仍未找到符合条件的数对
    return false;
}

int main() {

    vector<int> arr = {0, -1, 2, -3, 1};
    int target = -2;

    // 调用 twoSum 函数并输出结果
    if(twoSum(arr, target))
        cout << "true";
    else
        cout << "false";

    return 0;
}

(四) C

#include <stdbool.h>
#include <stdio.h>

// 函数用于检查是否存在一对数,其和等于给定的目标值
bool twoSum(int arr[], int n, int target){

    // 遍历数组中的每一个元素
    for (int i = 0; i < n; i++){

        // 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
        for (int j = i + 1; j < n; j++){

            // 检查当前数对的和是否等于目标值
            if (arr[i] + arr[j] == target)
                return true;
        }
    }
    // 如果检查完所有可能的组合后仍未找到符合条件的数对
    return false;
}

int main(){

    int arr[] = {0, -1, 2, -3, 1};
    int target = -2;
    int n = sizeof(arr) / sizeof(arr[0]);

    // 调用 twoSum 函数并打印结果
    if (twoSum(arr, n, target))
        printf("true\n");
    else
        printf("false\n");

    return 0;
}

(五) JAVA

class GfG {

    // 函数用于检查是否存在一对数,其和等于给定的目标值
    static boolean twoSum(int[] arr, int target){
        int n = arr.length;

        // 遍历数组中的每一个元素
        for (int i = 0; i < n; i++) {

            // 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
            for (int j = i + 1; j < n; j++) {

                // 检查当前数对的和是否等于目标值
                if (arr[i] + arr[j] == target) {
                    return true;
                }
            }
        }

        // 如果检查完所有可能的组合后仍未找到符合条件的数对
        return false;
    }

    public static void main(String[] args){

        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用 twoSum 函数并打印结果
        if (twoSum(arr, target))
            System.out.println("true");
        else
            System.out.println("false");
    }
}

(六) Python

# 函数用于检查是否存在一对数,其和等于给定的目标值
def twoSum(arr, target):
    n = len(arr)

    # 遍历数组中的每一个元素
    for i in range(n):

        # 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
        for j in range(i + 1, n):

            # 检查当前数对的和是否等于目标值
            if arr[i] + arr[j] == target:
                return True

    # 如果检查完所有可能的组合后仍未找到符合条件的数对
    return False

if __name__ == "__main__":
    arr = [0, -1, 2, -3, 1]
    target = -2

    if twoSum(arr, target):
        print("true")
    else:
        print("false")

(七) C#

using System;

class GfG {

    // 函数用于检查是否存在一对数,其和等于给定的目标值
    static bool twoSum(int[] arr, int target) {
        int n = arr.Length;

        // 遍历数组中的每一个元素
        for (int i = 0; i < n; i++) {

            // 对于每个元素 arr[i],检查其后面的每个元素 arr[j]
            for (int j = i + 1; j < n; j++) {

                // 检查当前数对的和是否等于目标值
                if (arr[i] + arr[j] == target) {
                    return true;
                }
            }
        }

        // 如果检查完所有可能的组合后仍未找到符合条件的数对
        return false;
    }

    static void Main() {

        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用 twoSum 函数并打印结果
        if (twoSum(arr, target))
            Console.WriteLine("true");
        else 
            Console.WriteLine("false");
    }
}

输出:

true

时间复杂度:O(n^2),因为使用了两个嵌套循环
空间复杂度:O(1)

三、【更优方法 1】排序加二分查找 — 时间复杂度 O(n*log(n)),空间复杂度 O(1)

(一) 算法思路与步骤

我们也可以使用二分查找来解决这个问题。众所周知,在有序数组中查找元素的时间复杂度是 O(log(n))。我们首先对数组进行排序。然后对于数组中的每个元素 arr[i],计算它的补数(即 target - 当前元素),并使用二分查找在索引 i 之后的子数组中快速判断这个补数是否存在。如果找到补数,则返回 true;如果遍历所有元素后都未找到补数,则返回 false。

(二) JavaScript

// 执行二分查找的函数
function binarySearch(arr, left, right, target) {
  while (left <= right) {
    let mid = Math.floor(left + (right - left) / 2);

    if (arr[mid] === target)
      return true;
    if (arr[mid] < target)
      left = mid + 1;
    else
      right = mid - 1;
  }
  return false;
}

// 判断是否存在任意一对数字
// 它们的和等于给定的目标值
function twoSum(arr, target) {
  // 对数组进行排序
  arr.sort((a, b) => a - b);

  // 遍历数组中的每一个元素
  for (let i = 0; i < arr.length; i++) {
    let complement = target - arr[i];

    // 使用二分查找寻找补数
    if (binarySearch(arr, i + 1, arr.length - 1, complement))
      return true;
  }
  // 如果没有找到符合条件的数字对
  return false;
}

// 驱动代码
let arr = [0, -1, 2, -3, 1];
let target = -2;

if (twoSum(arr, target)) {
  console.log("true");
} else {
  console.log("false");
}

(三) C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 执行二分查找的函数
bool binarySearch(vector<int> &arr, int left, int right, int target){
    while (left <= right){
        int mid = left + (right - left) / 2;

        if (arr[mid] == target)
            return true;
        if (arr[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return false;
}

// 判断是否存在任意一对数字
// 它们的和等于给定的目标值
bool twoSum(vector<int> &arr, int target){

    // 对数组进行排序
    sort(arr.begin(), arr.end());

    // 遍历数组中的每一个元素
    for (int i = 0; i < arr.size(); i++){
        int complement = target - arr[i];

        // 使用二分查找寻找补数
        if (binarySearch(arr, i + 1, arr.size() - 1, complement))
            return true;
    }

    // 如果没有找到符合条件的数字对
    return false;
}

int main(){
    vector<int> arr = {0, -1, 2, -3, 1};
    int target = -2;

    if (twoSum(arr, target))
        cout << "true";
    else
        cout << "false";

    return 0;
}

(四) C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// 比较函数,用于qsort排序
int compare(const void *a, const void *b){
    return (*(int *)a - *(int *)b);
}

// 执行二分查找的函数
bool binarySearch(int arr[], int left, int right, int target){
    while (left <= right){
        int mid = left + (right - left) / 2;

        if (arr[mid] == target)
            return true;
        if (arr[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }
    return false;
}

// 判断是否存在任意一对数字
// 它们的和等于给定的目标值
bool twoSum(int arr[], int n, int target){
    // 对数组进行排序
    qsort(arr, n, sizeof(int), compare);

    // 遍历数组中的每一个元素
    for (int i = 0; i < n; i++){
        int complement = target - arr[i];

        // 使用二分查找寻找补数
        if (binarySearch(arr, i + 1, n - 1, complement))
            return true;
    }
    // 如果没有找到符合条件的数字对
    return false;
}

int main(){
    int arr[] = {0, -1, 2, -3, 1};
    int target = -2;
    int n = sizeof(arr) / sizeof(arr[0]);

    if (twoSum(arr, n, target))
        printf("true\n");
    else
        printf("false\n");

    return 0;
}

(五) JAVA

import java.util.Arrays;

class GfG {

    // 执行二分查找的函数
    static boolean binarySearch(int[] arr, int left,
                                int right, int target){
        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] == target)
                return true;
            if (arr[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return false;
    }

    // 判断是否存在任意一对数字
    // 它们的和等于给定的目标值
    static boolean twoSum(int[] arr, int target){

        // 对数组进行排序
        Arrays.sort(arr);

        // 遍历数组中的每一个元素
        for (int i = 0; i < arr.length; i++) {
            int complement = target - arr[i];

            // 使用二分查找寻找补数
            if (binarySearch(arr, i + 1, arr.length - 1,
                             complement))
                return true;
        }
        // 如果没有找到符合条件的数字对
        return false;
    }

    public static void main(String[] args){
        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        if (twoSum(arr, target)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
}

(六) Python

# Function to perform binary search
def binary_search(arr, left, right, target):
    while left <= right:
        mid = left + (right - left) // 2

        if arr[mid] == target:
            return True
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return False

# Function to check whether any pair exists
# whose sum is equal to the given target value
def twoSum(arr, target):
    arr.sort()

    # Iterate through each element in the array
    for i in range(len(arr)):
        complement = target - arr[i]

        # Use binary search to find the complement
        if binary_search(arr, i + 1, len(arr) - 1, complement):
            return True
    # If no pair is found
    return False
  	
if __name__ == "__main__":
    arr = [0, -1, 2, -3, 1]
    target = -2

    if twoSum(arr, target):
        print("true")
    else:
        print("false")

(七) C#

using System;

class GfG {

    // 执行二分查找的函数
    static bool binarySearch(int[] arr, int left, int right,
                             int target){
        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] == target)
                return true;
            if (arr[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return false;
    }

    // 判断是否存在任意一对数字
    // 它们的和等于给定的目标值
    static bool twoSum(int[] arr, int target){
        // 对数组进行排序
        Array.Sort(arr);

        // 遍历数组中的每一个元素
        for (int i = 0; i < arr.Length; i++) {
            int complement = target - arr[i];

            // 使用二分查找寻找补数
            if (binarySearch(arr, i + 1, arr.Length - 1,
                             complement))
                return true;
        }
        // 如果没有找到符合条件的数字对
        return false;
    }

    static void Main(){
        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        if (twoSum(arr, target)) {
            Console.WriteLine("true");
        }
        else {
            Console.WriteLine("false");
        }
    }
}

四、【更优方法 2】排序加双指针技巧 — 时间复杂度 O(n*log(n)),空间复杂度 O(1)

思路是使用双指针技巧,但使用双指针技巧前,数组必须是已排序的。数组排序后,我们可以用这种方法:一个指针指向数组开头(左指针),另一个指针指向数组末尾(右指针)。然后检查这两个指针所指元素的和:

  • 如果和等于目标值,说明找到了符合条件的数对。
  • 如果和小于目标值,左指针向右移动以增加和。
  • 如果和大于目标值,右指针向左移动以减小和。

(一) 算法思路与步骤

(二) JavaScript

// 函数:检查是否存在任意一对元素,其和等于目标值
function twoSum(arr, target)
{
    // 对数组进行排序
    arr.sort((a, b) => a - b);

    let left = 0, right = arr.length - 1;

    // 当左指针小于右指针时进行遍历
    while (left < right) {
        let sum = arr[left] + arr[right];

        // 检查当前两个元素的和是否等于目标值
        if (sum === target)
            return true;
        else if (sum < target)
            left++; // 和小于目标值,左指针右移
        else
            right--; // 和大于目标值,右指针左移
    }
    // 如果没有找到满足条件的元素对
    return false;
}

let arr = [ 0, -1, 2, -3, 1 ];
let target = -2;

// 调用 twoSum 函数并打印结果
if (twoSum(arr, target)) {
    console.log("true");
} else {
    console.log("false");
}

(三) C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 函数:检查是否存在任意一对元素,其和等于目标值
bool twoSum(vector<int> &arr, int target){
  
    // 对数组进行排序
    sort(arr.begin(), arr.end());

    int left = 0, right = arr.size() - 1;

    // 当左指针小于右指针时进行遍历
    while (left < right){
        int sum = arr[left] + arr[right];

        // 检查当前两个元素的和是否等于目标值
        if (sum == target)
            return true;
        else if (sum < target)
            left++; // 和小于目标值,左指针右移
        else
            right--; // 和大于目标值,右指针左移
    }
    // 如果没有找到满足条件的元素对
    return false;
}

int main(){
    vector<int> arr = {0, -1, 2, -3, 1};
    int target = -2;

    // 调用 twoSum 函数并打印结果
    if (twoSum(arr, target))
        cout << "true";
    else
        cout << "false";

    return 0;
}

(四) C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// qsort的比较函数
int compare(const void *a, const void *b){
    return (*(int *)a - *(int *)b);
}

// 函数:检查是否存在任意一对元素,其和等于目标值
bool twoSum(int arr[], int n, int target){
    // 对数组进行排序
    qsort(arr, n, sizeof(int), compare);

    int left = 0, right = n - 1;

    // 当左指针小于右指针时进行遍历
    while (left < right){
        int sum = arr[left] + arr[right];

        // 检查当前两个元素的和是否等于目标值
        if (sum == target)
            return true;
        else if (sum < target)
            left++; // 和小于目标值,左指针右移
        else
            right--; // 和大于目标值,右指针左移
    }
    // 如果没有找到满足条件的元素对
    return false;
}

int main(){
    int arr[] = {0, -1, 2, -3, 1};
    int target = -2;
    int n = sizeof(arr) / sizeof(arr[0]);

    // 调用 twoSum 函数并打印结果
    if (twoSum(arr, n, target))
        printf("true\n"); 
    else
        printf("false\n");

    return 0;
}

(五) JAVA

import java.util.Arrays;

class GfG {

    // 函数:检查是否存在任意一对元素,其和等于给定的目标值
    static boolean twoSum(int[] arr, int target){
        // 对数组进行排序
        Arrays.sort(arr);

        int left = 0, right = arr.length - 1;

        // 当左指针小于右指针时遍历
        while (left < right) {
            int sum = arr[left] + arr[right];

            // 检查当前元素和是否等于目标值
            if (sum == target)
                return true;
            else if (sum < target)
                left++; // 和小于目标值,左指针右移
            else
                right--; // 和大于目标值,右指针左移
        }
        // 如果没有找到满足条件的元素对
        return false;
    }

    public static void main(String[] args){
        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用 twoSum 函数并打印结果
        if (twoSum(arr, target)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
}

(六) Python

# 函数:检查是否存在任意一对元素,其和等于给定的目标值
def two_sum(arr, target):
    # 对数组进行排序
    arr.sort()

    left, right = 0, len(arr) - 1

    # 当左指针小于右指针时遍历
    while left < right:
        sum = arr[left] + arr[right]

        # 检查当前元素和是否等于目标值
        if sum == target:
            return True
        elif sum < target: 
            left += 1  # 和小于目标值,左指针右移
        else:
            right -= 1 # 和大于目标值,右指针左移

    # 如果没有找到满足条件的元素对
    return False

arr = [0, -1, 2, -3, 1]
target = -2

# 调用 two_sum 函数并打印结果
if two_sum(arr, target):
    print("true")
else:
    print("false")

(七) C#

using System;
using System.Linq;

class GfG {

    // 函数用于检查是否存在一对数
    // 它们的和等于给定的目标值
    static bool TwoSum(int[] arr, int target){
      
        // 对数组进行排序
        Array.Sort(arr);

        int left = 0, right = arr.Length - 1;

        // 当左指针小于右指针时迭代
        while (left < right) {
            int sum = arr[left] + arr[right];

            // 检查和是否等于目标值
            if (sum == target)
                return true;
            else if (sum < target)
                left++; // 将左指针向右移动
            else
                right--; // 将右指针向左移动
        }
        // 如果没有找到满足条件的数对
        return false;
    }

    static void Main(){
        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用 TwoSum 函数并输出结果
        if (TwoSum(arr, target))
            Console.WriteLine("true");
        else 
            Console.WriteLine("false");
    }
}

输出:

true
  • 时间复杂度 (Time Complexity): O(n log n),主要开销来自对数组的排序操作。
  • 空间复杂度 (Auxiliary Space): O(1),双指针方法在排序后只使用了常数级别的额外空间。

注意:这种方法是针对已排序数组的最佳方法。但如果数组未排序,则应使用下面的方法。

五、【推荐方法】使用哈希集合 — 时间复杂度 O(n),空间复杂度 O(n)

(一) 算法思路与步骤

哈希法为2Sum问题提供了更高效的解决方案。我们不再检查所有可能的数对,而是在遍历数组元素时,将每个数字存入一个无序集合中。对于每个数字,我们计算其补数(即目标值减去当前数字),并检查该补数是否存在于集合中。如果存在,则说明找到了和为目标值的数对。该方法显著降低了时间复杂度,使我们能够在线性时间 O(n) 内解决该问题。

步骤方法:

  1. 创建一个空的哈希集合(Hash Set)或无序集合(Unordered Set)。
  2. 遍历数组,对于数组中的每个数字:
    • 计算补数(target - 当前数字)。
    • 检查补数是否存在于集合中:
      • 如果存在,说明找到了满足条件的数对。
      • 如果不存在,则将当前数字加入集合中。
  1. 如果遍历结束仍未找到满足条件的数对,则返回不存在这样的数对。

(二) JavaScript

// 函数用于检查是否存在一对数字
// 其和等于给定的目标值
function twoSum(arr, target) {

    // 创建一个集合来存储元素
    let set = new Set();

    // 遍历数组中的每个元素
    for (let num of arr) {
    
        // 计算补数,即与当前数字相加
        // 等于目标值的数
        let complement = target - num;

        // 检查补数是否存在于集合中
        if (set.has(complement)) {
            return true;
        }

        // 将当前元素添加到集合中
        set.add(num);
    }
    // 如果没有找到满足条件的数对
    return false;
}

let arr = [0, -1, 2, -3, 1];
let target = -2;

// 调用 twoSum 函数并打印结果
if (twoSum(arr, target))
    console.log("true");
else 
    console.log("false");

(三) C++

#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

// 函数用于检查是否存在一对数字
// 其和等于给定的目标值
bool twoSum(vector<int> &arr, int target){
  
    // 创建一个无序集合用于存储元素
    unordered_set<int> s;

    // 遍历向量中的每个元素
    for (int i = 0; i < arr.size(); i++){

        // 计算补数,即与当前数字相加
        // 等于目标值的数
        int complement = target - arr[i];

        // 检查补数是否存在于集合中
        if (s.find(complement) != s.end())
            return true;

        // 将当前元素插入集合
        s.insert(arr[i]);
    }
  
    // 如果没有找到满足条件的数对
    return false;
}

int main(){
    vector<int> arr = {0, -1, 2, -3, 1};
    int target = -2;

    if (twoSum(arr, target))
        cout << "true";
    else
        cout << "false";

    return 0;
}

(四) JAVA

import java.util.HashSet;

class GfG {

    /**
     * 判断数组中是否存在两个数之和等于目标值
     * 时间复杂度:O(n),只需遍历数组一次
     * 空间复杂度:O(n),用于存储哈希集合中的元素
     *
     * @param arr    输入数组
     * @param target 目标和
     * @return 是否存在满足条件的两个数
     */
    static boolean twoSum(int[] arr, int target){

        // 创建HashSet存储元素
        HashSet<Integer> set = new HashSet<>();

        // 遍历数组中的每个元素
        for (int i = 0; i < arr.length; i++) {

            // 计算目标和与当前元素的差值
            int complement = target - arr[i];

            // 判断差值是否已经存在于HashSet中
            if (set.contains(complement)) {
                return true;
            }

            // 当前元素加入HashSet
            set.add(arr[i]);
        }
        // 未找到符合条件的两个数
        return false;
    }

    public static void main(String[] args){

        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用twoSum函数并输出结果
        if (twoSum(arr, target))
            System.out.println("true");  // 输出:true
        else
            System.out.println("false");
    }
}

(五) Python

# 函数用于检查是否存在一对数字
# 其和等于给定的目标值
def twoSum(arr, target):
  
    # 创建一个集合用于存储元素
    s = set()

    # 遍历数组中的每个元素
    for num in arr:
      
        # 计算补数,即与当前数字相加
        # 等于目标值的数
        complement = target - num

        # 检查补数是否存在于集合中
        if complement in s:
            return True

        # 将当前元素加入集合
        s.add(num)

    # 如果没有找到满足条件的数对
    return False

arr = [0, -1, 2, -3, 1]
target = -2

# 调用twoSum函数并打印结果
if twoSum(arr, target):
    print("true")
else:
    print("false")

(六) C#

using System;
using System.Collections.Generic;

class GfG {

    // 函数用于检查是否存在一对数字
    // 其和等于给定的目标值
    static bool TwoSum(int[] arr, int target){

        // 创建一个HashSet用于存储元素
        HashSet<int> set = new HashSet<int>();

        // 遍历数组中的每个元素
        for (int i = 0; i < arr.Length; i++) {

            // 计算补数,即与当前数字相加
            // 等于目标值的数
            int complement = target - arr[i];

            // 检查补数是否存在于HashSet中
            if (set.Contains(complement))
                return true;

            // 将当前元素加入HashSet
            set.Add(arr[i]);
        }
        // 如果没有找到满足条件的数对
        return false;
    }

    static void Main(){
        int[] arr = { 0, -1, 2, -3, 1 };
        int target = -2;

        // 调用TwoSum函数并打印结果
        if (TwoSum(arr, target))
            Console.WriteLine("true");
        else 
            Console.WriteLine("false"); 
    }
}

输出

true

时间复杂度:O(n),因为只需遍历数组一次。

空间复杂度:O(n),用于存储哈希集合中的元素。

--THE END--

相关推荐

其实TensorFlow真的很水无非就这30篇熬夜练

好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...

交叉验证和超参数调整:如何优化你的机器学习模型

准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...

机器学习交叉验证全指南:原理、类型与实战技巧

机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...

深度学习中的类别激活热图可视化

作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...

超强,必会的机器学习评估指标

大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...

机器学习入门教程-第六课:监督学习与非监督学习

1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...

Python教程(三十八):机器学习基础

...

Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置

你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...

超全面讲透一个算法模型,高斯核!!

...

神经网络与传统统计方法的简单对比

传统的统计方法如...

AI 基础知识从0.1到0.2——用“房价预测”入门机器学习全流程

...

自回归滞后模型进行多变量时间序列预测

下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...

苹果AI策略:慢哲学——科技行业的“长期主义”试金石

苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...

时间序列预测全攻略,6大模型代码实操

如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...

AI 基础知识从 0.4 到 0.5—— 计算机视觉之光 CNN

...

取消回复欢迎 发表评论: