摘要:
二分查找。要注意越界问题。以及除数是否为0的情况 int sqrt(int x) { // Note: The Solution object is instantiated only once and is reused by each test case. if(xmid) left = mid+1; else return mid; } return right; //返回的是right } 阅读全文
摘要:
分两步走:1. 沿着反对角线做交换(i,j)--》(n-1-j,n-1-i)2. 沿着中间做交换 void rotate(vector > &matrix) { // Note: The Solution object is instantiated only once and is reused by each test case. if(matrix.empty()||matrix[0].empty()) return; int n = matrix.size(); for(int i=0;... 阅读全文
摘要:
注意条件。 vector spiralOrder(vector > &matrix) { // Note: The Solution object is instantiated only once and is reused by each test case. vector res; if(matrix.empty()||matrix[0].empty()) return res; int m = matrix.size(); int n = matrix[0].size(); ... 阅读全文
摘要:
注意一些边界条件 ListNode *rotateRight(ListNode *head, int k) { // Note: The Solution object is instantiated only once and is reused by each test case. if(head==NULL) return NULL; int len = getLength(head); k = k%len; if(k==0) return head; L... 阅读全文
摘要:
不要忘了考虑这种情况[3,1] 此时left == mid,A[left]==A[mid] int search(int A[], int n, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. int left = 0,right = n-1; while(leftA[mid]) //right part is sorted { if(A[m... 阅读全文
摘要:
vector > generateMatrix(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int start = 0; vector> res(n,vector(n)); if(n>& res) { int i; for(i=start;i=start;i--) res[end][i] = cnt++; ... 阅读全文
摘要:
注意一点:n为负数时, n = -n; 有可能会溢出,当n == INT_MIN时 double pow(double x, int n) { // Note: The Solution object is instantiated only once and is reused by each test case. if(equal(x,0.0)&&n>1); if((n&0x1)==0) return res*res; else return res*res*x; } ... 阅读全文