Algorithmic Questions C #

Source: Internet
Author: User
Tags string find

several large companies (IBM, MicroSoft and so on) interview classic data structures and algorithmic questions C # answers 1. List reversal

I thought of two simple ways.

The first is the need to open a new linked list, the original list of elements from the back-to-front insertion into the new linked list (that is, the first element of the original list is inserted into the new list of the last element).

The second is that you do not need to open a new list, but instead gradually reverse the direction of the elements in the original list, for example:

The original list is 1->2->3->4->null is gradually modified to ①2->1->null, 3->4->null②3->2->1->null, 4->null③4- >3->2->1->null

Finally, point head to 4.

  1 Namespace List reversal 2 {3 class Node 4 {5 public int Num {get; set;}  6 Public Node Next {get; set;}  7} 8 Class program 9 {ten static void Main (string[] args) One {NodeList list = new NodeList (); List. ADD (1); List. ADD (2); List. ADD (3); List. ADD (4); List. ADD (5); List. Reverse1 (); List. Print (); NodeList List1 = list. Reverse (); List1. Print (); Console.readkey (); (+)-class NodeList {public Node Head {get; set;} to public void                 ADD (int num) + {(head = = NULL) + + + head = new Node (); 36 Head.next = new Node () {num = num}; PNS}-Else (+)Ile (TN. Next! = null) (TN = TN). Next; . Next = new Node () {num = num};             (+)------() ()--public void Print () () {-Node tn = Head; 52 while (TN. Next!=null) (tn = TN). Next; Console.WriteLine (TN. Num);             56} 57} 58 59//need to open a new linked list of the reverse public NodeList Reverse () 61 {62 NodeList list = new NodeList (); The Node tn = Head; Node NEWTN = null; (TN. Next! = null) (+ +) (tn = TN). Next; if (NEWTN = = null), NEWTN = new Node () {Num = tn. Num}; NEWTN = new Node () {Num = tn. NUM,NEXT=NEWTN};            75} 76 } list. Head = new Node (); List. Head.next = NEWTN; return list; 80} 81 82//Do not need to open the new linked list of the reversal of the public void Reverse1 (), (). {N1 = Head.next ; N2 Node = Head.Next.Next; 87///The first node is the last node after the reversal, and Next will point to null for the X node, and Head.next; First. Next = null; 90//If the list is empty or if the list has only one element then you do not need to adjust the N2 = = NULL | | N1 ==null) Turn 94} 95//First point to the next element of N2, then point to N2 next to N1, and finally move N1 and N2 to the right of the (N2). Next! = null) 98 {Head.next = N2. Next; N2. Next = n1;100 N1 = n2;101 N2 = head.next;102}103//Last to point the head node to the list before reversing The last element of 104 Head.next = n2;105//Because the judging condition only adjusts to the previous element of the last element, so to adjust the last element's Next point to 106 n2.           Next = N1; 107}108    } 
2. String to find the first non-repeating letter

Rationale: The first time the string is traversed, the hash table is used to store the number of occurrences of each character, the second time traversing the string, filter out the first hash of the corresponding saved occurrences of 1 characters.

 1 namespace string find first non-repeating letter 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); Console.WriteLine (Search (s)); Console.readkey ();}13-static void Coun T (string s) {(int i = 0; i < s.length; i++) (hash).                 Contains (S[i])) {Hash[s[i]] = (int) hash[s[i]]+1;21}22         Else23 {Hash[s[i]] = 1;25}26}27}28 29                 Static char Search (string s) (int i = 0; i < s.length; i++) 32 {33             if ((int) hash[s[i]] = = 1) {return s[i];36}37 }38 return ' # '; 39         }40}41} 

Essay classification-data structure and algorithms Big company interview classic data structure and algorithmic questions C # solution Summary: Several large companies (IBM, MicroSoft and so on) interview classic data structures and algorithmic questions C # Answer 1. List reversal I thought of two simpler methods. The first is the need to open a new linked list, inserting the elements from the original list into the new linked list (that is, the first element of the original list is inserted into the last element of the new list). The second is that you do not need to open a new linked list, but instead gradually reverse the original list of elements ... Read full posted @ 2015-05-03 20:43 Sunnier read (352) | Comments (0) editing common data structures and algorithms C # implementation Summary: Common data structure and algorithm C # implementation 1. Bubble sort, select sort, insert sort (three simple non-recursive sort) 1 int[] Waitsort = {1, 0, 12, 13, 14, 5, 6, 7, 8, 9, 1 0}; 2 3//bubble sort 4 ... Read full posted @ 2015-04-17 23:52 Sunnier read (36) | Comments (0) Edit categories: Data Structures and algorithms

Algorithmic Questions C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.