Array Mock

27,26,283,844,189,  41

27. Remove Element

Easy

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • Return k.
class Solution {
    public int removeElement(int[] nums, int val) {
        int slow = 0;
        int fast = 0;
        for(fast = 0; fast < nums.length; fast++){
            if(nums[fast] != val){
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}

26. Remove Duplicates from Sorted Array

Easy

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
  • Return k.
class Solution {
    public int removeDuplicates(int[] nums) {
        int slow = 1;
        for(int fast = 1; fast < nums.length; fast++){
            if(nums[fast] != nums[fast-1]){
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}

283. Move Zeroes

Easy

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

class Solution {
    public void moveZeroes(int[] nums) {
        int slow = 0;
        int fast = 0;
        for(fast = 0; fast < nums.length; fast++){
            if(nums[fast] != 0){
              nums[slow] = nums[fast];
              slow++;
            }
        }
        for(int i = slow ; i < nums.length; i++){
            nums[i] = 0;
        }
    }
}
class Solution {
    public void moveZeroes(int[] nums) {
        int slow = 0;
        int fast = 0;
        for(fast = 0; fast < nums.length; fast++){
            if(nums[fast] != 0){
                int temp = nums[slow];
                nums[slow] = nums[fast];
                nums[fast] = temp;
                slow++;
            }
        }
    }
}

844. Backspace String Compare

Easy

Given two strings s and t, return true if they are equal when both are typed into empty text editors'#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Example 1:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
class Solution {
    public boolean backspaceCompare(String s, String t) {
        return backSpace(s).equals(backSpace(t));
    }
    public String backSpace(String s){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch != '#'){
                sb.append(ch);
            }else{
                if(!sb.isEmpty()){
                    sb.deleteCharAt(sb.length() - 1);
                }
            }
        }
        return sb.toString();
    }
}

189. Rotate Array

Medium

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

class Solution {
    public void rotate(int[] nums, int k) {
        int[] tempNums = new int[nums.length];
        for(int i = 0; i < nums.length; i++){
            tempNums[(i + k) % nums.length]= nums[i];
        }
        for(int i = 0; i < nums.length; i++){
            nums[i] = tempNums[i];
        }
    }
}
class Solution {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        reverse(nums, 0, n-1);
        reverse(nums, 0, k - 1);
        reverse(nums, k , n - 1);
    }
    private void reverse(int[] nums, int start, int end){
        while(start <= end){
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }
}

238. Product of Array Except Self

Medium

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

Example 1:

Input: nums = [1,2,3,4]
Output: [24,12,8,6]
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] ans = new int[nums.length];
        int[] leftProduct = new int[nums.length];
        int[] rightProduct = new int[nums.length];
        leftProduct[0] = 1;
        for(int i = 1; i < nums.length; i++){
            leftProduct[i] = nums[i-1] * leftProduct[i-1];
        }
        rightProduct[nums.length - 1] = 1;
        for(int i = nums.length - 2; i >= 0; i--){
            rightProduct[i] = nums[i+1] * rightProduct[i+1];
        }
        for(int i = 0;i < nums.length; i++){
            ans[i] = leftProduct[i] * rightProduct[i];
        }
        return ans;
    }
}

41. First Missing Positive

Solved

Hard

Topics

Companies

Hint

Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

Example 1:

Input: nums = [1,2,0]
Output: 3
Explanation: The numbers in the range [1,2] are all in the array.
public class Solution {

    public int firstMissingPositive(int[] nums) {
        int len = nums.length; // 获取数组的长度

        // 第一遍遍历:尝试将每个正数放到其应有的位置上(即数值 1 应该放在索引 0 的位置上)
        for (int i = 0; i < len; i++) {
            while (nums[i] > 0 && nums[i] <= len && nums[nums[i] - 1] != nums[i]) {
                // 只有当当前元素是正数、不超过数组长度,并且它不在正确的位置上时,才进行交换
                // nums[i] - 1 是当前元素应该在的位置
                swap(nums, nums[i] - 1, i);
            }
        }

        // 第二遍遍历:检查每个位置是否放置了正确的数
        // [1, -1, 3, 4]
        for (int i = 0; i < len; i++) {
            // 如果当前位置上的数不是 i + 1,那么 i + 1 就是缺失的第一个正数
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }

        // 如果数组中的所有正数都正确地放在了它们的位置上,则缺失的第一个正数是数组长度+1
        return len + 1;
    }

    // 辅助函数:交换数组中的两个元素
    private void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }
}

  • 12
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值