1,题目
2,思路
循环遍历满足条件就记录,最后返回结果值
3,代码
java">public class Leetcode2239 {public static void main(String[] args) {System.out.println(new Solution2239().findClosestNumber(new int[]{-4, -2, 1, 4, 8}));}
}class Solution2239 {public int findClosestNumber(int[] nums) {int num = nums[0];for (int i = 1; i < nums.length; i++) {int a = Math.abs(num);int b = Math.abs(nums[i]);if (a == b) {num = num > nums[i] ? num : nums[i];} else {num = a > b ? nums[i] : num;}}return num;}
}