蔚来7.1号笔试题,第三个笔试题 HTML标签匹配 有无hxd给个答案。
#蔚来笔试#蔚来#
#蔚来笔试#蔚来#
全部评论
https://www.nowcoder.com/discuss/977525
就是**里面的有小括号题目的复杂化而已,原理是一样的
https://leetcode.cn/problems/valid-parentheses/
随机输出yes和no,多提交几次,能过80%🤣
go的,没有全部过,88%好像是
package main
import (
"fmt"
"strings"
)
func main() {
var s string
fmt.Scan(&s)
s_t := strings.Split(s, "><")
var stack []string
for i := 0; i < len(s_t); i++ {
if strings.HasPrefix(s_t[i], "<") {
s_t[i] = s_t[i][1:]
} else if strings.HasSuffix(s_t[i], ">") {
s_t[i] = s_t[i][:len(s_t[i])-1]
stack = append(stack)
} else {
stack = append(stack, s_t[i])
}
}
if len(s_t) == 1 {
fmt.Println("NO")
return
}
for i := 0; i < len(s_t); i++ {
if strings.HasPrefix(s_t[i], "/") {
if len(stack) == 0 {
fmt.Println("NO")
return
} else if stack[len(stack)-1] != s_t[i][1:] {
fmt.Println("NO")
return
} else if stack[len(stack)-1] == s_t[i][1:] {
stack = stack[:len(stack)-1]
}
} else {
stack = append(stack, s_t[i])
}
}
fmt.Println("YES")
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String str = sc.next();
Deque<Character> stackCh = new ArrayDeque<>();//存 '<'
Deque<StringBuilder> stackSB = new ArrayDeque<>();//存<>里面的字符串
int len = str.length();
int cur = 0;
while (cur < len) {
if (str.charAt(cur) == '<') {
stackCh.push('<');
if (str.charAt(cur + 1) == '/') {
if (stackSB.isEmpty()) {
break;
}
cur += 2;
StringBuilder sec = stackSB.poll();
int sbLen = sec.length();
String tmpStr = str.substring(cur, cur + sbLen);
if (judgeStr(sec.toString(), tmpStr)) {
cur += sbLen;
if (str.charAt(cur) == '>') {
cur++;
continue;
} else {
break;
}
} else {
break;
}
} else {
cur++;
char ch = str.charAt(cur);
StringBuilder sb = new StringBuilder();
while (cur < len && ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) {
sb.append(ch);
cur++;
ch = str.charAt(cur);
}
if (sb.isEmpty()) break;
stackSB.push(sb);
}
} else if (str.charAt(cur) == '>') {
if (stackCh.isEmpty()) {
break;
}
stackCh.poll();
cur++;
} else {
break;
}
}
if (cur == len) {
if(!stackSB.isEmpty()) {
System.out.println("NO");
} else {
System.out.println("YES");
}
} else {
System.out.println("NO");
}
}
}
private static boolean judgeStr(String sec, String s) {
int len1 = sec.length(), len2 = s.length();
if(len1 != len2) return false;
for(int i = 0;i < len1;i++) {
if(sec.charAt(i) != s.charAt(i)) {
return false;
}
}
return true;
}
相关推荐
点赞 评论 收藏
分享
02-08 11:05
广东科技学院 全栈开发 点赞 评论 收藏
分享
点赞 评论 收藏
分享