public static SingleLinkedListNode OrderedMerge(SingleLinkedListNode first, SingleLinkedListNode second) {
if (first == null)
throw new ArgumentNullException(\); if (second == null)
throw new ArgumentNullException(\);
SingleLinkedListNode less = first.Value < second.Value ? first : second; SingleLinkedListNode larger = first.Value > second.Value ? first : second; SingleLinkedListNode newRoot = less; SingleLinkedListNode iter = newRoot;
less = less.Next;
while (less != null && larger != null) {
if (less.Value < larger.Value) {
iter.Next = less; less = less.Next; } else {
iter.Next = larger; larger = larger.Next; }
iter = iter.Next; }
while (less != null) {
iter = iter.Next = less; less = less.Next; }
while (larger != null) {
iter = iter.Next = larger; larger = larger.Next; }
return newRoot; }
Implement an algorithm to do wild card string matching.
1. private static bool WildCardMatch(string pattern, string input) 2. { 3. if (string.IsNullOrEmpty(pattern)) 4. throw new ArgumentNullException(\); 5. if (string.IsNullOrEmpty(input)) 6. throw new ArgumentNullException(\); 7. 8. int inputIter = 0; 9. int patternIter = 0; 10. for (; patternIter < pattern.Length; ++patternIter) 11. { 12. if (pattern[patternIter] == '*') 13. { 14. while ((patternIter < pattern.Length - 1) && (pattern[patternIter + 1] == '*')) patternIter++; 15. if (patternIter == pattern.Length - 1)
16. return true; 17. 18. while ((patternIter < pattern.Length) && 19. (inputIter < input.Length) && 20. (pattern[patternIter + 1] != input[inputIter])) 21. { 22. inputIter++; 23. } 24. } 25. else 26. { 27. if (inputIter >= input.Length) break; 28. 29. if (pattern[patternIter] != input[inputIter++]) 30. return false; 31. } 32. } 33. 34. return patternIter == pattern.Length && inputIter == input.Length; 35. }
Write a routine that prints out a 2-D array in spiral order!
int
left=0,top=0,right=3,bottom=3,CurrentRow=0,CurrentCulom=0,step;
int[,] Num = new int[,] { { 1, 2, 3,4 }, { 5, 6 ,7,8}, { 9,10,11,12 }, { 13,14,15,16 } };
//rule define:{X,Y},if X=0,traversal colum,if X=1,traversal row;if Y=1,increase by degrees,if Y=-1,decrease by degrees
//int[,] rule = new int[,] { { 1, 1 }, { 0, 1 }, { 1, -1 }, { 0, -1 } };//clockwise
int[,] rule = new int[,] { { 0, 1 }, { 1, 1 }, { 0, -1 }, { 1, -1 } }; //counter-clockwise
int a = Num.Rank; int b = Num.Length; int process=0; while (true) {
switch (rule[process, 0]) {
case 1://row
step = rule[process, 1];
for (; CurrentCulom >= left && CurrentCulom <= right; CurrentCulom += step)
{
Console.WriteLine(Num[CurrentRow, CurrentCulom]); }
CurrentCulom -= step; if (CurrentRow == top) {
top++;
CurrentRow++; } else {
bottom--;
CurrentRow--; }
break; case 0://colum
step = rule[process, 1];
for (; CurrentRow >= top && CurrentRow <= bottom; CurrentRow += step)
{
Console.WriteLine(Num[CurrentRow, CurrentCulom]);
}
CurrentRow -= step;
if (CurrentCulom == left) {
CurrentCulom++; left++; } else {
CurrentCulom--; right--; } break; }
if (top > bottom || left > right) break;
process =( process + 1) % 4; } }
Implement a string.Replace.
1. public static string Replace(string strSrc, string oldStr, string newStr) 2. {//Implement a string.Replace. 3.
4. if (string.IsNullOrEmpty(strSrc) || string.IsNullOrEmpty(oldStr) || newStr == null)
5. throw new ArgumentNullException(\); 6.
7. string strResult = \;
8. int lenSrc = strSrc.Length; 9. int lenOld = oldStr.Length;
10. for (int i = 0; i <= lenSrc - lenOld; ) 11. {
12. bool flag = false; 13. int indexOld = 0;
14. while (indexOld < lenOld) 15. {
16. if (strSrc[i + indexOld] != oldStr[indexOld]) 17. break; 18. else
19. indexOld++; 20. }
21. if (indexOld == lenOld) flag = true; 22.
23. if ( flag/*strSrc.Substring(i, lenOld) == oldStr*/) 24. {
25. strResult += newStr;
26. i += lenOld; 27. } 28. else 29. {
30. strResult += strSrc[i]; 31. i++; 32. } 33. } 34.
35. return strResult; 36. }
Count the number of set bits in a number. Now optimize for speed. Now optimize for size.
///
/// Given any integer, return the count of 1 in its binary representation. ///
/// ///
/// The count of 1 in its binary representation, for e.g. ///
/// GetBitCount(3) returns 2, because 3's binary reprenstation is 11. /// GetBitCount(2) returns 1, because 2's binary reprenstation is 10. /// 1. private static int GetBitCount(int value) 2. { 3. int iter = 1; 4. int count = 0; 5. 6. for (int i = 0; i < 32; ++i) 7. { 8. if ((value & iter) != 0) 9. count++; 10. 11. iter <<= 1; 12. } 13. 14. return count; 15. }
Implement an algorithm to sort a linked list. Why did you pick the method you did? Now do it in O(n) time.
1. typedef struct LinkedList 2. {
3. int Value;
4. struct LinkedList *Next; 5. } LINKLIST, *LPLINKLIST; 6.
7. LPLINKLIST InsertionSortLinkedList(LPLINKLIST lpList) 8. {
9. if ( lpList == NULL ) return NULL; 10.
11. LPLINKLIST lpHead = lpList; 12. LPLINKLIST lpIter = lpList->Next; 13.
14. lpHead->Next = NULL;
15. while ( lpIter != NULL ) 16. { 17. LPLINKLIST lpNext = lpIter->Next;
相关推荐: