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 ...
随机推荐
- 修改HTTPD.CONF中的DocumentRoot,出现 You don't have permission to access /??? on this server.
apache 2.4 修改/conf/extra/httpd-vhosts.conf <VirtualHost _default_:80> DocumentRoot '${SRVROOT} ...
- centos7下安装nodejs
http://www.runoob.com/nodejs/nodejs-install-setup.html这个教程中注明 注意:Linux上安装Node.js需要安装Python 2.6 或 2.7 ...
- Adobe Audition音频制作
Adobe Audition 同义词 AU(软件(Adobe Audition))一般指Adobe Audition Adobe Audition是一个专业音频编辑和混合环境,原名为Cool Edit ...
- git入门及上传项目到github
Git入门: 如果你完全没有接触过Git,你现在只需要理解通过Git的语法(敲入一些命令)就可以将代码上传到远程的仓库或者下载到本地的仓库(服务器),我们此时应该有两个仓库,就是两个放代码 ...
- 模拟Post请求
此文摘自csdn青山的博客地址:http://blog.csdn.net/a497785609/article/details/6437154 本人随笔只为方便自己查阅,也为广大网友提供方便,不喜勿喷 ...
- vconfig使用帮助
====================================================== VCONFIG(8) ...
- 如何使用花生壳 发布WCF服务 进行外网访问
当我们发布WCF服务的时候,可以直接通过服务器的域名或者IP进行. 但是如果仅仅是通过花生壳进行域名解析,需要我们自己在设置的时候注意以下几点, 直接用图说明问题 1.首先配置花生壳,在红色处填写一个 ...
- 排球比赛计分规则(P205页)
排球比赛计分规则: 1.SPEC的目标是什么?SPEC的目标不包括什么? 为了让大家更加了解排球比赛. 2.SPEC用户和典型场景是什么? 用户:运动员,观众,教练 场景:排球赛场 3.SPEC用到哪 ...
- 面试时被问到js的绑定事件,我居然不知道怎么回答。回来查了下,做个笔记
事件绑定是几种方法 以下为例: <button id='btn'>click me</button> function Btn(){ alert('click'); } 1.直 ...
- Surprise团队第二周项目总结
Surprise团队第二周项目总结 项目进展 已实现五子棋人人模式部分 人人模式: 基本方式:采取黑棋先行,黑白交替的下棋顺序. 模式:通过鼠标点击相应棋盘中的"交叉点",在lay ...