LeetCode 2239.找到最接近 0 的数字:遍历

【LetMeFly】2239.找到最接近 0 的数字:遍历

力扣题目链接:https://leetcode.cn/problems/find-closest-number-to-zero/

给你一个长度为 n 的整数数组 nums ,请你返回 nums 中最 接近 0 的数字。如果有多个答案,请你返回它们中的 最大值

示例 1:

复制代码
输入:nums = [-4,-2,1,4,8]
输出:1
解释:
-4 到 0 的距离为 |-4| = 4 。
-2 到 0 的距离为 |-2| = 2 。
1 到 0 的距离为 |1| = 1 。
4 到 0 的距离为 |4| = 4 。
8 到 0 的距离为 |8| = 8 。
所以,数组中距离 0 最近的数字为 1 。

示例 2:

复制代码
输入:nums = [2,-1,1]
输出:1
解释:1 和 -1 都是距离 0 最近的数字,所以返回较大值 1 。

提示:

  • 1 <= n <= 1000
  • -105 <= nums[i] <= 105

解题方法:遍历

使用变量ans记录当前的最优答案,初始值为nums[0]

遍历nums数组,对于其中元素t,若以下则说明tans更优,更新anst

  1. abs(t) < abs(ans)
  2. abs(t) == abs(ans)ans < 0

最终返回ans

  • 时间复杂度 O ( l e n ( n u m s ) ) O(len(nums)) O(len(nums))
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
cpp 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-01-20 13:25:25
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-01-20 13:29:40
 */
class Solution {
public:
    int findClosestNumber(vector<int>& nums) {
        int ans = nums[0];
        for (int t : nums) {
            if (abs(t) < abs(ans) || abs(t) == abs(ans) && ans < 0) {
                ans = t;
            }
        }
        return ans;
    }
};
Python
python 复制代码
'''
Author: LetMeFly
Date: 2025-01-20 13:31:49
LastEditors: LetMeFly.xyz
LastEditTime: 2025-01-20 13:32:00
'''
from typing import List

class Solution:
    def findClosestNumber(self, nums: List[int]) -> int:
        ans = nums[0]
        for t in nums:
            if abs(t) < abs(ans) or abs(t) == abs(ans) and ans < 0:
                ans = t
        return ans
Java
java 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-01-20 13:33:12
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-01-20 13:33:16
 */
class Solution {
    public int findClosestNumber(int[] nums) {
        int ans = nums[0];
        for (int t : nums) {
            if (Math.abs(t) < Math.abs(ans) || Math.abs(t) == Math.abs(ans) && ans < 0) {
                ans = t;
            }
        }
        return ans;
    }
}
Go
go 复制代码
/*
 * @Author: LetMeFly
 * @Date: 2025-01-20 13:34:23
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2025-01-20 16:00:01
 */
package main

func abs(a int) int {
    if a < 0 {
        return -a
    }
    return a
}

func findClosestNumber(nums []int) int {
    ans := nums[0]
    for _, t := range nums {
        if abs(t) < abs(ans) || abs(t) == abs(ans) && ans < 0 {
            ans = t
        }
    }
    return ans
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/145264674

相关推荐
纪元A梦2 小时前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_2 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls2 小时前
C语言打印爱心
c语言·开发语言·算法
Python技术极客2 小时前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
吃着火锅x唱着歌3 小时前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun3 小时前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌3 小时前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
我星期八休息3 小时前
深入理解跳表(Skip List):原理、实现与应用
开发语言·数据结构·人工智能·python·算法·list
lingran__3 小时前
速通ACM省铜第四天 赋源码(G-C-D, Unlucky!)
c++·算法
haogexiaole4 小时前
贪心算法python
算法·贪心算法