给定大小为N且整数K的数组arr [] ,任务是在将所有等于K的值移到数组末尾后打印该数组。
例子:
Input: arr = [2, 1, 2, 2, 2, 3, 4, 2], K = 2
Output: [4, 1, 3, 2, 2, 2, 2, 2]
Explanation:
2 is the number which has to be moved to the end of the array arr[]. Therefore, after making the change the array is [4, 1, 3, 2, 2, 2, 2, 2]. The numbers 4, 1, and 3 could be ordered differently.
Input: arr = [1, 1, 3, 5, 6], K = 1
Output: [6, 5, 3, 1, 1 ]
Explanation:
1 is the number which has to be moved to the end of the array arr[]. Therefore, after making the change the array is [6, 5, 3, 1, 1 ].
方法:为了解决上述问题,我们使用了两指针技术。
- 初始化两个指针,其中左指针分别标记数组的开始,另一个右指针标记数组的结束。
- 只要右指针指向K ,就递减计数,而当右指针不指向整数m时,递增左指针的计数。
- 当两个指针都没有移动时,将它们的值交换到适当的位置。
- 重复此过程,直到指针相互传递为止。
下面是上述方法的实现:
C++
// C++ program to move all values
// equal to K to the end of the Array
#include
using namespace std;
// Function to move the element to the end
vector moveElementToEnd(
vector array, int toMove)
{
// Mark left pointer
int i = 0;
// Mark the right pointer
int j = array.size() - 1;
// Iterate untill left pointer
// crosses the right pointer
while (i < j) {
while (i < j && array[j] == toMove)
// decrement right pointer
j--;
if (array[i] == toMove)
// swap the two elements
// in the array
swap(array[i], array[j]);
// increment left pointer
i++;
}
// return the result
return array;
}
// Driver code
int main(int argc, char* argv[])
{
vector arr = { 1, 1, 3, 5, 6 };
int K = 1;
vector ans
= moveElementToEnd(arr, K);
for (int i = 0; i < arr.size(); i++)
cout << ans[i] << " ";
return 0;
}
Java
// Java program to move all values
// equal to K to the end of the Array
class GFG{
// Function to move the element to the end
static int[] moveElementToEnd(int []array,
int toMove)
{
// Mark left pointer
int i = 0;
// Mark the right pointer
int j = array.length - 1;
// Iterate untill left pointer
// crosses the right pointer
while (i < j)
{
while (i < j && array[j] == toMove)
// Decrement right pointer
j--;
if (array[i] == toMove)
// Swap the two elements
// in the array
swap(array, i, j);
// Increment left pointer
i++;
}
// Return the result
return array;
}
static int[] swap(int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int []arr = { 1, 1, 3, 5, 6 };
int K = 1;
int []ans = moveElementToEnd(arr, K);
for(int i = 0; i < arr.length; i++)
System.out.print(ans[i] + " ");
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program to move all values
# equal to K to the end of the Array
# Function to move the element to the end
def moveElementToEnd(array, toMove):
# Mark left pointer
i = 0
# Mark the right pointer
j = len(array) - 1
# Iterate untill left pointer
# crosses the right pointer
while (i < j):
while (i < j and array[j] == toMove):
# decrement right pointer
j-=1
if (array[i] == toMove):
# swap the two elements
# in the array
array[i], array[j] = array[j] , array[i]
# increment left pointer
i += 1
# return the result
return array
# Driver code
if __name__ =="__main__":
arr = [ 1, 1, 3, 5, 6 ]
K = 1
ans = moveElementToEnd(arr, K)
for i in range(len(arr)):
print(ans[i] ,end= " ")
# This code is contributed by chitranayal
C#
// C# program to move all values
// equal to K to the end of the Array
using System;
class GFG{
// Function to move the element to the end
static int[] moveElementToEnd(int []array,
int toMove)
{
// Mark left pointer
int i = 0;
// Mark the right pointer
int j = array.Length - 1;
// Iterate untill left pointer
// crosses the right pointer
while (i < j)
{
while (i < j && array[j] == toMove)
// Decrement right pointer
j--;
if (array[i] == toMove)
// Swap the two elements
// in the array
swap(array, i, j);
// Increment left pointer
i++;
}
// Return the result
return array;
}
static int[] swap(int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main(string[] args)
{
int []arr = { 1, 1, 3, 5, 6 };
int K = 1;
int []ans = moveElementToEnd(arr, K);
for(int i = 0; i < arr.Length; i++)
Console.Write(ans[i] + " ");
}
}
// This code is contributed by rock_cool
Javascript
输出:
6 5 3 1 1
时间复杂度: O(N) ,其中N是数组的长度。
空间复杂度: O(1)