头像

半岛铁盒_1




离线:4天前


最近来访(2)
用户头像
UncleLeeML
用户头像
yxc的小迷妹


java 代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        int[][] arr = new int[K][];
        String[] s = new String[K];


        for(int i = 0; i < K; i ++){
            arr[i] = new int[sc.nextInt()];
            int count = 0;
            for(int j = 0; j < arr[i].length; j++){
                arr[i][j] = sc.nextInt();
                if(!isplace(arr,i,j)){
                    s[i] = "NO";
                    count++;
                }

                if(j == arr[i].length - 1 && count == 0){
                    s[i] = "YES";
                }
            }
        }



        for(int i = 0; i < K; i++){
            System.out.println(s[i]);
        }
    }

    public static boolean isplace(int[][] arr, int i, int j){
        for(int m = 0; m < j; m++){
            if(arr[i][m] == arr[i][j] || Math.abs(m-j) == Math.abs(arr[i][m]-arr[i][j])){
                return false;
            }
        }
        return true;
    }

}



java 代码

class Solution {
    public static int NumberOf1(int n) {
        int count = 0;
        Integer.toString(n,2);
        for(int i = 0; i < 32; i++){
            if((n&(1<<i)) == 1<<i){
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        System.out.println(NumberOf1(n));
    }
}



题目描述

找出数组中重复的数字

java 代码

class Solution {
    public static int duplicateInArray(int[] nums) {
        int N = nums.length;
        int[] sums = new int[N];

        for(int j = 0; j < N; j++){
            if (nums[j] < 0 || nums[j] >= N) {
                System.out.println(-1);
                return -1;
            }
        }

        for (int i = 0; i < N; i++) {
            if (++sums[nums[i]] == 2) {
                return nums[i];
            }

        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {2, 3, 5, 4, 3, 2, 6, 7};

        System.out.println(duplicateInArray(nums));
    }
}