205 Isomorphic Strings
class Solution {
public:
bool isIsomorphic(string s, string t) {
int len=s.size();
map<int,int> maps,mapt;
map<int,int>::iterator ps,pt;
maps.clear();
mapt.clear();
for(int i=0;i<len;i++)
{
int si=s[i]-'a';
int ti=t[i]-'a';
ps=maps.find(si);
pt=mapt.find(ti);
if (ps!=maps.end())
{
if(ps->second != ti) return false;
}
else maps.insert(pair<int,int>(si,ti));
if (pt!=mapt.end())
{
if(pt->second != si) return false;
}
else mapt.insert(pair<int,int>(ti,si));
}
return true;
}
};
206 Reverse Linked List
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL)
return head;
ListNode* p=head->next;
ListNode* q=head;
while (p!=NULL)
{
q->next=p->next;
p->next=head;
head=p;
p=q->next;
}
return head;
}
};
217 Contains Duplicate
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
int len=nums.size();
map<int,int> same;
map<int,int>::iterator p;
same.clear();
for(int i=0;i<len;i++)
{
p=same.find(nums[i]);
if (p!=same.end())
{
return true;
}
same.insert(pair<int,int>(nums[i],i));
}
return false;
}
};
219 Contains Duplicate II
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
int len=nums.size();
map<int,int> same;
map<int,int>::iterator p;
same.clear();
for(int i=0;i<len;i++)
{
p=same.find(nums[i]);
if (p!=same.end())
{
int dis=i - p->second ;
if (dis<=k) return true;
same.erase(p);
}
same.insert(pair<int,int>(nums[i],i));
}
return false;
}
};
223 Rectangle Area
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int cover;
if (A>E)
{
int t1=A,t2=B,t3=C,t4=D;
A=E;B=F;C=G;D=H;
E=t1;F=t2;G=t3;H=t4;
}
if (A<=E && B<=F && G<=C && H<=D) cover = (G-E)*(H-F);
else
if ((C<=E)||(G<=A)) cover =0;
else
if ((B>=H)||(F>=D)) cover =0;
else
{
int y[4]={B,D,F,H};
int x[4]={A,C,E,G};
sort(y,y+4);
sort(x,x+4);
cover =abs((x[1]-x[2])*(y[1]-y[2]));
}
// cout<<"cover"<<cover<<endl;
return (abs((A-C)*(B-D))+abs((E-G)*(F-H))-cover);
}
};
228 Summary Ranges
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> ans;
ans.clear();
if (nums.size()==0) return ans;
int left=0,right=0;
int len=nums.size();
nums.push_back(nums[len-1]-1);
for(int i=1;i<nums.size();i++)
{
if (nums[i]==nums[i-1]+1)
{
right=i;
continue;
}
if (left==right)
{
stringstream strStream;
strStream << nums[left];
string s = strStream.str();
ans.push_back(s);
}
else
{
stringstream strStream;
strStream<<nums[left]<<"->"<<nums[right];
string s =strStream.str();
ans.push_back(s);
}
left=i;right=i;
}
return ans;
}
};
231 Power of Two
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n==0) return false;
if (n==1) return true;
int x=n;
while (x%2==0)
x=x/2;
if (x==1) return true;
else return false;
}
};
111 Minimum Depth of Binary Tree
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> q;
queue<int> depth;
while (!q.empty()) q.pop();
while (!depth.empty()) depth.pop();
if (root==NULL)return 0;
q.push(root);
depth.push(0);
for(;;)
{
TreeNode* tmp=q.front();
q.pop();
int tmpdep = depth.front();
depth.pop();
if (tmp->left == NULL && tmp->right == NULL)
{
return tmpdep+1;
}
if (tmp->left!=NULL)
{
q.push(tmp->left);
depth.push(tmpdep+1);
}
if (tmp->right!=NULL)
{
q.push(tmp->right);
depth.push(tmpdep+1);
}
}
}
};
112 Path Sum
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (root==NULL) return false;
return search(root,0,sum);
}
bool search(TreeNode* root, int tmp, int sum)
{
tmp+=root->val;
if (root->left!=NULL)
{
if (search(root->left,tmp,sum)) return true;
}
if (root->right!=NULL)
{
if (search(root->right,tmp,sum)) return true;
}
if (root->left==NULL && root->right==NULL)
if (tmp==sum) return true;
tmp-=root->val;
return false;
}
};
LeetCode 2015.7.7 205,206,217,219,223,228,231,111,112
最新推荐文章于 2021-06-09 11:26:25 发布