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 ...
随机推荐
- 一些初级Java错误,不定期增加
1. Error: Dangling meta character '*' near index 0 对字符串使用split()方法截取 * ? + / | 等字符的时候会报以下异常 Dangling ...
- NPOI 单元格(cell) 格式参数
NPOI 单元格(cell) 将格式设为文本 在网上找了很久,都没有关于如何设置的信息,没办法查了下NPOI的源码终于找到了方法.这里共享下,就是“@”参数 ICellStyle cellStyle ...
- 第5章 搭建S3C6410开发板的测试环境
1.使用Eboot擦除NandFlash的方法如下: 第一步:准备工作 用串口线或USB转串口线连接开发板和PC,并启动minicom 第二步:进入Eboot状态 打开OK6410开发板的电源开关,过 ...
- mybatis配置文件查询参数的传递
通常来说,参数传递可以使用#与$进行编写,但是使用#的效率更高,使用$方式,查看日志更方便些,尤其是当执行的sql语句非常麻烦的时候. 1) 接口 形式 以下方式 [传递参数是一个实体] public ...
- bookstores网上书店测试缺陷报告1
Bookstore网上书店系统测试缺陷报告 缺陷编号 01.01.0001 发现人 吴赵昕 记录日期 2016-06-10 所属模块 购物车 确认人 吴赵昕 确认日期 2016-06-10 当前状 ...
- HDU 5884 (贪心)
problem sort 题目大意 有n个数组,每个数组有a[i]个元素,每次可以将至多k个数组合并为一个数组,所花费代价为这些数组的元素和.给定代价上限,求将所有数组合并为1个数组的最小k. 解题分 ...
- 模拟jQuery库
用js模拟jQuery方法,体会封装思想 <!DOCTYPE html><html><head><meta charset="UTF-8" ...
- linux磁盘存储命令 磁盘存储命令
硬盘空间是一个有限的资源, 硬盘空间是一个有限的资源,用户用下面的命令 可 以随时了解当前硬盘空间的使用情况. 以随时了解当前硬盘空间的使用情况. ? du,df命令 查看磁盘空间状况的操作 , ...
- pylot是一款开源的web性能测试工具
pylot是一款开源的web性能测试工具,http://www.pylot.org/ 参考文档:http://www.pylot.org/gettingstarted.html很容易上手 使用分为以下 ...
- js jquery实时计算输入字符
在项目中需要倒还可以输入多少字符