LeetCode 2015.7.13 9,21,104,6,191,198,204,225

本文解析了LeetCode上的多个经典算法题目,包括回文数判断、两有序链表合并、二叉树最大深度、Z字形字符串转换等,提供了详细的C++实现代码,并对部分代码进行了优化。
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();
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值