【LeetCode】Agorithms 题集(一)
Single Number
题目
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路:
为了满足时间和空间复杂度,必须利用异或的性质。
异或: 1 XOR 1 = 0 0 XOR 0 = 0 1 XOR 0 = 1 0 XOR 1 = 1 即同样为 0。不同为1
依据异或性质,有例如以下等式: 对随意整数。a b c , a XOR a = 0 a XOR b XOR a = b
即随意两个同样的数异或一定得 0, 若是一堆数,除了一个数,其它都是成对出现,则将全部数异或起来,剩下的就是我们要找的数。
复杂度为 O(n)
代码:
class Solution{
public:
int singleNumber(int A[], int n) {
int ans;
for(int i = 0; i < n;++i)
ans ^= A[i];
return ans;
}
};
Maximum Depth of Binary Tree
题目
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
思路
简单递归的考查,求一棵树的深度。仅仅要在左子树和右子树中取最大高度加 1 就是根的高度,递归下去即可。
代码
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL) return 0; int ans = 1;
int l = maxDepth(root->left);
int r = maxDepth(root->right); ans += max(l,r); return ans;
}
};
Same Tree
题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
思路:
考察递归。
推断两棵树相等,仅仅要递归推断两棵树的结构和值。所以遇到一个指针为空的时候,还有一个指针一定要为空。不为空的时候,两个指针的值必须相等。
再递归左右子树是否相等。
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
bool flag = true; /* 当中一个为空,则肯定结束 */
if(p == NULL || q == NULL)
{
/* 两个都为空才是相等的 */
if(p == NULL && q == NULL)
return true;
return false;
} /* 两个节点的值不等则 false */
if(p->val != q->val) return false; /* 递归推断左子树 */
flag = flag & isSameTree(p->left,q->left); /* 递归推断右子树 */
flag = flag & isSameTree(p->right,q->right); return flag;
}
};
Reverse Integer
题意:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:
把整数倒转。非常easy。仅仅要先推断是否负数,存起来。
之后取绝对值,把绝对值倒转后再决定是否是负数。
代码:
class Solution {
public:
int reverse(int x) {
bool neg = (x < 0); x = abs(x); int ans = 0; while(x)
{
int t = x%10;
ans = ans*10 + t;
x = x/10;
} if(neg) ans = -ans; return ans;
}
};
Binary Tree Preorder Traversal
题意:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
思路:
写个非递归的前序遍历,用 stack.
代码:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> ans;
stack<TreeNode *> s;
TreeNode *p = root;
while(p != NULL || !s.empty())
{
while(p != NULL)
{
ans.push_back(p->val);
s.push(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
p = p->right;
}
}
return ans;
}
};
【LeetCode】Agorithms 题集(一)的更多相关文章
- LeetCode算法题-Subdomain Visit Count(Java实现)
这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...
- LeetCode算法题-Number of Lines To Write String(Java实现)
这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...
- LeetCode算法题-Unique Morse Code Words(Java实现)
这是悦乐书的第318次更新,第339篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是804).国际莫尔斯电码定义了一种标准编码,其中每个字母映射到一系 ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotated Digits(Java实现)
这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...
- LeetCode算法题-Letter Case Permutation(Java实现)
这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- LeetCode算法题-Jewels and Stones(Java实现)
这是悦乐书的第313次更新,第334篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第182题(顺位题号是771).字符串J代表珠宝,S代表你拥有的石头.S中的每个字符都是 ...
- LeetCode算法题-Toeplitz Matrix(Java实现)
这是悦乐书的第312次更新,第333篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第181题(顺位题号是766).如果从左上角到右下角的每个对角线具有相同的元素,则矩阵是 ...
随机推荐
- POJ 3308 Paratroopers(最小割EK)
题目链接 题意 : 有一个n*m的矩阵,L个伞兵可能落在某些点上,这些点的坐标已知,需要在某些位置安上一些枪,然后每个枪可以将一行或者一列的伞兵击毙.把这种枪安装到不同行的行首.或者不同列的列首,费用 ...
- 如何设置EditPlus的默认编码utf-8方式
最近在使用英文版的Editplus写代码的时候,发现中文字符在调试过程中都变成了乱码,发现是Editplus编码设置的问题,Editplus默认编码可能是ANSI,需要将其改成UTF-8.干脆给设置默 ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
- easyui源码翻译1.32--accordion(手风琴)
前言 前几天加班比较忙 未能及时更新翻译的 今天多发布几篇..下载该插件翻译源码 Accordion 分类空间允许用户使用多面板,但在同一时间只会显示一个.每个面板都内建支持展开和折叠功能.点击一个面 ...
- QAbstractItemView为截断的项显示ToolTip(在eventFilter函数里覆盖QEvent::ToolTip事件)
在Qt中想要为QAbstractItemView中长度不够而使得内容被截断的项显示ToolTip,Qt官网有一篇文章介绍使用事件过滤器来显示太长的项,但是没有涵盖图标的情况.显示列头项太长的情况等等, ...
- QString内部仍采用UTF-16存储数据且不会改变(一共10种不同情况下的编码)
出处:https://blog.qt.io/cn/2012/05/16/source-code-must-be-utf-8-and-qstring-wants-it/ 但是注意,这只是QT运行(Run ...
- 一个灵巧的Delphi多播实事件现方案
一个灵巧的Delphi多播实现方案.必须是支持泛型的Delphi版本.也就是Delphi2009以后.强烈建议用DelphiXE. 用法就是例如写一个Class指定一个Event,触发的时候会通知多个 ...
- Show username instead of "System Account" in SharePoint 2010
Problems: When I load my local SharePoint site, the account always show as "System Account" ...
- Android开发之bindService()侦听service内部状态
在Android开发之bindService()通信的基础上,实现bindService()方法侦听service内部状态. 实现侦听service内部状态,使用的是回调机制 1.首先实现一个接口 p ...
- jQuery功能一览
// Hello world!"); }); $("#btn5").click(function(){ $("#test5").val("D ...