招行题解
p1:应该有更快的~:
import java.util.Arrays; import java.util.Scanner; public class wain { public static void main(String args[]){ Scanner in =new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); int arr[]=new int[n]; for (int i=0;i<n;i++){ arr[i]=in.nextInt(); } Arrays.sort(arr); for (int i=0;i<n;i++){ if(arr[i]!=i){ System.out.println(i); break; } } } } } p2:BFS+记录hashset:import java.util.*; import java.util.Arrays; public class xain { public static void main(String args[]){ Scanner in =new Scanner(System.in); while (in.hasNext()) { int x = in.nextInt(); System.out.println(find(x)); } } public static int find(int x){ Queue<Integer> queue =new LinkedList<>(); HashSet<Integer> set=new HashSet<>(); set.add(0); queue.offer(0); int step=0; while (!queue.isEmpty()){ int size=queue.size(); for (int i=0;i<size;i++){ int now=queue.poll(); if(now==x){ return step; } if(!set.contains(now-1)) { queue.offer(now-1); set.add(now-1); } if(!set.contains(now+1)) { queue.offer(now + 1); set.add(now+1); } if(!set.contains(now*2)) { queue.offer(2 * now); set.add(now*2); } } step++; } return 0; } } p3:丑数动态规划import java.util.*; public class Main { public static void main(String args[]){ Scanner in =new Scanner(System.in); while (in.hasNext()) { int n = in.nextInt(); System.out.println(find(n)); } } public static int find(int x){ int [] zhiyinshu= new int[3]; int [] dp=new int[x]; dp[0]=1; int a=2,b=3,c=5; for (int i=1;i<x;i++){ int min=Math.min(a,Math.min(b,c)); dp[i]=min; if(a==min){ a=2*dp[++zhiyinshu[0]]; } if(b==min){ b=3*dp[++zhiyinshu[1]]; } if(c==min){ c=5*dp[++zhiyinshu[2]]; } } return dp[x-1]; } }