LeetCode 2015.7.13 9,21,104,6,191,198,204,225
9 Palindrome Number
class Solution {
public:
bool isPalindrome(int x) {
if (x<0) return false;
int div = 1;
while (x/div>=10) div*=10;
while (x>0)
{
int l = x / div;
int r = x % 10;
if (l!=r) return false;
x = x % div;
x /=10;
div/=100;
}
return true;
}
};
21 Merge Two Sorted Lists
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode tmpnode(0);
ListNode* head = &tmpnode;
ListNode* terminal = head;
ListNode* list1 = l1;
ListNode* list2 = l2;
while ((list1!= NULL )&&(list2!=NULL))
{
if (list1->val < list2->val)
{
head->next=list1;
list1=list1->next;
}
else
{
head->next=list2;
list2=list2->next;
}
head=head->next;
}
/*冗余开始,因为是链表,不用再一个一个复制了。只要让head指向list1或者list2的头就行了*/
while (list1!=NULL)
{
head->next=list1;
head=head->next;
list1=list1->next;
}
while (list2!=NULL)
{
head->next=list2;
head=head->next;
list2=list2->next;
}
/*冗余结束*/
return terminal->next;
}
};
改掉冗余:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode tmpnode(0);
ListNode* head = &tmpnode;
ListNode* terminal = head;
ListNode* list1 = l1;
ListNode* list2 = l2;
while ((list1!= NULL )&&(list2!=NULL))
{
if (list1->val < list2->val)
{
head->next=list1;
list1=list1->next;
}
else
{
head->next=list2;
list2=list2->next;
}
head=head->next;
}
/*冗余修改开始*/
if (list1!=NULL)
head->next=list1;
if (list2!=NULL)
head->next=list2;
/*冗余修改结束*/
return terminal->next;
}
};
104 Maximum Depth of Binary Tree
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root==NULL) return 0;
maxdep(root,1);
return maxval;
}
int maxval=-1;
void maxdep(TreeNode* root,int depth)
{
if (root->left== NULL && root->right== NULL)
{
if (depth>maxval) maxval=depth;
return ;
}
depth++;
if (root->left!=NULL)
maxdep(root->left,depth);
if (root->right!=NULL)
maxdep(root->right,depth);
depth--;
}
};
看过Solution,修改的简洁代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
6 ZigZag Conversion
class Solution {
public:
string convert(string s, int numRows) {
if (numRows==1) return s;
string ans;
ans.clear();
int len=s.size();
int sum = (numRows-1)*2;
int i=0;
while (i<len)
{
ans=ans+s[i];
i+=sum;
}
int k=0,j;
for(i=sum-2;i>=2;i=i-2)
{
k++;j=k;
while (j<len)
{
ans=ans+s[j];
if (j+i<len)
{
ans=ans+s[j+i];
}
j=j+sum;
};
}
i=numRows-1;
while (i<len)
{
ans=ans+s[i];
i+=sum;
}
return ans;
}
};
191 Number of 1 Bits
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
while (n!=0)
{
int tmp = n % 2;
if (tmp==1) cnt++;
n/=2;
}
return cnt;
}
};
198 House Robber
class Solution {
public:
int rob(vector<int>& nums) {
int len=nums.size();
if (len==0) return 0;
vector<int>f;
f.clear();
int ans=-INT_MAX;
for(int i=0;i<len;i++)
{
int tmpmax = -1;
for(int j=0;j<=i-2;j++)
if (f[j]>tmpmax) tmpmax=f[j];
if (tmpmax==-1) f.push_back(nums[i]);
else f.push_back(nums[i]+tmpmax);
if (f[i]>ans) ans=f[i];
}
return ans;
}
};
204 Count Primes
class Solution {
public:
int countPrimes(int n) {
bool isPrime[n];
for(int i=1;i<=n;i++)
isPrime[i]=true;
int sqrtn=(int) sqrt(n);
for(int p=2;p<=sqrtn;p++)
{
if (isPrime[p])
{
int j=p*p;
while (j<=n)
{
isPrime[j]=false;
j+=p;
}
}
}
int cnt=0;
for(int i=2;i<n;i++)
if (isPrime[i]) cnt++;
return cnt;
}
};
225 Implement Stack using Queues
class Stack {
private:
queue<int> Q[2];
int flag;
public:
// Push element x onto stack.
void push(int x) {
Q[flag].push(x);
}
// Removes the element on top of the stack.
void pop() {
int tmpflag=1-flag;
while (Q[flag].size()>1)
{
Q[tmpflag].push(Q[flag].front());
Q[flag].pop();
}
Q[flag].pop();
flag=tmpflag;
}
// Get the top element.
int top() {
return (Q[flag].back());
}
// Return whether the stack is empty.
bool empty() {
if (Q[flag].empty()) return true; else return false;
}
Stack()
{
flag = 0;
while (!Q[1].empty()) Q[1].pop();
while (!Q[0].empty()) Q[0].pop();
}
};