leetcode题解日练--2016.6.22

来源:互联网 发布:淘宝曼妮芬内衣怎么样 编辑:程序博客网 时间:2024/05/18 11:05

编程日记,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。

今日题目:1、对称二叉树;2、判定是否4的次方数;3、用栈实现队列;4、杨辉三角

101. Symmetric Tree | Difficulty: Easy

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
Bonus points if you could solve it both recursively and iteratively.
题意:给定一棵二叉树,检查它是否对称。
思路:
无论采用何种方式遍历,只要保证左右颠倒去遍历就可以了,一个队列存先访问左节点的情况,另一个队列存先访问右节点的情况,两相比较。
代码:
C++
迭代版本

class Solution {public:    bool isSymmetric(TreeNode* root) {        if(root==NULL) return true;        queue<TreeNode*> q1,q2;        TreeNode *left,*right;        q1.push(root->left);        q2.push(root->right);        while(!q1.empty() and !q2.empty())        {            left = q1.front();            q1.pop();            right = q2.front();            q2.pop();            //两边都是空            if(NULL==left && NULL==right)                continue;            //只有一边是空            if(NULL==left||NULL==right)                return false;             if (left->val != right->val)                return false;            q1.push(left->left);            q1.push(left->right);            q2.push(right->right);            q2.push(right->left);        }        return true;    }};

结果:4ms
递归版本

class Solution {public:    bool isSymmetrical(TreeNode* root1,TreeNode* root2)    {        if(NULL==root1&&NULL==root2)        return true;        if(NULL==root1||NULL==root2)        return false;        if(root1->val!=root2->val)            return false;        return (isSymmetrical(root1->left,root2->right)&&isSymmetrical(root1->right,root2->left));    }    bool isSymmetric(TreeNode* root) {        return isSymmetrical(root,root);    }};

结果:6ms

342. Power of Four | Difficulty: Easy

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.
题意:给一个int型的数,判断是否是4的平方
判断2的次方、3 的次方:
http://blog.csdn.net/sysu_cis/article/details/51723219

思路:
1、可以先观察一下有哪些4的平方数,从小到大列举一些,1,4,16,64,256,1028,4096……是不是能发现一些规律,没错,除了1之外其他的都是4、6结尾的,其次呢?我们在做2的平方的时候发现2的平方数的二进制表示都只有一个1,其实4的平方数也具有类似性质并且这个1还只能在偶数位置,0,2,4……这就好办了,我们只需要判断尾数是4或者6,然后做一次n&(n-1)的操作,如果等于0说明是4的平方数。
c++

class Solution {public:    bool isPowerOfFour(int num) {        if(num%10!=4 && num%10!=6)            return false;        else          return (num&(num-1)==0)&&num>0;    }};

结果:8ms

2、利用一个性质,4的次方数只有一个1并且模3等于1

class Solution {public:    bool isPowerOfFour(int num) {        return (num & (num - 1)) == 0 && num % 3 == 1;    }};

结果:4ms
3、既然是需要判断只有一个1并且在第偶数位(从0开始计算),那么只需要如下代码即可实现:

class Solution {public:    bool isPowerOfFour(int num) {         return (num > 0) && ((num & (num - 1)) == 0) && ((num & 0x55555555) == num);    }};

232. Implement Queue using Stacks | Difficulty: Easy

Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.

题意:使用栈来模拟队列的功能。
思路:很经典的一道题,基本思想就是用一个栈来模拟入队,一个栈来模拟出队。没什么好讲的。
代码:
C++

class Queue {public:    stack<int> s1,s2;    // Push element x to the back of queue.    void push(int x) {        s1.push(x);    }    // Removes the element from in front of queue.    void pop(void) {        peek();        s2.pop();    }    // Get the front element.    int peek(void) {        if(!s2.empty())            return s2.top();        else        {        while(s1.size())        {            int tmp = s1.top();            s1.pop();            s2.push(tmp);        }        }        if(s2.empty())            return 0;        else            return s2.top();    }    // Return whether the queue is empty.    bool empty(void) {        return s2.empty()&&s1.empty();    }};

结果:0ms

118. Pascal’s Triangle | Difficulty: Easy

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
题意:杨辉三角,给出一个数返回一个杨辉三角形式的数组。
思路:
首先将杨辉三角这样来看。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
找杨辉三角的规律,首先,两端元素都是1,第0行可以认为既是左边又是右边。
除了两端元素之外的其他每个元素等于它上面一个元素和上面左边元素。i表示行号,j表示列号。
r[i][j] = r[i-1][j-1]+r[i-1][j]
代码:

class Solution {public:    vector<vector<int> > generate(int numRows) {        vector<vector<int>> res(numRows);        for (int i = 0; i < numRows; i++) {            res[i].resize(i + 1);            for(int j=0;j<=i;j++)            {                if(j==0||j==i)                    res[i][j] = 1;                else                    res[i][j] = res[i-1][j-1]+res[i-1][j];            }        }        return res;    }};

结果:4ms

0 0