首页 > 试题广场 >

【编程题】知某次聚会中共有N个人参加,这N个人来自26个地区

[问答题]

【编程题】知某次聚会敏感词有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);
        }
    }
}
发表于 2020-07-21 20:00:56 回复(0)
public boolean check(int[] Locations){
    Arrays.sort(Locations);
    for(int i=0;i<Locations.length-1;i++){
        if(Locations[i]==Locations[i+1]) return false;
    }
     return true;
}

发表于 2018-07-24 23:41:52 回复(1)
    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;
    }

发表于 2022-08-30 16:51:56 回复(0)
def f(arr):
    arr.sort()
    for i in range(0, len(arr) - 1):
        if arr[i] == arr[i + 1]:
            return False
    return True


if __name__ == '__main__':
    a1 = [2, 3, 6, 1, 7, 8, 4]
    print(f(a1))
    

发表于 2021-08-18 09:51:27 回复(0)
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;
}
N 可能 小于 26   但是给的数组  Locations 数组大小是固定的,O(1) 的 空间复杂度咋弄?
要不就循环两次?
发表于 2021-04-12 17:22:18 回复(0)
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());
    }
}

发表于 2020-09-02 21:50:01 回复(0)
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));     }
}

发表于 2019-03-13 08:19:59 回复(0)
boolisFromDifferentAreas(intLocations[],intN) {
    intarea[26]={0};
    for(inti=0;i<N;i++)
         area[Locations[i]]++;
    for(inti=0;i<26;i++)
     {
        if(area[i]>1) returnfalse;
        }
        returntrue;
}

发表于 2018-08-28 22:58:10 回复(1)
这个题目不难
发表于 2018-08-04 00:30:55 回复(0)
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;
}

发表于 2018-08-03 10:07:54 回复(0)
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;
    }
}

发表于 2018-07-20 15:00:34 回复(2)
想了好多的方法,实在没想到是这个。。。
发表于 2018-07-19 21:30:32 回复(1)