摘要:
注意一点: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; } ... 阅读全文
摘要:
int maxSubArray(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. if(ncurMax) curMax = sum; } return curMax; } 阅读全文
摘要:
#include using namespace std;int selectGasStation(const vector &a, const vector &g) { int total = 0; int sum = 0; int n = g.size(); int index = -1; for(int i=0;i=0?index+1:-1);} 阅读全文
摘要:
DP.O(n) solution.对于每个bar,找出其左边与右边的最高高度, 可以hold的water =min(max_left, max_right) - height int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int maxHeight = 0; vector left(n); for(int i=0;i right(n); for... 阅读全文
摘要:
int canCompleteCircuit(vector &gas, vector &cost) { // Note: The Solution object is instantiated only once and is reused by each test case. int tank = 0; int total = 0; int n = gas.size(); int index = -1; for(int i=0;i=0?index+1:-1); }... 阅读全文
摘要:
int firstMissingPositive(int A[], int n) { for(int i=0;i0&&A[i]<=n) { if(A[i]-1!=i&&A[A[i]-1]!=A[i]) { int tmp = A[A[i]-1]; A[A[i]-1] = A[i]; A[i] = tmp; i--; } } } for(int i=0;i<n... 阅读全文
摘要:
void swap(int* a,int* b){ int tmp = *a; *a = *b; *b = tmp;}void reverse(vector &num,int i,int j){ while(i &num) { // Start typing your C/C++ solution below // DO NOT write int main() function if(num.empty()) return; int i,j; for(i=num.size()-... 阅读全文
摘要:
bool search(int A[], int n, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. int begin = 0,end = n-1; while(beginA[mid]) //right part is sorted { if(A[mid]<target&&target<=A[end]) ... 阅读全文
摘要:
vector > combinationSum(vector &candidates, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. sort(candidates.begin(),candidates.end()); vector> res; vector comb; dfs(0,candidates,target,comb,res); retu... 阅读全文
摘要:
回溯。返回所有的全排列 vector > permute(vector &num) { // Note: The Solution object is instantiated only once and is reused by each test case. vector> res; vector tmp; vector visited(num.size(),false); dfs(0,num,visited,tmp,res); return res; } void dfs(int... 阅读全文