Leetcode Learning

Leetcode Learning Note - Day1-Array

Move Zeros

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

Example

// Example 
Input: [0,1,0,3,12]
Output: [1,3,12,0,0] 

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

Solution
The simplest way is obvious. Copy the non-zero elements to another array and fill the rest of the alternative array with zeros.
But according the above, be aware that we cannot create another array. As a solution, we can simply replace the original array with non-zero elements first( in order), and then fill the rest with zeros. We can achieve this by maintaining a index k during the loops.

public static void MoveZeros(int [] nums)
	{   int k=0;  // the moving index
		for(int i=0;i<nums.length;i++)
		{
			if(nums[i]!=0) 
			{
				nums[k]=nums[i];
				k++;
			}
		} 
		for(int j=k; j<nums.length;j++)
		{
			nums[j]=0;
		} 
	} 

Remove Elements

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example1

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2]
Explanation: Your function should return length = 2, 
with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
For example if you return 2 with nums = [2,2,3,3] or nums = [2,2,0,0],
your answer will be accepted.

Example2

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3]
Explanation: Your function should return length = 5, with the first 
five elements of nums containing 0, 1, 3, 0, and 4. Note that the order 
of those five elements can be arbitrary. It doesn't matter 
what values are set beyond the returned length. 

Constraints

  1. 0 <= nums.length <= 100
  2. 0 <= nums[i] <= 50
  3. 0 <= val <= 100

Solution
Similar to the previous problem (Move Zeros), this problem simply requires you to search for a particular value val and return nums.length-number of val in nums. A little modification of the previous code will easily solve this.

public static void MoveZeros(int [] nums, int val)
	{   int k=0;  // the moving index
		for(int i=0;i<nums.length;i++)
		{
			if(nums[i]!=val)    // note that here, we replace 0 with the intended value val
			{
				nums[k]=nums[i];
				k++;
			}
		} 
		for(int j=k; j<nums.length;j++)
		{
			nums[j]=0;
		}  
		return k; // k being the number of elements in nums that does not equal val.
	} 

Remove Duplicates

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example1

Input: nums = [1,1,2]
Output: 2, nums = [1,2]
Explanation: Your function should return length = 2, with 
the first two elements of nums being 1 and 2 respectively. 
It doesn't matter what you leave beyond the returned length.

Example2

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.

Constraints

  1. 0 <= nums.length <= 3 * 10^4
  2. -10^4 <= nums[i] <= 10^4
  3. nums is sorted in ascending order.

Solution
First, note that the given array is already sorted in ascending order. We can solve this problem in the following steps:

  1. set index k =0
  2. for loops to search for the next nums[i] > nums[k]
  3. set k=k+1 and go back to step 2
  4. return k+1(we return k+1 because the above solution does not count the initial value nums[0] , we should add 1 so the final answer could return the right array length).
public int removeDuplicates(int[] nums) { 
		// special case. when the given array is empty.
		if(nums.length==0)
			return 0;
        int k=0; 
		for (int i =0; i<nums.length;i++) 
		{
			if(nums[i]>nums[k])
			{
				k++; 
				nums[k]=nums[i];
			}
		} 
		return k+1;
    }

Remove Duplicates (upgrade!)

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array; you must do this by modifying the input array in-place with O(1) extra memory.

Example1

Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3]
Explanation: Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned length.

Example2

Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3]
Explanation: Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -10^4 <= nums[i] <= 10 ^4
  • nums is sorted in ascending order.

Solution
Be aware that the array is already sorted. We can solve this by identifying the conditions of adjacent element

  • variable n—> the length of array nums .
    variable k—> use it keep track of the new array. initial value—>0
    variable j—> the number of duplicate element. initial value—>1
    variable i—> For loops. initial value—>1

  • First condition: when nums[i]>nums[i-1], we increment k by 1 and set nums[k]=nums[i], make sure to set j to 1 because this condition marks the end of counting the duplicates of previous element.

  • Second condition: when nums[i]==nums[i-1] && j<2, similarly we increment k by 1 and set nums[k]=nums[i], here we also increment j by 1 to track the number of duplicates of current element.
    Third condition: when the same element appears more than twice, for example nums{1,1,1,2,2,3}, when i equals 2, nums[2] =nums[1] && j=2. Do nothing.

public static int removeDuplicates(int[] nums) { 
		int n = nums.length;  //length of the array 
		int k=0;   //index  
		int j=1;   // element count
		for (int i =1; i<nums.length;i++) 
		{   
			if(nums[i]>nums[i-1])
			{
				k++; 
				nums[k]=nums[i];
				j=1;
			}
			else if( nums[i]==nums[i-1] && j<2)
			{
				k++; 
				nums[k]=nums[i];
				j++;
			}		
		} 
		return k+1; 
    } 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值