上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 18 下一页
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.依然是双指针思想 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Start ... 阅读全文
posted @ 2012-11-14 15:32 chkkch 阅读(4423) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].接上题,增加一个count变量来记录key出现的次数。 1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 . 阅读全文
posted @ 2012-11-14 15:28 chkkch 阅读(1333) 评论(0) 推荐(1) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2012-11-14 15:20 chkkch 阅读(5249) 评论(0) 推荐(1) 编辑
摘要: Implement pow(x,n).用二分法,O(logn)。注意n < 0的处理 1 class Solution { 2 public: 3 double power(double x, int n) 4 { 5 if (n == 0) 6 return 1; 7 8 double v = power(x, n / 2); 9 10 if (n % 2 == 0)11 return v * v;12 else13... 阅读全文
posted @ 2012-11-14 15:12 chkkch 阅读(2229) 评论(0) 推荐(0) 编辑
摘要: Given a number represented as an array of digits, plus one to the number.进位和加法。 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 vector<int> ret(digits); 7 ... 阅读全文
posted @ 2012-11-13 21:35 chkkch 阅读(1855) 评论(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].先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否和自己相等,相等的时候则前面的数必须使用了,自己才能使用,这样就不会产生重复的排列了。 1 class Solution { 2 private: 3 b... 阅读全文
posted @ 2012-11-13 21:22 chkkch 阅读(4190) 评论(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].DFS 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 bool canUse[100]; 5 int a[100]; 6 public: 7 void dfs(... 阅读全文
posted @ 2012-11-13 13:06 chkkch 阅读(637) 评论(0) 推荐(0) 编辑
摘要: The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文
posted @ 2012-11-13 12:17 chkkch 阅读(2007) 评论(1) 推荐(0) 编辑
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
posted @ 2012-11-13 12:06 chkkch 阅读(2924) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2012-11-13 11:57 chkkch 阅读(5238) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 18 下一页