```java public class SecondLargestNumber { public static int findSecondLargest(int[] nums) { if (nums.length < 2) return -1; int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE; for (int num : nums) { if (num > max) { secondMax = max; max = num; } else if (num > secondMax && num != max) { secondMax = num; } } return (secondMax == Integer.MIN_VALUE) ? max : secondMax; } public static void main(String[] args) { int[] nums = {5, 3, 9, 10, 2, 7}; findSecondLargest(nums); // Output: 9 } } ```
点赞 1

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务