SnapChat 面试题:HTML Parser
来源:互联网 发布:企业即时通讯软件 免费 编辑:程序博客网 时间:2024/05/16 00:35
题目:
给一个html文件,生成一个包含所有tag的树状结构。可以假设html格式是valid,即每一个tag一定有ending tag,而且tag的顺序也是valid。不会出现<div><span></div></span>的情况。
// This is the text editor interface. // Anything you type or change here will be seen by the other person in real time./*<html> <head> </head> <body> <div> <div> </div> <div> </div> </div> <span> </span> <img> </body></html>*/import java.util.*;class TreeNode { String tag; List<TreeNode> children; TreeNode(String t) { tag = t; children = new ArrayList<>(); }}class HtmlParser { public TreeNode dummyRoot = new TreeNode(""); public TreeNode parser(String document) { findChildren(dummyRoot, document); return dummyRoot; } public void findChildren(TreeNode root, String document) { //need to find all the children here int start = 0; while(document.indexOf("<", start) != -1) { int begin = document.indexOf("<", start); int end = document.indexOf(">", start); String tag = ""; if(begin != -1 && end != -1) tag = document.substring(begin + 1, end); else return; if(tag.equals("img")) { TreeNode node = new TreeNode(tag); root.children.add(node); start = end + 1; continue; } int i = end; int count = 1; while(document.indexOf(tag, i) != -1) { int p = document.indexOf(tag, i); if(document.charAt(p - 1) == '/') { count--; if(count == 0) { TreeNode node = new TreeNode(tag); root.children.add(node); findChildren(node, document.substring(end + 1, p - 2)); i = p + tag.length() + 1; break; } } else if(document.charAt(p - 1) == '<') { count++; } i = p + tag.length() + 1; } start = i; } }}public class Solution { public static void main(String[] args) { HtmlParser p = new HtmlParser(); String s = "<html><head></head><body><div><div></div><div></div></div><span></span><img></body></html>"; TreeNode root = p.parser(s); }}
用了递归。
0 0
- SnapChat 面试题:HTML Parser
- 403. Snapchat面试题:青蛙过河/653. Two Sum IV
- Snapchat面经
- HTML常见面试题
- html笔试面试题
- html面试题
- HTML常见面试题
- HTML面试题
- html+css面试题
- html+css面试题
- HTML常见面试题
- html+css面试题
- HTML+CSS面试题
- html前端面试题
- HTML parser
- html parser
- HTML::Parser
- Snapchat 面经(电面+onsite)
- 外部排序
- 一起来做chrome扩展《使用代理proxy》
- Service API Guides
- 黑马程序员_javaCode的数组2
- LeetCode-Simplify Path
- SnapChat 面试题:HTML Parser
- my frist linux
- Wildcard matching
- Log4j2使用案例
- POJ 2676 解题报告
- [Leetcode]Letter Combinations of a Phone Number My Submissions Question Solution
- [汇编语言]debug中用r命令修改cs、ip,并不能影响a命令的代码起始写入地址?
- HtmlUnit模拟登录网站时注意事项
- *LeetCode-Longest Palindromic Substring