C++
文章平均质量分 52
yangfang2111
这个作者很懒,什么都没留下…
展开
-
给定数组删除多余允许的相同的数
#include#include#includeusing namespace std;//int removeDuplicates(int A[],int n,int count)//{// if(A==NULL||n// return -1;// sort(A,A+n);// for(int i=0;i// cout// cout// int i原创 2015-06-15 22:21:22 · 354 阅读 · 0 评论 -
二分法pow和sqrt
#includeusing namespace std;int sqrt(int x){if(xreturn 1;int left=1;int right=x/2;//int last_mid;while(left{int mid=left+(right-left)/2;if(x/mid>mid){left=mid+1;// last_m原创 2015-11-06 22:37:09 · 467 阅读 · 0 评论 -
wordSearch
#include#include#includeusing namespace std;bool dfs(vector>&board,string&word,int index,int x,int y,vector>&visited){if(index==word.size())return true;if(x=board.size()||y>=board[0].s原创 2015-11-06 17:05:45 · 296 阅读 · 0 评论 -
next_permutation
#include#include#includeusing namespace std;int main(){vectornum;num.push_back(1);num.push_back(2);num.push_back(3);do{for(auto i:num)coutcout}while(next_permutation(num.原创 2015-09-19 14:05:28 · 278 阅读 · 0 评论 -
wordLadder
#include#include#include#include#includeusing namespace std;int ladderLength(const string& start,const string& end,const unordered_set& dict){queuecur,next;unordered_setvisited;aut原创 2015-09-20 20:49:34 · 356 阅读 · 0 评论 -
longestSubstring
#include#include#include#includeusing namespace std;int lengthOfLongestSubsring(string s){const int ascII = 26;int last[ascII] = { -1 };int max_len = 0;int start = 0;for (int i =原创 2015-11-10 22:39:41 · 394 阅读 · 0 评论 -
canJump&&jumpII
#include#include#includeusing namespace std;bool canJump(int a[], int n){int reach = 1;for (int i = 0;i {reach = max(reach, i + 1 + a[i]);}return reach >= n;}bool canJump1(in原创 2015-11-10 16:20:23 · 301 阅读 · 0 评论 -
container with most water
#include#include#includeusing namespace std;int maxArea(vector&height){int left = 0;int right = height.size() - 1;int result = 0;while (left {int area = min(height[left], height[原创 2015-11-11 16:11:46 · 258 阅读 · 0 评论 -
minCut
#include#include#include#includeusing namespace std;int minCut(string s){const int n = s.size();vectorf(n+1);bool p[100][100];fill_n(&p[0][0], n*n, false);for (int i = 0;i f[i]原创 2015-11-21 15:35:34 · 339 阅读 · 0 评论 -
triangle
#include#include#includeusing namespace std;int minimumTotal(vector>&triangle){for(int i=triangle.size()-2;i>=0;--i){for(int j=0;jtriangle[i][j]+=min(triangle[i+1][j],triangle[i+1][j原创 2015-11-20 22:23:02 · 295 阅读 · 0 评论 -
maxProfit
#include#include#includeusing namespace std;int maxProfit(vector&prices){const int n=prices.size();if(nreturn 0;vectorf(n,0);vectorg(n,0);for(int i=1,valley=prices[0];i{valle原创 2015-11-22 21:59:04 · 749 阅读 · 0 评论 -
isInterleave
#include#include#include#includeusing namespace std;bool isInterleave(string& s1,string& s2,string& s3){vector>f(s1.size()+1,vector(s2.size()+1,true));for(int i=1;if[i][0]=f[i-1][0]&原创 2015-11-23 00:12:53 · 459 阅读 · 0 评论 -
maximalRectangle
#include#include#includeusing namespace std;int maximalRectangle(vector>&matrix){const int m=matrix.size();const int n=matrix[0].size();vectorh(n,0);vectorl(n,0);vectorr(n,n);int原创 2015-11-22 17:00:35 · 357 阅读 · 0 评论 -
增量法subsets
#include#include#include#includeusing namespace std;vector>subsets(vector&s){sort(s.begin(),s.end());vector>result(1);int i=1;for(auto elem:s){result.reserve(2*result.size());原创 2015-09-17 00:09:06 · 407 阅读 · 0 评论 -
generateParenthesses
#include#include#includeusing namespace std;void generate(int n,string s,int l,int r,vector&result){if(l==n){s.append(n-r,')');result.push_back(s);return ;}generate(n,s+"(",l+1原创 2015-11-05 20:12:29 · 270 阅读 · 0 评论 -
combinationSum&&combinationSumII
#include#include#include#includeusing namespace std;void dfs(vector&nums,int gap,int start,vector&intermediate,vector>&result){if(gap==0){result.push_back(intermediate);return;}原创 2015-11-05 13:29:24 · 256 阅读 · 0 评论 -
leetcode 最长回文串
string preProcess(string s){int n=s.size();if(n==0)return string("^$");string ret("^");for(int i=0;i{ret+="#"+s.substr(i,1);}ret+="#$";return ret;}string longestPalindrome(st转载 2015-07-28 21:22:57 · 270 阅读 · 0 评论 -
leetcode wildcard matching
#includeusing namespace std;bool isMatch(const char *s,const char *p){const char *star=nullptr;const char *rs=nullptr;while(*s!='\0'){if(*s==*p||*p=='?'){++s;++p;continue;}原创 2015-08-01 10:09:38 · 225 阅读 · 0 评论 -
二叉树非递归遍历
#include#include#includeusing namespace std;struct TreeNode{int value;TreeNode *left;TreeNode *right;};vector preOrder(TreeNode *root){stack s;vector result;TreeNode* p;i原创 2015-08-16 11:30:53 · 264 阅读 · 0 评论 -
leetcode intToRoman
#include#include#includeusing namespace std;string intToRoman(int num){if(num>3999||numreturn string("the num is out of range");unordered_map map;map[1000]="M";map[900]="CM";map[原创 2015-08-05 12:27:04 · 254 阅读 · 0 评论 -
leetcode romanToInt
#include#include#includeusing namespace std;string intToRoman(int num){if(num>3999||numreturn string("the num is out of range");unordered_map map;map[1000]="M";map[900]="CM";map[原创 2015-08-05 13:33:20 · 351 阅读 · 0 评论 -
leetcode countAndSay
#include#include#include#includeusing namespace std;string getNext(const string &s){stringstream ss;for(auto i=s.begin();i!=s.end();){auto j=find_if(i,s.end(),bind1st(not_equal_to(原创 2015-08-05 16:52:14 · 269 阅读 · 0 评论 -
sum root to leaf
int sum(TreeNode* root,int sum){if(root==nullptr)return 0;if(root->left==nullptr&&root->right==nullptr)return sum*10+root->val;return sum(root->left,sum*10+root->val)+sum(root->right,sum*1原创 2015-09-09 21:30:51 · 255 阅读 · 0 评论 -
populate next right pointer in each node
void populate(TreeLinkNode* root){while(root){TreeLinkNode* pre=nullptr;TreeLinkNode* next=nullptr;for(;root;root=root->next){if(!next)next=root->left?root->left:root->right;if(roo原创 2015-09-09 21:42:13 · 292 阅读 · 0 评论 -
maxPathSum
int maxPathSum(TreeNode* root){int maxSum=INT_MIN;dfs(root,maxSum);return maxSum;}int dfs(TreeNode* root,int&maxSum){if(root==nullptr)return 0;int l=dfs(root->left,maxSum);int r=原创 2015-09-09 22:02:59 · 349 阅读 · 0 评论 -
findFirstMissingPositive
void bucket_sort(int A[],int n){for(size_t i=0;i{while(A[i]!=i+1){if(A[i]n||A[i]==A[A[i]-1])break;swap(A[i],A[A[i]-1]);}}}int findFirstMissingPositive(int A[],int n){bucket原创 2015-09-13 20:43:22 · 234 阅读 · 0 评论 -
searchRange
#include#include#includeusing namespace std;vector searchRange(int A[],int n,int target){int l=distance(A,lower_bound(A,A+n,target));int u=distance(A,prev(upper_bound(A,A+n,target)));i原创 2015-09-13 22:52:12 · 277 阅读 · 0 评论 -
storeIpAddresses
#include#include#includeusing namespace std;void dfs(string s,size_t start,size_t step,string&ip,vector& result){if(start==s.size()&&step==4){ip.resize(ip.size()-1);result.push_back(原创 2015-11-05 12:47:24 · 329 阅读 · 0 评论 -
maxSubArray
#include#includeusing namespace std;int maxSubArray(int A[],int n){int result=INT_MIN,f=0;for(int i=0;i{f=max(f+A[i],A[i]);result=max(f,result);}return result;}int main(){原创 2015-11-20 23:02:07 · 346 阅读 · 0 评论