执行给定操作后查找结果输出数组
给定一个大小为N的整数数组integers[] 。执行一些操作后找到结果输出数组:
- 如果第(i-1)个元素为正且第 i个元素为负,则:
- 如果(i) th的绝对值大于(it)th ,则从左侧删除所有绝对值小于ith的连续正元素。
- 如果(i) th的绝对值小于(it)th ,则删除该元素。
- 如果(i) th的绝对值等于(i-1) th ,则删除这两个元素。
例子:
Input: integers[] = {3, -2, 4}
Output: 3 4
Explanation: The first number will cancel out the second number.
Hence, the output array will be {3,4}.
Input: integers[] = {-2, -1, 1, 2}
Output: -2 -1 1 2
Explanation: After a positive number, negative number is not added.
So every number would be present in the output array
方法:忽略相同符号的数字,不管它们的大小。所以我们唯一需要考虑的情况是,当前一个元素为正值而下一个元素为负值时,我们可以使用堆栈来模拟问题。请按照以下步骤解决问题:
- 初始化堆栈s[]。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 如果integers[i]大于0或s[]为空,则将integers[i]压入堆栈s[] 。
- 否则,在while循环中遍历直到s不为空且s.top()大于0且s.top()小于abs(integers[i])并执行以下任务:
- 从堆栈s[] 中弹出元素。
- 如果s不为空并且s.top()等于abs(integers[i])则从堆栈s[] 中弹出。
- 否则,如果s[]为空或s.top()小于0 ,则将整数 [i]压入堆栈s[]。
- 初始化向量res[s.size()]以存储结果。
- 使用变量i遍历范围[s.size()-1, 0]并执行以下任务:
- 将res[i]设置为s.top()并从堆栈s[] 中弹出。
- 执行上述步骤后,打印res[]的值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the remaining numbers
vector remainingNumbers(vector&
integers)
{
// Size of the array
int n = integers.size();
// Initialize the stack
stack s;
// Traverse the array
for (int i = 0; i < n; i++) {
if (integers[i] > 0 || s.empty()) {
s.push(integers[i]);
}
else {
while (!s.empty() and s.top() > 0
and s.top() <
abs(integers[i])) {
s.pop();
}
if (!s.empty()
and s.top() ==
abs(integers[i])) {
s.pop();
}
else if (s.empty() ||
s.top() < 0) {
s.push(integers[i]);
}
}
}
// Finally we are returning the elements
// which remains in the stack.
// we have to return them in reverse order.
vector res(s.size());
for (int i = (int)s.size() - 1; i >= 0;
i--) {
res[i] = s.top();
s.pop();
}
return res;
}
// Driver Code
int main()
{
vector integers = { 3, -2, 4 };
vector ans =
remainingNumbers(integers);
for (int x : ans) {
cout << x << " ";
}
return 0;
}
Python3
# python3 program for the above approach
# Function to find the remaining numbers
def remainingNumbers(integers):
# Size of the array
n = len(integers)
# Initialize the stack
s = []
# Traverse the array
for i in range(0, n):
if (integers[i] > 0 or len(s) == 0):
s.append(integers[i])
else:
while (len(s) != 0 and s[len(s) - 1] > 0
and s[len(s) - 1] < abs(integers[i])):
s.pop()
if (len(s) != 0
and s[len(s) - 1] ==
abs(integers[i])):
s.pop()
elif (len(s) == 0 or
s[len(s) - 1] < 0):
s.append(integers[i])
# Finally we are returning the elements
# which remains in the stack.
# we have to return them in reverse order.
res = [0 for _ in range(len(s))]
for i in range(len(s) - 1, -1, -1):
res[i] = s[len(s) - 1]
s.pop()
return res
# Driver Code
if __name__ == "__main__":
integers = [3, -2, 4]
ans = remainingNumbers(integers)
for x in ans:
print(x, end=" ")
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the remaining numbers
static List remainingNumbers(List integers)
{
// Size of the array
int n = integers.Count;
// Initialize the stack
Stack s = new Stack();
// Traverse the array
for (int i = 0; i < n; i++) {
if (integers[i] > 0 || s.Count == 0) {
s.Push(integers[i]);
}
else {
while (s.Count != 0 && s.Peek() > 0
&& s.Peek()
< Math.Abs(integers[i])) {
s.Pop();
}
if (s.Count != 0
&& s.Peek() == Math.Abs(integers[i])) {
s.Pop();
}
else if (s.Count == 0 || s.Peek() < 0) {
s.Push(integers[i]);
}
}
}
// Finally we are returning the elements
// which remains in the stack.
// we have to return them in reverse order.
List res = new List(new int [s.Count]);
for (int i = (int)s.Count - 1; i >= 0; i--) {
res[i] = s.Peek();
s.Pop();
}
return res;
}
// Driver Code
public static void Main()
{
List integers = new List() { 3, -2, 4 };
List ans = remainingNumbers(integers);
foreach(int x in ans) { Console.Write(x + " "); }
}
}
// This code is contributed by ukasp.
Javascript
输出
3 4
时间复杂度: O(N)
辅助空间: O(N)