摘要:
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 ... 阅读全文
摘要:
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 . 阅读全文
摘要:
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 阅读全文
摘要:
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... 阅读全文