牛客网算法练习题目——(查找问题)

给定一个整型数组arr,请打印其中出现次数大于一半的数,如果没有这样的数,请输出-1。

/**
 * Copyright (C), 2018-2020
 * FileName: Main
 * Author:   xjl
 * Date:     2020/7/9 8:36
 * Description: 查找问题集合
 */
package Search;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //输入的数字
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        sc.nextLine();
        //点翻译数组
        int[] array = new int[m];
        for (int i = 0; i < m; i++) {
            array[i] = sc.nextInt();
        }

        //调用函数
        int result = test2(array);
        //输出结果
        System.out.println(result);

    }

    //只能通过95%的
    private static int test(int[] array) {
        int length = array.length / 2;
        for (int i = 0; i < array.length; i++) {
            int count = 1;
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] == array[j]) {
                    count++;
                }
            }
            if (count > length) {
                return array[i];
            }
        }
        return -1;
    }
    //100%的用例的通过测试
    private static int test2(int[] array) {
        int length = array.length / 2;
        Arrays.sort(array);
        int count = 1;
        for (int i = 1; i < array.length; i++) {
            if (array[i] == array[i - 1]) {
                count++;
                if (count>length){
                    return array[i];
                }
            } else {
                count = 1;
            }
        }
        return -1;
    }
}

题目描述

给定一个N×MN \times MN×M的整形矩阵matrix和一个整数K, matrix的每一行和每一列都是排好序的。

实现一个函数,判断K是否在matrix中

[要求]:时间复杂度为O(N+M)O(N+M)O(N+M),额外空间复杂度为O(1)O(1)O(1)。

import java.util.*;

public class Main {
    public static void main(String[] args) {
       sc3();
    }
    public static void sc3() {
        //输入的数字
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int K = sc.nextInt();
        sc.nextLine();
        //简历一个二维数组
        int[][] martix = new int[N][M];

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                martix[i][j] = sc.nextInt();
            }
            sc.nextLine();
        }
        //调用函数
        String result = test3(martix,K);
        //输出结果
        System.out.println(result);
    }
    private static String test3(int[][] martix, int k) {
        for (int i = 0; i < martix.length; i++) {
            for (int j = 0; j < martix[0].length; j++) {
                if (martix[i][j] == k) {
                    return "Yes";
                }
            }
        }
        return "No";
    }
}

题目描述

给定一个数组arr,返回不包含本位置值的累乘数组

例如,arr=[2,3,1,4],返回[12, 8, 24, 6],即除自己外,其他位置上的累乘

[要求]:时间复杂度为O(n)O(n)O(n),额外空间复杂度为O(1)O(1)O(1)

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
  
public class Main {
      
    private static int modNum;
      
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] numStrArr = in.readLine().split(" ");
        String[] seqStrArr = in.readLine().split(" ");
        int N = Integer.valueOf(numStrArr[0]);
        modNum = Integer.valueOf(numStrArr[1]);
        int[] arr = new int[N];
        for (int i = 0; i < N; i++) {
            arr[i] = Integer.valueOf(seqStrArr[i]);
        }
        long[] resArr = productArray2(arr);
        StringBuilder res = new StringBuilder();
        for (long item : resArr) {
            res.append(item).append(' ');
        }
        System.out.println(res.substring(0, res.length() - 1));
    }
      
 
      
    // 利用数组res作为辅助数组,计算过程中调整为结果数组
    public static long[] productArray2(int[] arr) {
        long[] res = new long[arr.length];
        res[0] = arr[0];
        for (int i = 1; i < arr.length; i++) {    // 从左向右计算累乘
            res[i] = res[i - 1] * arr[i] % modNum;
        }
        long tmp = 1;    // 记录右侧的累乘
        for (int i = arr.length - 1; i > 0; i--) {
            res[i] = res[i - 1] * tmp % modNum;
            tmp = tmp * arr[i] % modNum;
        }
        res[0] = tmp;
        return res;
    }
}

定义局部最小的概念。arr长度为1时,arr[0]是局部最小。arr的长度为N(N>1)时,如果arr[0]

给定无序数组arr,已知arr中任意两个相邻的数不相等。写一个函数,只需返回arr中任意一个局部最小出现的位置即可

[要求]

时间复杂度为O(log⁡n)O(\log n)O(logn),空间复杂度为O(1)O(1)O(1)

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        //输入
        int m = sc.nextInt();
        int[] array = new int[m];

        sc.nextLine();
        for (int i = 0; i < m; i++) {
            array[i] = sc.nextInt();
        }
        //函数调用
        int test = test(array);
        //结果的输出
        System.out.println(test);
    }

    private static int test(int[] array) {
        if (array.length == 1) {
            return 0;
        }

        for (int i = 0; i < array.length; i++) {
            //位置为0
            if (i == 0) {
                if (array[i] < array[i + 1]) {
                    return i;
                }
                continue;
            }
            //位置在最后的时候
            if (i == array.length - 1) {
                if (array[i] > array[i - 1]) {
                    return i-1;
                }
                continue;
            }
            
            if (array[i] < array[i - 1] && array[i] < array[i + 1]) {
                return i;
            }
        }
        return 0;
    }
}

 

你可能感兴趣的:(数据结构与算法)