摘要: 例如 0,1,0,3,5,0 变成0,0,0,1,3,5View Code static void SortArrayZero(int[] intArr) { if (intArr == null || intArr.Length == 0) { throw new Exception("input can't be empty"); } int i = intArr.Length - 1; int k = intArr.Le... 阅读全文
posted @ 2013-03-10 14:16 Ligeance 阅读(191) 评论(0) 推荐(0) 编辑
摘要: 奇数层从左到右打印,偶数层从右到左打印。例如:ab cd e f g输出则为 a c b d e f gView Code void SpecialPrintTrees(node root){ Node temp; Stack stack1 = new Stack(); Stack stack2 = new Stack(); stack1.Push(root); while(stack1.Count>0||stack2.Count>0) { while(stack1.Count>0) { temp = sta... 阅读全文
posted @ 2013-03-06 16:34 Ligeance 阅读(397) 评论(0) 推荐(0) 编辑
摘要: 例如"aabbccdeeef",其中eee最长,返回第一个E的位置,应该为7.View Code static int FindStartLargestRepeat(string str) { if (string.IsNullOrEmpty(str)) { throw new Exception("Input can't be empty."); } int count = 1; int maxCount = 1; ... 阅读全文
posted @ 2013-03-06 15:06 Ligeance 阅读(278) 评论(0) 推荐(0) 编辑
摘要: 给一个随机数字的方法Random,以及数字6,输出随机数列3,5,1,2,4,6.View Code static void Randlist(int n) { if (n<=0) { throw new Exception("Input number must be greater than 0."); } int[] intArr = new int[n]; Random rand = new Random(n); ... 阅读全文
posted @ 2013-03-05 16:29 Ligeance 阅读(377) 评论(0) 推荐(0) 编辑
摘要: 例如,字符串1"abcdefg"就包含有字符串2"cb",因为字符串1中有"bc"子字符串,经过排列后可为"cb"。View Code static bool IsPermutation(string str1, string str2) { if (String.IsNullOrEmpty(str1)||String.IsNullOrEmpty(str2)) { throw new Exception("input can't be empty"); } ... 阅读全文
posted @ 2013-03-05 15:26 Ligeance 阅读(291) 评论(0) 推荐(0) 编辑
摘要: 例如,abcdefg,xdefkl,那么最大相同子字符串为defView Code static string LongCommonSubstring(string str1, string str2) { if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2)) { throw new Exception("input can't be empty."); } string res... 阅读全文
posted @ 2013-01-03 15:16 Ligeance 阅读(501) 评论(0) 推荐(0) 编辑
摘要: 例如,给出一个数字5,奇数和则为1+3+5=9。偶数和则为2+4=6。第一种是简单的算法,第二种是改进后,时间复杂度为1的算法。View Code static void SumMath(int n, ref int sumOdd, ref int sumEven) { if (n >= 0) { for (int i = 1; i <= n; i += 2) { checked { ... 阅读全文
posted @ 2012-12-12 14:44 Ligeance 阅读(3517) 评论(0) 推荐(0) 编辑
摘要: 例如 abcdbbfg 变成 bbbacdfg,要求时间复杂度为N,空间复杂度为1,写了两个方法都未达到要求 :(View Code static void MoveDupCharToFront(char[] input) { if (input == null|| input.Length <=1) { throw new Exception("input can't be empty or less than 2 length"); } int... 阅读全文
posted @ 2012-11-15 08:14 Ligeance 阅读(186) 评论(0) 推荐(0) 编辑
摘要: 例如123455567123,变成123467123555。View Code static int[] MoveFirstDupNumToEnd(int[] intArr) { int[] result = new int[intArr.Length]; int dup = 0; int dupCount = 0; int x = 0; Hashtable ht = new Hashtable(intArr.Length); for... 阅读全文
posted @ 2012-11-09 16:14 Ligeance 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 例如:123456转换成 1 -> 2 -> 3-> 4-> 5-> 6View Code static LinkedList<int> CovertIntToLinkedList(int num) { Stack<int> stack = new Stack<int>(); LinkedList<int> result = new LinkedList<int>(); while (num!=0) { stack.Push(num % 10); ... 阅读全文
posted @ 2012-11-09 15:45 Ligeance 阅读(373) 评论(0) 推荐(0) 编辑