面试遇到的题目及解决办法

1.用js写一个类似Dom的getElementByTagName的函数。

试验了一下,js的childNodes属性有兼容性和准确度的问题,经常会多一些空白标签,所以用深搜的方法根本行不通,没法建树,没法准确获取节点树信息。

所以,想到了正则表达式,只用几行代码即可搞定。

 1             window.onload = function () {
 2             var reg = new RegExp("<span.+/span>", "g");
 3             var element = new Array();
 4             var temp;
 5             while ((temp = reg.exec(document.body.innerHTML)) != null) {
 6                 element.push(temp);
 7             }
 8             while ((temp = element.pop()) != null) {
 9                 alert(temp);
10             }
11         }    

之后,又想到另一种DFS深搜解法

 1      class Program
 2     {
 3         private static XmlElement _root;
 4         public static XmlElement Root
 5         {
 6             get { return _root; }
 7             set { _root = value; }
 8         }
 9 
10         private static List<XmlNode> list = new List<XmlNode>();
11         static void Main(string[] args)
12         {
13             WebClient wc = new WebClient();
14             string htmlStr = wc.DownloadString("http://localhost:30907/test1.html");
15             XmlDocument doc = new XmlDocument();
16             doc.LoadXml(htmlStr);
17             Root = doc.DocumentElement;
18             
19             DFS(Root);
20             foreach (var n in list)
21             {
22                 Console.WriteLine(n.OuterXml);
23             }
24             
25             Console.ReadKey();
26         }
27 
28         private static void DFS(XmlNode parent)
29         {
30             
31             if (!parent.HasChildNodes)
32             {
33                 return;
34             }
35             else if (parent.HasChildNodes)
36             {
37                 for (int j = 0; j < parent.ChildNodes.Count; j++)
38                 {
39                     if (parent.ChildNodes[j].Name == "span")
40                     {
41                         list.Add(parent.ChildNodes[j]);
42                     }
43                     DFS(parent.ChildNodes[j]);
44                 }
45             }
46         }
47

如果用宽搜BFS也可以,时间复杂度都是o(n)

 1     class Program
 2     {
 3         private static List<XmlNode> list = new List<XmlNode>();
 4         private static Queue<XmlNode> queue = new Queue<XmlNode>();
 5         static void Main(string[] args)
 6         {
 7             WebClient wc = new WebClient();
 8             string htmlStr = wc.DownloadString("http://localhost:30907/test1.html");
 9             XmlDocument doc = new XmlDocument();
10             doc.LoadXml(htmlStr);
11             XmlElement root = doc.DocumentElement;
12             BFS(root);
13 
14             foreach (var n in list)
15             {
16                 Console.WriteLine(n.OuterXml);
17             }
18 
19             Console.ReadKey();
20         }
21 
22         public static void BFS(XmlNode parent)
23         {
24             queue.Enqueue(parent);
25             while (queue.Count != 0)
26             {
27                 XmlNode n = queue.Dequeue();
28                 if (n.Name == "span")
29                 {
30                     list.Add(n);
31                 }
32                 for (int i = 0; i < n.ChildNodes.Count; i++)
33                 {
34                     queue.Enqueue(n.ChildNodes[i]);
35                 }
36             }
37         }
38     }

2.用js写一个链表,或者用其他语言。

我比较熟C#所以就用C#写,类似List。

 1     public class Node
 2     {
 3         public int Num{get;set;}
 4         public Node Next{get;set;}
 5     }
 6 
 7     public class MyList
 8     {
 9         public Node Head { get; set; }
10         public int Length { get; set; }
11 
12         public MyList()
13         {
14             Head = new Node();
15             Head.Next = null;
16             Length = 1;
17         }
18 
19         public void Add(int num)
20         {
21             Node n = new Node();
22             n.Num = num;
23             Node node = Head;
24             while (node.Next != null)
25             {
26                 node = node.Next;
27             }
28             node.Next = n;
29             Length++;
30         }
31 
32         public void Delete(int index)
33         {
34             Node n = Head;
35             if (index == 0)
36             {
37                 Head = n.Next;
38             }
39             else if(Length-1==index)
40             {
41                 for (int i = 0; i < index - 1; i++)
42                 {
43                     n = n.Next;
44                 }
45                 n.Next = null;
46             }
47             else
48             {
49                 for (int i = 0; i < index - 1; i++)
50                 {
51                     n = n.Next;
52                 }
53                 n.Next = n.Next.Next;
54             }
55             Length--;
56         }
57 
58         public int this[int index]
59         {
60             get {
61                 Node n = Head;
62                 for (int i = 0; i < index; i++)
63                 {
64                     n = n.Next;
65                 }
66                 return n.Num;
67             }
68             set  {
69                 Node n = Head;
70                 for (int i = 0; i < index; i++)
71                 {
72                     n = n.Next;
73                 }
74                 n.Num = value;
75             }
76         }
77     }

 

posted @ 2015-04-08 13:38  Sunnier  阅读(641)  评论(0编辑  收藏  举报