英文:
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number.(Java)
问题
import java.util.Arrays;
import java.util.Scanner;
public class ThirdLargest {
public static void main(String[] args) {
int temp = 0;
int temp2 = 0;
int max = 0;
/*To get the length of the array from the user*/
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = sc.nextInt();
int[] myArray = new int[length];
System.out.println("Enter the elements of the array:");
/*Adding the numbers provided by the user as an array*/
for (int i = 0; i < length; i++) {
myArray[i] = sc.nextInt();
}
/*Sorting the array in descending order*/
Arrays.sort(myArray);
int distinctCount = 1;
/*Counting the number of distinct elements in the array*/
for (int i = length - 2; i >= 0; i--) {
if (myArray[i] != myArray[i + 1]) {
distinctCount++;
if (distinctCount == 3) {
System.out.println("The third maximum is: " + myArray[i]);
return;
}
}
}
System.out.println("The array does not have a third maximum element.");
}
}
英文:
For a given array input,the output should be the third maximum number from the list of numbers provided by the user.
Example 1:
Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]
Output: 2
I have shared my code below along with the comments.
import java.util.Arrays;
import java.util.Scanner;
public class ThirdLargest {
public static void main(String[] args) {
int temp = 0;
int temp2 = 0;
int max = 0;
/*To get the length of the array from the user*/
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = sc.nextInt();
int[] myArray = new int[length];
System.out.println("Enter the elements of the array:");
/*Adding the numbers provided by the user as an array*/
for (int i = 0; i < length; i++) {
myArray[i] = sc.nextInt();
}
System.out.println(Arrays.toString(myArray));
/*trying to get the maximum number from the array*/
for (int i = 0; i + 1 < length - 1; i++) {
if (myArray[i] < myArray[i + 1]) {
/*Storing the maximum number in temp*/
temp = myArray[i + 1];
} else
temp = myArray[i];
/*Trying to get the second maximum number from the array and storing it in temp2*/
if (myArray[i] > myArray[i + 1] && myArray[i] < temp)
temp2 = myArray[i];
/*Trying to get the third maximum number from the array and storing it as max*/
if (myArray[i] > myArray[i + 1] && myArray[i] < temp && myArray[i] < temp2)
max = myArray[i];
System.out.println(max);
}
}}
My result is:
I am getting the result as 0 and 0. I should actually get 2. Please help.
答案1
得分: 0
import java.util.Arrays;
import java.util.Scanner;
public class ThirdLargest {
public static void main(String[] args) {
int temp = 0;
int temp2 = 0;
int max = 0;
/*To get the length of the array from the user*/
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = sc.nextInt();
int[] myArray = new int[length];
System.out.println("Enter the elements of the array:");
/*Adding the numbers provided by the user as an array*/
for (int i = 0; i < length; i++) {
myArray[i] = sc.nextInt();
}
System.out.println(Arrays.toString(myArray));
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (myArray[i] > myArray[j]) {
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
}
}
System.out.println(myArray[2]);
英文:
import java.util.Arrays;
import java.util.Scanner;
public class ThirdLargest {
public static void main(String[] args) {
int temp = 0;
int temp2 = 0;
int max = 0;
/*To get the length of the array from the user*/
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the array");
int length = sc.nextInt();
int[] myArray = new int[length];
System.out.println("Enter the elements of the array:");
/*Adding the numbers provided by the user as an array*/
for (int i = 0; i < length; i++) {
myArray[i] = sc.nextInt();
}
System.out.println(Arrays.toString(myArray));
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (myArray[i] > myArray[j]) {
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
}
}
System.out.println(myArray[2]);
答案2
得分: 0
以下是翻译好的代码部分:
public static void main(String[] args) {
Random rr = new Random();
int[] a = rr.ints(10, 1, 100).toArray();
// 降序排列
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
int third = Integer.MIN_VALUE;
for (int v : a) {
if (first < v) {
third = second;
second = first;
first = v;
} else if (second < first && second < v) {
third = second;
second = v;
} else if (third < v) {
third = v;
}
}
int len = a.length;
System.out.println(len > 2 ? "第三大的数 = " + third :
len > 1 ? "第二大的数 = " + second :
"最大的数 = " + first);
System.out.println(Arrays.toString(a));
}
英文:
There is no need to sort the array to do this. It can be done in O(n) time by checking relative values as you iterate once thru the list.
public static void main(String[] args) {
Random rr = new Random();
int[] a = rr.ints(10, 1, 100).toArray();
// descending order
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
int third = Integer.MIN_VALUE;
for (int v : a) {
if (first < v) {
third = second;
second = first;
first = v;
} else if (second < first && second < v) {
third = second;
second = v;
} else if (third < v) {
third = v;
}
}
int len = a.length;
System.out.println(len > 2 ? "Third largest = " + third :
len > 1 ? "Second largest = " + second :
"Largest = " + first);
System.out.println(Arrays.toString(a));
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论