我想到了兩種比較簡單的方法面試
第一種是須要開一個新的鏈表,將原鏈表的元素從後到前的插入到新鏈表中(也就是原鏈表第一個元素被插入成新鏈表的最後一個元素)。算法
第二種是不須要開新的鏈表,而是逐步反轉原鏈表中元素的指向,例如:數組
原鏈表是 1->2->3->4->null 被 逐步修改成 ①2->1->null、3->4->null ②3->2->1->null、4->null ③4->3->2->1->null數據結構
最後再將head指向4。spa
1 namespace 鏈表反轉 2 { 3 class Node 4 { 5 public int Num { get; set; } 6 public Node Next { get; set; } 7 } 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 NodeList list = new NodeList(); 13 list.Add(1); 14 list.Add(2); 15 list.Add(3); 16 list.Add(4); 17 list.Add(5); 18 list.Reverse1(); 19 list.Print(); 20 21 NodeList list1 = list.Reverse(); 22 list1.Print(); 23 24 Console.ReadKey(); 25 } 26 } 27 28 class NodeList 29 { 30 public Node Head { get; set; } 31 public void Add(int num) 32 { 33 if (Head == null) 34 { 35 Head = new Node(); 36 Head.Next = new Node() { Num = num }; 37 } 38 else 39 { 40 Node tn = Head; 41 while (tn.Next != null) 42 { 43 tn = tn.Next; 44 } 45 tn.Next = new Node() { Num = num }; 46 } 47 } 48 49 public void Print() 50 { 51 Node tn = Head; 52 while(tn.Next!=null) 53 { 54 tn = tn.Next; 55 Console.WriteLine(tn.Num); 56 } 57 } 58 59 //須要開新鏈表的反轉 60 public NodeList Reverse() 61 { 62 NodeList list = new NodeList(); 63 Node tn = Head; 64 Node newTn = null; 65 while (tn.Next != null) 66 { 67 tn = tn.Next; 68 if (newTn == null) 69 { 70 newTn = new Node() { Num = tn.Num }; 71 } 72 else 73 { 74 newTn = new Node() { Num = tn.Num,Next=newTn }; 75 } 76 } 77 list.Head = new Node(); 78 list.Head.Next = newTn; 79 return list; 80 } 81 82 //不須要開新鏈表的反轉 83 public void Reverse1() 84 { 85 Node n1 = Head.Next; 86 Node n2 = Head.Next.Next; 87 //第一個節點也就是反轉後的最後一個節點,Next要指向null 88 Node first = Head.Next; 89 first.Next = null; 90 //若是鏈表爲空或者鏈表只有一個元素那就無需調整 91 if (n2 == null || n1 ==null) 92 { 93 return; 94 } 95 //先用Head指向n2的下一個元素,再將n2的Next指向n1,最後將n1和n2向右移動 96 while (n2.Next != null) 97 { 98 Head.Next = n2.Next; 99 n2.Next = n1; 100 n1 = n2; 101 n2 = Head.Next; 102 } 103 //最後要將頭結點指向鏈表反轉前的最後一個元素 104 Head.Next = n2; 105 //由於判斷條件只調整到最後一個元素的前一個元素,因此要調整最後一個元素的Next指向 106 n2.Next = n1; 107 } 108 }
基本原理:第一遍遍歷字符串時,用hash表存儲每一個字符出現的次數,第二遍遍歷字符串時,篩選出第一個hash中對應保存的出現次數爲1的字符。code
1 namespace 字符串查找第一個不重複的字母 2 { 3 class Program 4 { 5 static Hashtable hash = new Hashtable(); 6 static void Main(string[] args) 7 { 8 string s = "asfgasjfoiwoeqkwzxc"; 9 Count(s); 10 Console.WriteLine(Search(s)); 11 Console.ReadKey(); 12 } 13 14 static void Count(string s) 15 { 16 for (int i = 0; i < s.Length; i++) 17 { 18 if (hash.Contains(s[i])) 19 { 20 hash[s[i]] = (int)hash[s[i]]+1; 21 } 22 else 23 { 24 hash[s[i]] = 1; 25 } 26 } 27 } 28 29 static char Search(string s) 30 { 31 for (int i = 0; i < s.Length; i++) 32 { 33 if ((int)hash[s[i]] == 1) 34 { 35 return s[i]; 36 } 37 } 38 return '#'; 39 } 40 } 41 }
基本原理:找到正負數分界點0,若是存在就是0,不存在就比較它左右兩個數的絕對值。blog
(PS:爲了方便我寫到一個JUnit方法中)排序
public class testAlgorithm { @Test public void Fun(){ int[] a = {-8,-7,-5,1,3,5}; int middle = (a.length-1)/2; int result = middle; if (a[middle]==0){ System.out.println("The smallest number is "+a[middle]); return; } //find the demarcation point else{ if(a[result]>0){ while(a[result]>0){ result = result - 1; } if(Math.abs(a[result])>a[result+1]){ System.out.println("The smallest number is "+a[result+1]); return; } else{ System.out.println("The smallest number is "+a[result]); return; } } else{ while(a[result]<0){ result = result + 1; } if(a[result]>Math.abs(a[result+1])){ System.out.println("The smallest number is "+a[result+1]); return; } else{ System.out.println("The smallest number is "+a[result]); return; } } } } }