【编程题】知某次聚会敏感词有N个人参加,这N个人来自26个地区,现在将26个地区使用数字0-25表示,使用整数数组Locations存储这N个人的地区, 请返回一个bool值, True代表所有人的地区全都不同,False代表存在相同地区。要求:不能使用额外的存储结构。
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Meeting { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[] Locations = new int[26]; for(int i = 0; i < 26; i ++){ Locations[i] = scan.nextInt(); } Set<Integer> set = new HashSet<>(); for(int n: Locations){ set.add(n); } if(set.size() == Locations.length){ System.out.println(true); }else { System.out.println(false); } } }
public static boolean findRepeatNumber(int[] Locations) { for (int i = 0; i < Locations.length; i++) { while(Locations[i]!=i && Locations[Locations[i]]!=Locations[i]){ swap(Locations,i,Locations[i]); } if(Locations[i]!=i && Locations[Locations[i]]==Locations[i]){ return false; } } return true; } public static void swap(int[] arr,int i,int j){ int t=arr[i]; arr[i]=arr[j]; arr[j]=t; }
public static boolean isSameArea(int[] Locations, int N) { if(N > 26) { return false; } for (int i = 0; i < N; i ++) { arr[i] = Locations[i]; } for (int i = N; i < 26; i ++) { arr[i] = -1; } int tmp; for(int i = 0; i < 26; i ++) { while(i != arr[i] && arr[i] != -1) { if (arr[i] == -1) { break; } if(arr[i] == arr[arr[i]]) { return false; } tmp = arr[i]; if (arr[tmp] == -1) { arr[i] = -1; } else { arr[i] = arr[tmp]; } arr[tmp] = tmp; } } return true; }
import java.util.Arrays; public class DiffJuge { public static boolean diff(){ int[] Location = new int[26]; boolean flag = true; for(int i = 0;i<Location.length;i++){ Location[i] = (int) ((Math.random())*26); } System.out.println(Arrays.toString(Location)); Arrays.sort(Location); for(int i = 0;i<Location.length-1;i++){ if(Location[i]==Location[i+1]) flag = false; return false; } return flag; } public static void main(String[] args) { System.out.println(diff()); } }
import java.util.Arrays; import java.util.Scanner; public class Locations { public static void main(String[] args) { Scanner in=new Scanner(System.in); int[] loca=new int [26]; for(int i=0;i<26;i++){ int lo=in.nextInt(); loca[lo]++; } Arrays.sort(loca); System.out.println(!(loca[25]>1)); } }
public boolean test(int[] address) { int[] array = new int[26]; for (int i : address) { array[i] = array[i] + 1; } for (int i : array) { if (i == 0) { return false; } } return true; }
publicclassSolution {publicbool comeFromEqualArea(int[] Location) {if(Location == null|| Location.length == 0 || Location.length > 26)return false;int[] map = new int[26];for(int i = 0, len = Location.length; i < len; ++i) {map[Location[i]]++;if(map[Location[i]] > 1)return false;}return true;}}