In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces Java how to delete all adjacent duplicates in the string of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that after reading this Java how to delete all adjacent duplicates in the string article will have a harvest, let's take a look.
Algorithm:
Stack a more common scenario is the operation of strings, such as de-duplication, backspace, string representation of the path, etc., the operation is often relatively simple.
1. First put the most conditional string into the stack 2. Judge whether to go out of stack 3 according to the new elements. The most comparative elements are often stored in the stack. When comparing, sometimes the elements at the top of the stack are compared, and sometimes the whole stack is compared.
Topic 1: delete all adjacent duplicates in the string
Code implementation:
Func removeDuplicates (S string) string {ss: = [] byte (S) var stacks [] byte for i: = 0; I < len (ss) IDiv + {if len (stacks)! = 0 {jvv = len (stacks)-1 if ss [I] = = stacks [j] {/ / if it is the same as the top stack element Delete the data in the stack stacks = stacks [: J] continue}} stacks = append (stacks,ss [I]) / / otherwise enter the stack} return string (stacks)} / * the use of the stack, the following elements are the same as the top elements of the stack, off the stack and the new elements do not enter the stack. All the other scenes are on the stack. , /
Topic 2: compare strings with backspace
Code implementation:
Func backspaceCompare (S string, T string) bool {s: = getStack (S) t: = getStack (T) return s = t} func getStack (S string) string {ss: = [] byte (S) var stacks [] byte for iVAN0 I 0 {/ / Note "# abc" in the first case stacks = stacks [: len (stacks)-1]}} return string (stacks)}
Topic 3: simplify the path
Code implementation:
Import "strings" func simplifyPath (path string) string {tmps: = strings.Split (path, "/") var paths [] string for _, s:=range tmps {if len (s)! = 0 {paths = append (paths,s)}} / / fmt.Println ("name", paths) var stacks [] string for iVO I 0 {stacks=stacks [: len (stacks)-1]} continue} if paths [I]! = "." {stacks=append (stacks,paths [I])}} / / fmt.Println ("name", stacks,len (stacks)) resStr: = "" for idx S:=range stacks {resStr + = s if idx! = len (stacks)-1 {resStr + = "/"}}
This is the end of the article return "/" + resStr} on how Java deletes all adjacent duplicates in a string. Thank you for reading! I believe you all have a certain understanding of "how Java deletes all adjacent duplicates in a string". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.