import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); TreeSet<Integer> ts = new TreeSet<>(); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); for (int i = 0; i < n; i++) { ts.add(Integer.parseInt(s[i])); } int k = Integer.parseInt(br.readLine()); System.out.println(ts.toArray()[k-1]); } }
//使用hash import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n=sc.nextInt(); int []arr=new int[1000]; for(int i=0;i<n;i++) arr[sc.nextInt()]++; int k=sc.nextInt(); int cnt=0; for(int i=0;i<arr.length;i++) { if(arr[i]!=0)cnt++; if(cnt==k) { cnt=i; break; } } System.out.println(cnt); } } }
import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); TreeSet<Integer> set = new TreeSet<>(); int n = scanner.nextInt(); for (int i = 0; i < n; i++) set.add(scanner.nextInt()); ArrayList<Integer> list = new ArrayList<>(set); int k = scanner.nextInt(); System.out.println(list.get(k-1)); } }
import java.util.Arrays; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int n = scanner.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n; i++) { nums[i] = scanner.nextInt(); } int min = scanner.nextInt(); Arrays.sort(nums); for(int i = 1; i < nums.length; i++) { if(nums[i] == nums[i - 1]) continue; min--; if(min == 1) { System.out.println(nums[i]); return; } } } } }
import java.util.*;
运行时间:44ms
占用内存:10932k
思路、注释,在代码里了。
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Allen_Hua
* @create_time 创建时间:May 11, 2018 9:24:25 PM 类说明
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
int k = scan.nextInt();// 第k小的数
// 从小到大排了序
Arrays.sort(arr);
// 遇到一种新的元素就加一 默认arr[0]是一种元素 temp初始化值为1
int temp = 1;
for (int i = 1; i < arr.length; i++) {
// 如果该数和前面一个数一样 则continue
// 不会执行循环体内该句下面的代码
if (arr[i] == arr[i - 1]) {
continue;
} else {// 如果该数和前面一个数不一样 则遇到了新的数
temp++;
}
if (temp == k) {
System.out.println(arr[i]);
}
}
}
}
}
import java.util.Scanner; import java.util.TreeSet; /** * Created by fhqplzj on 17-2-19 at 下午5:08. */ public class Ex1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { TreeSet<Integer> treeSet = new TreeSet<>(); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { treeSet.add(scanner.nextInt()); } int k = scanner.nextInt(); for (int i = 1; i < k; i++) { treeSet.pollFirst(); } System.out.println(treeSet.first()); } } }
import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; /** * Created by fhqplzj on 17-2-19 at 下午5:08. */ public class Ex1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextInt()) { TreeSet<Integer> treeSet = new TreeSet<>(); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { treeSet.add(scanner.nextInt()); } Iterator<Integer> iterator = treeSet.iterator(); int k = scanner.nextInt(); for (int i = 1; i < k; i++) { iterator.next(); } System.out.println(iterator.next()); } } }