还是英文的哦!
Reverse a Singly Linked List without using Recursion.
///
/// Revert a single linked list ///
/// ///
public static SingleLinkedListNode Reverse(SingleLinkedListNode root) {
if (root == null)
throw new ArgumentNullException(\);
SingleLinkedListNode iter = root, next = root.Next; SingleLinkedListNode newRoot = root; root.Next = null;
while (next != null) {
newRoot = next.Next; next.Next = iter; iter = next; next = newRoot; }
return iter; }
Reverse a Singly Linked List with using Recursion.
编程找出N以下的所有素数
void findPrimes(bignum topCandidate) { bignum candidate = 2; while(candidate <= topCandidate) { bignum trialDivisor = 2; int prime = 1; while(trialDivisor * trialDivisor <= candidate) { if(candidate % trialDivisor == 0) { prime = 0; break; } trialDivisor++; } if(prime) printPrime(candidate); candidate++; } }
Reverse every word in a String (abc def becomes cba fed).
1. private static string ReverseStringByWord(string input) 2. { 3. if (string.IsNullOrEmpty(input)) 4. throw new ArgumentNullException(\); 5. 6. char[] sb = new char[input.Length]; 7. int lastIndex = input.Length; 8. while ((lastIndex > 0) && char.IsWhiteSpace(input[lastIndex - 1])) lastIndex--; 9. 10. int appendIndex = 0; 11. for (int i = lastIndex - 1; i >= 0; i--) 12. { 13. char c = input[i]; 14. // if (char.IsWhiteSpace(input[i])) 15. if ( c == ' ' || c == '\\t' || c == '\\n' || c == '\\r' ) 16. { 17. for (int j = i + 1; j < lastIndex; ++j) 18. sb[appendIndex++] = input[j]; 19. 20. sb[appendIndex++] = input[i]; 21. lastIndex = i; 22. } 23. } 24. 25. for (int j = 0; j < lastIndex; ++j) 26. sb[appendIndex++] = input[j]; 27. 28. return new string(sb); 29. }
Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.
1. public static int Parse(string args) 2. {
3. // Author: Yimin Shi. 2008-05-30
4. // 这个函数也有一个BUG, 比如在处理字符串 \时候,就不会throw OverflowException.
5. if (string.IsNullOrEmpty(args))
6. throw new ArgumentNullException(\); 7.
8. int iter = 0;
9. bool bNegative = false; 10. int result = 0; 11. // Skip whites
12. while (iter < args.Length && (args[iter] == ' ')) iter++; 13.
14. if (args[iter] == '-') 15. {
16. bNegative = true; 17. iter++; 18. }
19. else if (args[iter] == '+') 20. iter++; 21.
22. for (; iter < args.Length; ++iter) 23. {
24. if (args[iter] >= '0' && args[iter] <= '9')
25. {
26. int cur = args[iter] - '0'; 32.
33. checked(result *= 10); 34.
35. result = bNegative ? result - cur 36. : result + cur; 42. } 43. else
44. break; 45. } 46.
47. // Skip whites
48. while (iter < args.Length && (args[iter] == ' ')) iter++; 49. if (iter != args.Length)
50. throw new ArgumentException(); 51.
52. return result; 53. }
Implement strstr() (or some other string library function).
char * __cdecl strstr ( const char * str1, const char * str2 ) {
char *cp = (char *) str1; char *s1, *s2;
if ( !*str2 )
return((char *)str1);
while (*cp) {
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) ) s1++, s2++;
if (!*s2)
return(cp);
cp++; }
return(NULL); }
Implement an algorithm to print out all files below a given root node.
public abstract class AbstractGraph : AbstractContainer, Graph {
protected int numberOfVertices; protected int numberOfEdges; protected Vertex[] vertex;
public void BreadthFirstTraversal( Visitor visitor, int start) { bool[] enqueued = new bool[numberOfVertices]; for (int v = 0; v < numberOfVertices; ++v) enqueued[v] = false; Queue queue = new QueueAsLinkedList(); queue.Enqueue(vertex[start]); enqueued[start] = true; while (!queue.IsEmpty && !visitor.IsDone) { Vertex v = (Vertex)queue.Dequeue(); visitor.Visit(v); foreach (Vertex to in v.Successors) { if (!enqueued[to.Number]) { queue.Enqueue(to); enqueued[to.Number] = true; } } } } }
现有一单链表,输出从尾端开始的第5个数值,并写出相应的测试用例
Given two linked lists which have data in ascending order, how would you merge them
///
/// Merge two sorted linked list in sorted order ///
/// /// ///
/// The result sorted linked list ///
/// For e.g. ///
SingleLinkedListNode.PrintLinkedList(SingleLinkedListNode.OrderedMerge(SingleLinkedListNode.InitializeTestList(new int[] { 1, 2, 4, 5, 6, 7, 8, 9 }),
SingleLinkedListNode.InitializeTestList(new int[] { 3, 6, 8, 9, 10 }))); /// - returns -
/// 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 6 -> 7 -> 8 -> 8 -> 9 -> 9 -> 10 -> /// ///
SingleLinkedListNode.PrintLinkedList(SingleLinkedListNode.OrderedMerge(SingleLinkedListNode.InitializeTestList(new int[] { 1, 2, 4, 5, 7 }),
SingleLinkedListNode.InitializeTestList(new int[] { 3, 6, 8, 9, 10 }))); /// - returns -
/// 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> ///
相关推荐: