CodingTour
ARTS #175

Algorithm

本周选择的算法题是:3Sum Closest

规则

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

Example 1:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Example 2:

Input: nums = [0,0,0], target = 1
Output: 0
Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).

Constraints:

  • 3 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • -104 <= target <= 104

Solution

impl Solution {
    pub fn three_sum_closest(nums: Vec<i32>, target: i32) -> i32 {
        let mut result = 10_i32.pow(5);

        let mut nums = nums;
        nums.sort();

        for i in 0..nums.len()-2 {
            let mut left_ptr = i + 1;
            let mut right_ptr = nums.len() - 1;

            while left_ptr < right_ptr {
                let sum = nums[i] + nums[left_ptr] + nums[right_ptr];
                if (sum - target).abs() < (result - target).abs() {
                    result = sum;
                }
                match sum.cmp(&target) {
                    std::cmp::Ordering::Greater => right_ptr -= 1,
                    std::cmp::Ordering::Less => left_ptr += 1,
                    std::cmp::Ordering::Equal => return sum
                }
            }
        }

        result
    }
}

Review

A Beginners Guide to Freelance AI-Based Generative Art Design

基于 AI 的艺术创作是目前比较火热的话题,它最吸引人的一点是:无论你会何种创作工具,你个人的创作能力如何,只要你愿意探索,AI 就能帮你实现最疯狂的想法。

李录曾经总结过 “人类就本性而言,情感上追求结果平等,理性上追求机会平等”,AI 创作显然也会继承互联网和数字媒体的特点,变成塑造我们社会的重要一环 —— 无论好坏。

Generative design is a great way to bring art, science, and business together. It can also help you create more efficient, usable, and beautiful products.

AI 创作的作品席卷世界只是时间问题。

Tip

推荐一款观星应用:Sky Guide,可以用来找星星以及寻找观星点:

Share

国庆在家期间设计了一套 1024 题目,分享其中一道 “找人” 的题目吧~

刻意把图片做了模糊处理:

额外提示:他是第二年轻的。