当前位置:网站首页>Leetcode simple problem building arrays with stack operation
Leetcode simple problem building arrays with stack operation
2022-06-29 05:53:00 【·Starry Sea】
subject
Give you an array of targets target And an integer n. Every iteration , Need from list = {1,2,3…, n} Read a number in sequence .
Use the following operations to build the target array target :
Push: from list Read a new element in , And push it into the array .
Pop: Delete the last element in the array .
If the target array is built , Stop reading more elements .
The title data ensures that the target array is strictly incremented , And only contains 1 To n Number between .
Please return the operation sequence used to build the target array .
The question data ensures that the answer is unique .
Example 1:
Input :target = [1,3], n = 3
Output :[“Push”,“Push”,“Pop”,“Push”]
explain :
Read 1 And automatically push the array -> [1]
Read 2 And automatically push the array , Then delete it -> [1]
Read 3 And automatically push the array -> [1,3]
Example 2:
Input :target = [1,2,3], n = 3
Output :[“Push”,“Push”,“Push”]
Example 3:
Input :target = [1,2], n = 4
Output :[“Push”,“Push”]
explain : Just before reading 2 A number can stop .
Example 4:
Input :target = [2,3,4], n = 4
Output :[“Push”,“Pop”,“Push”,“Push”,“Push”]
Tips :
1 <= target.length <= 100
1 <= target[i] <= 100
1 <= n <= 100
target It's strictly incremental
source : Power button (LeetCode)
Their thinking
Since the given target It's strictly incremental , Then we just need to traverse the generated sequence from front to back , When the encountered value is not equal to the current target Elements , Then press it directly into the stack and then pop it up , If it is equal to , Just push into the stack . Or transform the sequence into a queue , Just go out and compare .
class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
operating=[]
i=1
j=0
while i<n+1 and j<len(target):
if i==target[j]:
operating.append('Push')
j+=1
i+=1
else:
operating.extend(['Push','Pop'])
i+=1
return operating

class Solution:
def buildArray(self, target: List[int], n: int) -> List[str]:
operating=[]
Q=deque(range(1,n+1))
i=0
while Q and i<len(target):
temp=Q.popleft()
if temp==target[i]:
operating.append('Push')
i+=1
else:
operating.extend(['Push','Pop'])
return operating

边栏推荐
- Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code
- D parallel and rotator
- 51 lines of code, self-made TX to MySQL software!
- How to use thread stack location
- patent filter
- Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
- D Author: import C programming in D
- Can use the mouse, will reinstall the computer system tutorial sharing
- [high concurrency] deeply analyze the callable interface
- Creation of Arduino uno development environment
猜你喜欢

β- Tetraphenyl nickel porphyrin with all chlorine substitution| β- Thiocyano tetraphenyl porphyrin copper| β- Dihydroxy tetraphenyl porphyrin 𞓜 2-nitroporphyrin | supplied by Qiyue

Manual (functional) test 1

Plugin

Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code

Research Report on the new energy industry of recommended power equipment in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report

Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report

The fresh student who was born in Ali after 2000: it's really fragrant to mend this

PCI Verilog IP

2022 recommended cloud computing industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
![[C language series] - branch and loop statements](/img/bf/656c9189b4ab4387c5acab1c4a2804.jpg)
[C language series] - branch and loop statements
随机推荐
Open source demo| you draw and I guess -- make your life more interesting
Testing grpc service with grpcui
Can use the mouse, will reinstall the computer system tutorial sharing
Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
JIRA basic usage sharing
Analysis report on the investment market situation of the development planning prospect of the recommended chip industry research industry in 2022 (the attachment is a link to the network disk, and th
Rich material libraries make modeling easy and efficient for developers
D Author: import C programming in D
The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
2022 recommended trend toy blind box industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuously up
What if the hard disk fails to recognize how to recover data
ASP. Net core 6 framework unveiling example demonstration [03]:dapr initial experience
Robot reinforcement learning -- first person vs third person
Quickly write MVVM code using source generators
Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?
Introduction to Photoshop (the first case)
QT precautions and RCC download address
Why Houdini made the pyside2 plug-in crash
patent filter
CodeIgniter active record not equal - CodeIgniter active record not equal