GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Stack/439.Ternary-Expression-Parser at master · wisdompeak/LeetCode

Expand file tree

/

439.Ternary-Expression-Parser

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

439.Ternary-Expression-Parser

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
439.Ternary Expression Parser.cpp
439.Ternary Expression Parser.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

439.Ternary-Expression-Parser

此题粗看很复杂,实际很明晰。

需要最早解析的字符串,其实是已经形成标准型a?X:Y的这五个连续字符。所以在第一次出现这五个连续字符之前的字符,都可以不断存进一个栈里供后续处理。

具体的算法是,当发现?字符,表明这可能是一组标准型的head。所以把a?之前的字符串扔进栈里,然后重置curRes并顺着a?继续向后探索。如果还有出现类似?的标记,则重复之前的入栈操作;反之,如果之后curRes集齐五个字符,则说明这五个字符是类似a?X:Y的标准型,可以被解析变为一个字符。此时考虑一下栈里的栈顶元素,如果栈顶元素加上当前的这一个字符(curRes),又构成五个字符的话,可以进一步解析;如此重复直至栈顶无法提供凑齐五个字符的string,此时curRes需要顺着expression继续探索下去。

最终返回的是当前的字符串,即curRes。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.