摘要: Implement Pow(x, n) 1 public static double Pow(double x, int n) 2 { 3 bool is_neg = n < 0; 4 5 //Because Math.Abs(int.MinValue) not exist. 6 if (n == int.MinValue) 7 { 8 return 1 / Pow(x, int.MaxValue) * x; 9 ... 阅读全文
posted @ 2012-10-10 05:14 ETCOW 阅读(390) 评论(0) 推荐(0) 编辑
摘要: Given a number represented as an array of digits, plus one to the number. 1 public static List<int> PlusOne(List<int> digits) 2 { 3 int carry = 1; 4 for (int i = digits.Count - 1; i >= 0; i--) 5 { 6 digits[i] = digits[i] + carry; 7 ... 阅读全文
posted @ 2012-10-10 04:47 ETCOW 阅读(233) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 1 public static List<List<int>> PermutationsII(List<int> num) 2 { 3 List<List<int... 阅读全文
posted @ 2012-10-10 04:41 ETCOW 阅读(273) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 1 public static List<List<int>> Permutation(List<int> num) 2 { 3 List<List<int>> ret = new List& 阅读全文
posted @ 2012-10-05 22:11 ETCOW 阅读(573) 评论(0) 推荐(0) 编辑
摘要: The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321"Given n and k, return the kth permutation se 阅读全文
posted @ 2012-10-05 04:39 ETCOW 阅读(262) 评论(0) 推荐(0) 编辑
摘要: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2-> 阅读全文
posted @ 2012-10-05 04:08 ETCOW 阅读(431) 评论(0) 推荐(0) 编辑
摘要: Determine whether an integer is a palindrome. Do this without extra space. 1 public static bool PalindromeNumberRec(int x) 2 { 3 return isPalindrome(x, ref x); 4 } 5 6 public static bool isPalindrome(int x, ref int y) 7 { 8 if(x < 0) r... 阅读全文
posted @ 2012-10-02 22:45 ETCOW 阅读(300) 评论(0) 推荐(0) 编辑
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2012-10-02 05:42 ETCOW 阅读(659) 评论(0) 推荐(1) 编辑
摘要: Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. 1 public static int NQueenII(int n) 2 { 3 int[] map = new int[n]; 4 int ret = NQueenIIHelper(map, n, 0); 5 return ret; 6 ... 阅读全文
posted @ 2012-10-02 03:40 ETCOW 阅读(231) 评论(0) 推荐(0) 编辑
摘要: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' 阅读全文
posted @ 2012-10-02 01:50 ETCOW 阅读(547) 评论(0) 推荐(0) 编辑