add: 230413
This commit is contained in:
parent
d77ba88558
commit
2deff47e80
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include <bitset>
|
||||||
|
#include <vector>
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 946. Validate Stack Sequences
|
||||||
|
*
|
||||||
|
* Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static bool validateStackSequences(const std::vector<int>&, const std::vector<int>&);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool Solution::validateStackSequences(const std::vector<int>& i, const std::vector<int>& o) {
|
||||||
|
std::bitset<1024> pushed;
|
||||||
|
std::stack<int> s;
|
||||||
|
const int n = i.size();
|
||||||
|
for (int pi = 0, po = 0; po < n; ++po, s.pop()) {
|
||||||
|
for (; !pushed[o[po]]; pushed[i[pi++]] = true)
|
||||||
|
s.push(i[pi]);
|
||||||
|
if (s.empty() || s.top() != o[po])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return s.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Solution::validateStackSequences({1,2,3,4,5},{4,3,5,1,2});
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -4,4 +4,4 @@ PROJECT(2304)
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2304 230413-CN.cpp)
|
ADD_EXECUTABLE(2304 230413.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue