leetcode题解日练--2016.6.22

2024-03-06 10:48
文章标签 leetcode 题解 22 日练 2016.6

本文主要是介绍leetcode题解日练--2016.6.22,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编程日记,尽量保证每天至少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;elsereturn (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;elsereturn 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;elseres[i][j] = res[i-1][j-1]+res[i-1][j];}}return res;}
};

结果:4ms

这篇关于leetcode题解日练--2016.6.22的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779817

相关文章

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述