Commit 3e509ac5 by yaozc

### keep going

parent 6c8075fe
 ... ... @@ -1168,7 +1168,7 @@ Median of Two Sorted Arrays //https://www.youtube.com/watch?v=LPFhl65R7ww&t=428s //解： 找出两个array中的两个index，将两个array分为4部分，称之为 x1, x2, y1, y2 //两个index使得： len(x1) + len(y1) == len(x2) + len(y2) // max(x1) <= min(y2), min(x2) >= max(y1); // max(x1) <= min(y2), max(y1) <= min(x2); //array1的x的index称为partition_x, array2的称为partition_y //只需要找到xpartition_x。因为 partition_y = (len(array1) + len(array2) + 1) / 2 //这题太难了，不想修代码了，这段跑不起来的 ... ... @@ -3001,9 +3001,189 @@ public: Populating Next Right Pointers in Each Node II //本质为bfs，简单！ /* // Definition for a Node. class Node { public: int val; Node* left; Node* right; Node* next; Node() {} Node(int _val, Node* _left, Node* _right, Node* _next) { val = _val; left = _left; right = _right; next = _next; } }; */ class Solution { public: Node* connect(Node* root) { if(!root) return root; Node* prev; Node* curr; queue q; q.push(root); while(!q.empty()){ int q_size = q.size(); for(int i=0; ileft) q.push(curr->left); if(curr->right) q.push(curr->right); if(prev) prev->next = curr; prev = curr; } prev = NULL; } return root; } }; Find Minimum in Rotated Sorted Array //用了binary search，其实不是很理解。。。 //比如 if(nums[mid] >= nums[left]) 为什么是 >= // left = mid+1 为什么要加1 class Solution { public: int findMin(vector& nums) { if(nums.size() == 1) return nums[0]; int left = 0; int right = nums.size()-1; while(left < right){ if(nums[right] > nums[left]) return nums[left]; int mid = left + (right-left)/2; if(nums[mid] >= nums[left]){ left = mid+1; } else{ right = mid; } } return nums[left]; } }; Search a 2D Matrix II //woc这题太聪明了 //非常好理解，从最右上开始（选择右上或者左下皆可） //因为右上可以选择下左移动 //要是当前大于target就向左 //要是小于就向下 //complexity为O(m+n) class Solution { public: bool searchMatrix(vector>& matrix, int target) { if(matrix.empty()) return false; int row = 0; int col = matrix[0].size()-1; while(row=0){ if(matrix[row][col] == target) return true; else if(matrix[row][col] < target){ row++; } else col--; } return false; } }; Binary Search //不是某个公司的面试题but自己写着练习一下 //一直不理解为什么 left = mid+1和 right = mid-1,测试后发现若不+-1，则会在找不到target的情况下出现死循环 //left 或者right一直等于mid的值，而mid的值又得不到跟新 class Solution { public: int search(vector& nums, int target) { if(nums.size()<2){ if(nums.empty()) return -1; if(nums.size()==1){ if(nums[0] == target) return 0; return -1; } } int left = 0; int right = nums.size()-1; while(left<=right){ int mid = left + (right-left)/2; if(nums[mid] < target){ left = mid+1; } else if(nums[mid] > target){ right = mid-1; } else return mid; } return -1; } }; Search a 2D Matrix // treat it as a binary search in a sorted vector!!! //本质上来说和单个array的binary search没什么区别，就是计算inner和outer vector的index麻烦了一点 class Solution { public: bool searchMatrix(vector>& matrix, int target) { if(matrix.empty()) return false; int start = 0; int end = matrix.size() * matrix[0].size()-1; while(start <= end) { int mid = (start + end)/2; //注意outer和inner的计算方法，都是除以inner vector的size int outer = mid / matrix[0].size(); int inner = mid % matrix[0].size(); if(matrix[outer][inner] == target) return true; else if(matrix[outer][inner] < target){ start = mid+1; } else end = mid-1; } return false; } }; Single Number // ^ 为XOR，这里使用了xor的性质，即 a ^ a = 0, a ^ 0 = a class Solution { public: int singleNumber(vector& nums) { int sum = 0; for(auto &k:nums) sum ^= k; return sum; } }; ... ... @@ -3017,7 +3197,6 @@ public: // end ... ... @@ -3035,4 +3214,4 @@ public: // \ No newline at end of file // end \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!