Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下。
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. int addDigits(int num) {
char strint[] = {};
sprintf(strint, "%d", num);
int i=,a = ;
while(strint[i+]!= '\0')
{
a = (strint[i]-'')+(strint[i+]-'');
if(a>)
{
a = a%+a/;
}
strint[++i] = a+'';
}
return strint[i]-'';
}
上边这是O(n)的复杂度。网上还有O(1)的复杂度的:return (num - 1) % 9 + 1;
104. Maximum Depth of Binary Tree
求二叉树的深度,可以用递归也可以用循环,如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root) {
int left = ,right = ;
if (root == NULL) return ; left = maxDepth(root->left);
right = maxDepth(root->right); return left>right? left+:right+;
}
******************************************************************************************************************
用队列存结点,记录每一层的结点数levelCount,每出一个结点levelCount--,如果减为0说明要进入下一层,然后depth++,同时队列此时的结点数就是下一层的结点数。
/**
* Definition for a binary tree node.
* 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 ; //深度和每一层节点数
int depth = ,levelCount = ;
//队列保存每一层的节点数
queue<TreeNode*> node; node.push(root);
while(!node.empty())
{
//依次遍历每一个结点
TreeNode *p = node.front();
node.pop();
levelCount--; if(p->left) node.push(p->left);
if(p->right) node.push(p->right); if (levelCount == )
{
//保存下一层节点数,深度加1
depth++;
levelCount = node.size();
}
}
return depth;
}
};
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
[5, 7, 7, 8, 8, 10] and target value 8,[3, 4] vector<int> searchRange(vector<int>& nums, int target) {
int size = nums.size();
int l=,r=size-;
while(l<=r)
{
int mid = (l+r)/;
if (nums[mid] >= target)
{
r = mid-;
}
else if(nums[mid] < target)
{
l = mid+;
}
}
int left = l;
l = ;
r = size-;
while(l<=r)
{
int mid = (l+r)/;
if (nums[mid] <= target)
{
l = mid+;
}
else if(nums[mid] > target)
{
r = mid-;
}
}
int right = r;
vector<int> v;
v.push_back(left);
v.push_back(right);
if(nums[left] != target || nums[right] != target)
{
v[] = -;
v[] = -;
}
return v;
}
这个题就是要把握好边界,比如找左边界时 =号要给右边界的判断,找右边界则相反。这样才能保证不遗漏
int singleNumber(vector<int>& nums) {
int result=;
int len = nums.size();
if (len <=) return ;
for(int i=;i<len;i++)
{
result ^= nums[i];
}
return result;
}
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
char findTheDifference(string s, string t) {
map<char, int> sumChar;
char res;
for(auto ch: s) sumChar[ch]++;
for(auto ch: t)
{
if(--sumChar[ch]<)
res = ch;
}
return res;
}
这道题也可以用异或 因为相同的会异或掉,剩下一个就是多余的char。
Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference的更多相关文章
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Maximum Depth of Binary Tree 解答
Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- Maximum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Maximum Depth of Binary Tree 树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
随机推荐
- gnuplot安装的小问题
今天在学习NS2的过程中接触到了awk和gnuplot来分析延迟,丢包等情况. gnuplot是一款非常精巧的绘图工具,使用方法也很简单,功能却很强大. 安装还是通过终端: sudo apt-get ...
- WAMPserver配置(允许外部访问、phpmyadmin设置为输入用户名密码才可登录等)
对于很多不熟悉PHP环境安装的朋友来说,用集成环境可以更快的上手,更方便的搭建PHP的运行环境,但是,WAMP的集成环境仅仅是将底层基础工作做好 了,有些个别关键的配置操作并没有集成到环境安装中,所以 ...
- [GodLove]Wine93 Tarining Round #8
比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...
- 关于javascript自定义对象(来自网络)(最近几天不会的)
javascript定义对象的几种简单方法 1.构造函数方式,全部属性及对象的方法都放在构造方法里面定义 优点:动态的传递参数 缺点:每创建一个对象就会创建相同的方法函数对象,占用大量内存 funct ...
- bootstrapCDN和本地化
因为公司网络环境较差和自己是菜鸟的原因,很简单的事情折腾了不少时间.测试开发的网页时候 更新速度总是很慢,这跟使用bootstrapCDN有关系,因为每次更新,它都要重新访问cdn.bootstrap ...
- NotePad++ 调试PHP代码中文显示乱码
最近在NotePad++上调试PHP代码,按照示例代码进行调试,结果在显示中文的时候显示一堆乱码,于是上网百度,有2种方法可以解决: 按调试方式有2种方法: 1.菜单插件-NppExec: " ...
- Matlab 2013b 在El Capitan 中无法使用问题解决
更新了mac的操作系统到El capitan, 结果发现Matlab打不开了,每次都弹出一个Java error的窗口.现实如下内容 java.lang.NullPointerException at ...
- mysqldump-info
其实很多东西都能在info里面找到非常详细的说明,只是,我们太忙了,只想要一个答案,而无心去看而已,所以呢,就把用得到的都看一下来记录吧. 命令模式:mysqldump [options] [db_n ...
- Linux下的五个查找命令:grep、find、locate、whereis、which
原文转自 http://www.cnblogs.com/wanqieddy/archive/2011/07/15/2107071.html 1.grep grep(General Regular Ex ...
- java可变参数
Java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理.注意:可变参数必须位于最后一项.当可变参数个数多余一个时,必将有一个不是最后一项,所以只支持 ...