当前位置:网站首页>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

边栏推荐
- 2022-02 Microsoft vulnerability notification
- data management plan
- Manual (functional) test 1
- Would like to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- Set column width in jitter - set column width in jitter
- Open source demo| you draw and I guess -- make your life more interesting
- 5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
- Signal slot mechanism
- QT precautions and RCC download address
猜你喜欢

Introduction to Photoshop (the first case)

使用VS创建静态链接库.lib并使用
![[high concurrency] deeply analyze the callable interface](/img/dc/174f97fdd27180ed210d76768cc345.jpg)
[high concurrency] deeply analyze the callable interface

What has urbanization brought to our mental health and behavior?
![[high concurrency] deeply analyze the callable interface](/img/17/93056547aa1a2b342e1b159b7c41c2.jpg)
[high concurrency] deeply analyze the callable interface

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?

Boost the digital economy and face the future office | the launch of the new version of spreadjsv15.0 is about to begin

Why can't the article be posted?

Tcapulusdb Jun · industry news collection (V)

2022 community group buying industry research industry development planning prospect investment market analysis report (the attachment is the online disk link, and the report is continuously updated)
随机推荐
2022-02 Microsoft vulnerability notification
AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
HTTP Caching Protocol practice
机器人强化学习——Transferring End-to-End Visuomotor Control from Simulation to RealWorld (CoRL 2017)
Pytest (7) -yield and termination function
5,10-di (4-aminophenyl) - 15,20-diphenylporphyrin (cis-dadph2) /5,15-di (4-aminophenyl) - 10,20-diphenylporphyrin (trans-dadph2) / (tri-apph2) supplied by Qiyue
[CV] wuenda machine learning course notes Chapter 13
Ctrip launched the "3+2" office mode. Are you sour?
Use VS to create a static link library Lib and use
2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up
Signal slot mechanism
Multiline regular expression search in Visual Studio code - multiline regular expression search in Visual Studio code
HTTP Caching Protocol practice
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Week 10 - task 0- execution process instance resolution of constructors and destructors
Stack -- 739 Daily temperature
Analysis report on the investment market of the development planning prospect of the recommended NFT industry research industry in 2022 (the attachment is a link to the online disk, and the report is
The win11 file resource manager has an explicit Caton, and Microsoft promises to improve the performance in 2022
Top ten Devops best practices worthy of attention in 2022
How to use regex in file find