数组常见面试题

数组常见面试题

数组中重复的数字

https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/


空间复杂度O(n)
class Solution {
   
    public int findRepeatNumber(int[] nums) {
   
        if(nums == null || nums.length == 0) {
   
            return -1;
        }
       HashSet<Integer> set = new HashSet<>();
        for(int num : nums){
   
            if(set.contains(num)) {
   
                return num;
            }
            set.add(num);
        }
        return -1;
    }
}

空间复杂度O1class Solution {
   
    public int findRepeatNumber(int[] nums) {
   
        if(nums == null || nums.length == 0) {
   
            return -1;
        }
        for(int i = 0; i < nums.length; i++) {
   
            for(int j = i + 1; j < nums.length; j++ ) {
   
                if(nums[i] == nums[j]) {
   
                    return nums[i];
                }
            }
        }
        return -1;
    }
}

寻找重复数

https://leetcode-cn.com/problems/find-the-duplicate-number/

二分
class Solution {
   
    public int findDuplicate(int[] nums) {
   
        if(nums == null || nums.length == 0) return -1;
        int n = nums.length - 1;
        int l = 0, r = n;
        while (l < r) {
   
            int mid = l + r >> 1;      
            int cnt = 0;
            for (int num : nums) {
   
                if (num <= mid) {
   
                    cnt += 1;
                }
            }
            if (cnt > mid) {
   
            
                r = mid;
            } else {
   
                l = mid + 1;
            }
        }
        return l;
    }
}


快慢指针
class Solution {
   
    public int findDuplicate(int[] nums) {
   
        int slow = 0;
        int fast = 0;
        slow = nums[slow];
        fast = nums[nums[fast]];
        while(slow != fast){
   
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        int pre1 = 0;
        int pre2 = slow;
        while(pre1 != pre2){
   
            pre1 = nums[pre1];
            pre2 = nums[pre2];
        }
        return pre1;
    }
}

删除有序数组中的重复项I(元素只出现一次)

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

class Solution {
   
    public int removeDuplicates(int[] nums) {
   
        if(nums == null || nums.length == 0) {
   
            return -1;
        }
        int i = 0;
        int j = 1;
        while(j < nums.length) {
   
            if(nums[j] != nums[i]) {
   
                nums[i + 1] = nums[j];
                i++;
            }
            j++;
        }
        return i + 1;
    }
}

删除有序数组中的重复项 II(元素最多出现两次)

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/

class Solution {
   
    public int removeDuplicates(int[] nums) {
   
        if(nums.length <= 2) return nums.length;
        
        int index = 2;
        for(int i = 2; i < nums.length; i++){
   
            if(nums[i] != nums[index-2])
                nums[index++] = nums[i];
        }
        
        return index;
    }
}

连续子数组的最大和

https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/

class Solution {
   
    public int maxSubArray(int[] nums) {
   
        if(nums == null || nums.length == 0) {
   
            return -1;
        }
        int ret = nums[0];
        for(int i = 1; i < nums.length; i++) {
   
            nums[i] += Math.max(nums[i - 1], 0);
            ret = Math.max(ret, nums[i]);
        }
        return ret;
    }
}

乘积最大子数组(最大连续数子数组的积)

https://leetcode-cn.com/problems/maximum-product-subarray/

class Solution {
   
     public int maxProduct(int[] nums) {
   
        int ret= Integer.MIN_VALUE, imax = 1, imin = 1; //一个保存最大的,一个保存最小的。
        for(int i = 0; i < nums.length; i++){
   
            if(nums[i] < 0){
    
                int tmp = imax; 
                imax = imin; 
                imin = tmp;
                } 
                //如果数组的数是负数,那么会导致最大的变最小的,最小的变最大的。因此交换两个的值。
            imax = Math.max(imax*nums[i], nums[i]);
            imin = Math.min(imin*nums[i], nums[i]);
            
            ret= Math.max(ret, imax);
        }
        return ret;
    }
}

构建乘积数组

https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/

class Solution {
   
    public int[] constructArr(int[]nums) {
   
        if(nums == null || nums.length == 0) {
   
            return new int[0];
        }
        int[] ret = new int[nums.length];
        for(int i = 0, j = 1; i < nums.length; i++) {
   
            // 先乘左边的数(不包括自己)
            ret[i] = j;
            j *= nums[i];
        }
        for(int i = nums.length - 1, j = 1; i >= 0; i--) {
   
            // 再乘右边的数(不包括自己)
            ret[i] *= j;
            j *= nums[i];
        }
        return ret;
    }
}

把数组排成最小的数

https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
在这里插入图片描述

class Solution {
   
    public String minNumber(int[] nums) {
   
         String[] strs = new String[nums.length];
        for(int i = 0; i < nums.length; i++) {
   
            strs[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(strs, (x, y)-> (x + y).compareTo(y + x));
        StringBuffer ret = new StringBuffer();
        for(String s : strs) {
   
            ret.append(s);
        }
        return ret.toString();
    }
}

把数组排成最大的数(最大数)

https://www.nowcoder.com/practice/fc897457408f4bbe9d3f87588f497729?tpId=117&&tqId=37835&&companyId=665&rp=1&ru=/company/home/code/665&qru=/ta/job-code-high/question-ranking

import java.util.*;
public class Solution {
   
    public String solve (int[] nums) {
   
        String[] strs = new String[nums.length];
        for(int i = 0; i < nums.length; i++){
   
            strs[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(strs, (x, y)-> (y + x).compareTo(x + y));
//         Arrays.sort(strs, new Comparator<String>(){
   
//             public int compare(String a, String b){
   
//                 return (b + a).compareTo(a + b);
//             }
//         });
        if(strs[0].equals("0")) {
   
            return "0";
        }
        StringBuilder ret = new StringBuilder();
        for(int i = 0; i < nums.length; i++){
   
            ret.append(strs[i]);
        }
        return ret.toString();
    }
}

最小的k个数

https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/

class Solution {
   
    public int[] getLeastNumbers(int[] arr, int k) {
   
        if(arr == null || arr.length == 0 || k == 0) {
   
            return new int[0];
        }
        PriorityQueue<Integer> bigHeap = new PriorityQueue<>((o1, o2)->(o2 - o1));
        for(int num : arr) {
   
            if(bigHeap.size() < k) {
   
                bigHeap.offer(num);
            }else if(bigHeap.peek() > num) {
   
                bigHeap.poll();
                bigHeap.offer(num);
            }
        }
        int[] ret = new int[k];
        int index = 0;
        while(!bigHeap.isEmpty()) {
   
            ret[index++] = bigHeap.poll();
        }
        return ret;
    }
}

class Solution {
   
    public int[] getLeastNumbers(int[] arr, int k) {
   
            if (k >= arr.length) return arr;
        return quickSort(arr, k, 0, arr.length - 1);
    }
    private int[] quickSort(int[] arr, int k, int l, int r) {
   
        int i = l, j = r;
        while (i < j) {
   
            while (i < j && arr[j] >= arr[l]) j--;
            while (i < j && arr[i] <= arr[l]) i++;
            swap(arr, i, j);
        }
        swap(arr, i, l);
        if (i > k) return quickSort(arr, k, l, i - 1);
        if (i < k) return quickSort(arr, k, i + 1, r);
        return Arrays.copyOf(arr, k);
    }
    private void swap(int[] arr, int i, int j) {
   
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}

数组中第K大的数字

https://leetcode-cn.com/problems/xx4gT2/

class Solution {
   
    public int findKthLargest(int[] nums, int k) {
   
        PriorityQueue<
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值