// 快速选择
import java.util.*;
class Solution {
public int findK(int[] arr, int k) {
int left = 0, right = arr.length - 1;
return fun1(arr, left, right, arr.length - k);
}
public int fun1(int[] arr, int left, int right, int k) {
if (left >= right) return arr[left];
int l = left - 1, r = right + 1, target = arr[left];
while (l < r) {
do l++;
while (l <= right && arr[l] < target);
do r--;
while (r >= left && arr[r] > target);
if (l < r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
if (k <= r) return fun1(arr, left, r, k);
else return fun1(arr, r + 1, right, k);
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
// 去掉首尾的方括号
str = str.substring(1, str.length() - 1);
String[] arr = str.split(",");
int[] nums = new int[arr.length];
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(arr[i]);
}
Solution solution = new Solution();
System.out.println(solution.findK(nums, 3));
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String line = "";
List<Integer> list = new ArrayList<>();
while (in.hasNextLine()) { // 注意 while 处理多个 case
line = in.nextLine();
String[] numStr = line.replace("[", "").replace("]", "").split(",");
for (String str : numStr) {
list.add(Integer.parseInt(str));
}
}
int k = 3;
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> (a - b));
for (Integer num : list) {
if (pq.size() < k) {
pq.offer(num);
} else {
if (num > pq.peek()) {
pq.poll();
pq.offer(num);
}
}
}
System.out.println(pq.peek());
}
} 我就偷个懒,直接解析字符串数字,不要数组了
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception {
String s = reader.readLine();
if( s!=null) {
out.println(parseIntArray(s));
}
out.flush();
}
public static int parseIntArray(String s) {
int mx1 = Integer.MIN_VALUE;
int mx2 = Integer.MIN_VALUE;
int mx3 = Integer.MIN_VALUE;
int base = 0;
int deep = 0;
int isNeg = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\n' || c == '\t' || c == '\f' || c == '\r' || c == ' ')
continue;
switch (c) {
case '[':
base = 0; // 初始化
deep++;
break;
case ']':
deep--;
if (deep == 0) {
if (isNeg > 0) {
base *= -1;
isNeg--;
}
if ( mx1 < base) {
mx3 = mx2;
mx2 = mx1;
mx1 = base;
} else if ( mx2 < base ) {
mx3 = mx2;
mx2 = base;
} else if ( mx3 < base ) {
mx3 = base;
}
}
break;
case '-':
isNeg++;
break;
case ',':
if (isNeg > 0) {
base *= -1;
isNeg--;
}
// arr[size++] = base;
if ( mx1 < base) {
mx3 = mx2;
mx2 = mx1;
mx1 = base;
} else if ( mx2 < base ) {
mx3 = mx2;
mx2 = base;
} else if ( mx3 < base ) {
mx3 = base;
}
base = 0;
break;
default:
if( c >= '0' && c <= '9') {
base = base * 10 + c - '0';
}
break;
}
}
return mx3;
}
}
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String[] str=input.nextLine().replace("[","").replace("]","").split(",");
int strInt[]=new int[str.length];
for(int i=0;i<str.length;i++){
strInt[i]=Integer.parseInt(str[i]);
}
Arrays.sort(strInt);
System.out.println(strInt[strInt.length-3]);
}
}
利用Arrays类的sort方法轻松解决
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] strings = in.nextLine().replace("[","").
replace("]","").split(",");
int[] a = new int[strings.length];
int n = a.length;
for (int i=0;i<n;i++)
a[i] = Integer.parseInt(strings[i]);
int k = n-3;
int t = -1;
int low = 0,high = n-1;
while (t != k){
t = partition(a,low,high);
if (t > k)
high = t-1;
else if (t < k)
low = t+1;
}
System.out.println(a[t]);
}
public static int partition(int[] a,int low,int high){
int key = a[low];
while (low < high){
while (low < high && key <= a[high])
high--;
if (low < high)
a[low++] = a[high];
while (low < high && a[low] <= key)
low++;
if (low < high)
a[high--] = a[low];
}
a[low] = key;
return low;
}
} 在k已确定,且k并不是很大时,可以用这种方法,只需一次遍历 public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] strings = in.nextLine().replace("[","").
replace("]","").split(",");
int[] a = new int[strings.length];
int n = a.length;
for (int i=0;i<n;i++)
a[i] = Integer.parseInt(strings[i]);
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
int third = Integer.MIN_VALUE;
for (int i=0;i<n;i++){
if (a[i] > first){
third = second;
second = first;
first = a[i];
}else if (a[i] > second){
third = second;
second = a[i];
}else if (a[i] > third){
third = a[i];
}
}
System.out.println(third);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
String[] strNumber = str.substring(1, str.length() - 1).split(",");
int[] number = new int[strNumber.length];
for (int i = 0; i < strNumber.length; i++) {
number[i] = Integer.parseInt(strNumber[i]);
}
Arrays.sort(number);
System.out.println(number[number.length -3]);
}
}