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

相关推荐
SuperCandyXu5 分钟前
leetcode0310. 最小高度树-medium
数据结构·c++·算法·leetcode
鱼嘻21 分钟前
线程邮箱框架与示例
linux·c语言·开发语言·算法·php
有一个好名字29 分钟前
力扣:多数元素
算法·leetcode·职场和发展
pystraf42 分钟前
P2572 [SCOI2010] 序列操作 Solution
数据结构·算法·线段树·洛谷
吗喽对你问好1 小时前
华为5.7机考-最小代价相遇的路径规划Java题解
算法·华为
Trent19851 小时前
影楼精修-牙齿美型修复算法解析
算法
小王努力学编程2 小时前
高并发内存池(二):项目的整体框架以及Thread_Cache的结构设计
开发语言·c++·学习·算法
补三补四3 小时前
遗传算法(GA)
人工智能·算法·机器学习·启发式算法
dot to one3 小时前
C++ 渗透 数据结构中的二叉搜索树
数据结构·c++·算法·visual studio
好易学·数据结构4 小时前
可视化图解算法36: 序列化二叉树-I(二叉树序列化与反序列化)
数据结构·算法·leetcode·二叉树·力扣·序列化·牛客