题解 | #字符串解码#正则表达式与递归求解
字符串解码
https://www.nowcoder.com/practice/4e008fd863bb4681b54fb438bb859b92
package main import ( "fmt" "regexp" "strconv" "strings" ) /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ func decodeString(s string) string { // write code here var patRegexp = `[0-9]\[[a-z]*]` re, err := regexp.Compile(patRegexp) if err != nil { return "" } if re.MatchString(s) { UnicodeString(&s, re) } return s } func UnicodeString(s *string, re *regexp.Regexp) { newSlice := re.FindAllString(*s, -1) fmt.Println(newSlice) for i := 0; i < len(newSlice); i++ { var times = newSlice[i][0] var repeatString = newSlice[i][1:] repeatString = strings.TrimLeft(repeatString, "[") repeatString = strings.TrimRight(repeatString, "]") n, err := strconv.Atoi(string(times)) if err != nil { return } repeatString = strings.Repeat(repeatString, n) *s = strings.Replace(*s, newSlice[i], repeatString, -1) } if re.MatchString(*s) { UnicodeString(s, re) } }#正则表达式##递归##go#