算法练习(数组)

1.在这里插入图片描述

//回溯法
 public int maxProduct(int[] nums) {
        max = nums[0];
        backtracking(nums,0);
        return max;
    }

    public void backtracking(int[] nums , int startIndex){
        if (startIndex == nums.length){
            return;
        }
        int tmp = 1;
        for (int i = startIndex; i < nums.length; i++) {
            tmp = tmp * nums[i];
            if (max < tmp){
                max = tmp;
            }
        }
        backtracking(nums,startIndex+1);
    }

官方解法:

        int maxF = nums[0], minF = nums[0], ans = nums[0];
        int length = nums.length;
        for (int i = 1; i < length; ++i) {
            int mx = maxF, mn = minF;
            maxF = Math.max(mx * nums[i], Math.max(nums[i], mn * nums[i]));
            minF = Math.min(mn * nums[i], Math.min(nums[i], mx * nums[i]));
            ans = Math.max(maxF, ans);
        }
        return ans;

2.在这里插入图片描述

public int majorityElement(int[] nums) {
       int count = 1;
       int majority = nums[0];
       for(int i = 1; i < nums.length;i++){
          count = majority == nums[i]?count+1:count - 1;
          if(count == 0){
              majority = nums[i];
              count = 1;
          }
       }
       return majority;
    }

3.在这里插入图片描述

 public void rotate(int[] nums, int k) {
        if (k % nums.length == 0){
            return;
        }
        //第一种
        int n = nums.length;
        int[] newArr = new int[n];
        for (int i = 0; i < n; i++) {
            newArr[(i + k) % n] = nums[i];
        }
        System.arraycopy(newArr,0,nums,0,n);

        //第二种
        int n = nums.length;
        int count = gcd(k,n);
        for (int start = 0; start < count; start++) {
            int current = start;
            int prev = nums[start];
            do {
                int next = (current + k) % n;
                int tmp = nums[next];
                nums[next] = prev;
                current = next;
                prev = tmp;
            }while (start != current);
        }

        //第三种
        int n = nums.length;
        reverse(nums,0,n-1);
        reverse(nums,0,k%n-1);
        reverse(nums,k%n,n-1);
    }

    public int gcd(int x ,int y){
        return y > 0 ? gcd(y,x%y):x;
    }

    public void reverse(int[] nums , int start , int end){
        while (start < end){
            int tmp = nums[start];
            nums[start] = nums[end];
            nums[end] = tmp;
            start++;
            end--;
        }
    }

4.在这里插入图片描述

public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!set.add(nums[i])){
                return true;
            }
        }
        return false;
    }

5.在这里插入图片描述

public void moveZeroes(int[] nums) {
        int count = 0;
        int startIndex = 0;
        int endIndex = 0;
        while (startIndex < nums.length && endIndex < nums.length){
            if (nums[endIndex] == 0){
                endIndex++;
                count++;
            }else {
                if (startIndex != endIndex){
                    nums[startIndex] = nums[endIndex];
                }
                startIndex++;
                endIndex++;
            }
        }
        for (int i = nums.length - 1; i >= nums.length - count; i--) {
            nums[i] = 0;
        }
    }

6.在这里插入图片描述

 class Solution {

        private int[] original;
        private int[] array;
        Random random = new Random();


        public Solution(int[] nums) {
            array = nums;
            original = nums.clone();
        }

        /** Resets the array to its original configuration and return it. */
        public int[] reset() {
            array = original;
            original = original.clone();
            return original;
        }

        /** Returns a random shuffling of the array. */
        public int[] shuffle() {
            for (int i = 0; i < array.length; i++) {
                swap(i,random.nextInt(array.length - i) + i);
            }
            return array;
        }

        private void swap(int i , int j){
            int tmp;
            tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }

7.在这里插入图片描述

public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer , Integer> map1 = new HashMap<>();
        HashMap<Integer , Integer> map2 = new HashMap<>();
        HashMap<Integer , Integer> map3 = new HashMap<>();
        for (int i : nums1) {
            if (map1.containsKey(i)){
                map1.put(i,map1.get(i).intValue()+1);
            }else {
                map1.put(i,1);
            }
        }
        for (int i : nums2) {
            if (map2.containsKey(i)){
                map2.put(i,map2.get(i).intValue()+1);
            }else {
                map2.put(i,1);
            }
        }
        for (Integer integer : map1.keySet()) {
            if (map2.containsKey(integer)){
                if (map1.get(integer).intValue() > map2.get(integer).intValue()){
                    map3.put(integer,map2.get(integer).intValue());
                }else {
                    map3.put(integer,map1.get(integer).intValue());
                }
            }
        }
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer integer : map3.keySet()) {
            for (int i = 0; i < map3.get(integer).intValue(); i++) {
                list.add(integer);
            }
        }
        int[] max = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            max[i] = list.get(i);
        }
        return max;
    }

或者

public int[] intersect(int[] nums1, int[] nums2) {
        if (nums1.length > nums2.length) {
            return intersect(nums2, nums1);
        }
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int num : nums1) {
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        int[] intersection = new int[nums1.length];
        int index = 0;
        for (int num : nums2) {
            int count = map.getOrDefault(num, 0);
            if (count > 0) {
                intersection[index++] = num;
                count--;
                if (count > 0) {
                    map.put(num, count);
                } else {
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }

8.在这里插入图片描述
在这里插入图片描述

思路详解:https://leetcode-cn.com/problems/increasing-triplet-subsequence/solution/pou-xi-ben-zhi-yi-wen-bang-ni-kan-qing-t-3ye2/

 public boolean increasingTriplet(int[] nums) {
        int one = Integer.MAX_VALUE;
        int two = Integer.MAX_VALUE;
        for (int three : nums) {
            if (three > two){
                return true;
            }else if (three <= one){
                one = three;
            }else {
                two = three;
            }
        }
        return false;
    }

9.在这里插入图片描述

public boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length - 1;
        int col = 0;
        while(row >= 0 && col <= matrix[0].length - 1){
            if(matrix[row][col] > target){
                row--;
            }else if(matrix[row][col] < target){
                col++;
            }else{
                return true;
            }
        }
        return false;
    }

10.在这里插入图片描述

public int[] productExceptSelf(int[] nums) {
        int length = nums.length;
        int[] answer = new int[length];
        answer[0] = 1;
        //右边乘积
        for (int i = 1; i < length; i++) {
           answer[i] = answer[i - 1] * nums[i - 1];
        }
        int r = 1;
        //左边乘积
        for (int i = length - 1; i >= 0; i--) {
            answer[i] = answer[i] * r;
            r = r * nums[i];
        }
        return answer;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值