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 ...
随机推荐
- what just I know
#update_s#http://www.taijixy.com/linker.html#update_e# #server_s#www.taijixy.com#server_e# #live_ver ...
- 第9章 硬件抽象层:HAL
HAL(硬件抽象层)是建立在Linux驱动之上的一套程序库.这套程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.在传统的Linux系统中Linux驱动一般有两种类型的代码:访问硬件 ...
- MVC+UnitOfWork+Repository+EF 之我见
UnitOfWork+Repository模式简介: 每次提交数据库都会打开一个连接,造成结果是:多个连接无法共用一个数据库级别的事务,也就无法保证数据的原子性.一致性.解决办法是:在Reposito ...
- JS中直接从java后台获得对象的值(数组的值)
这里举得例子是:JS直接从后台Contorller中(SpringMVC中的model中)获得数值的值 Contorller 此处将 talentIntegralRecordsDay talentIn ...
- scikit-learn使用笔记与sign prediction简单小结
经Edwin Chen的推荐,认识了scikit-learn这个非常强大的python机器学习工具包.这个帖子作为笔记.(其实都没有笔记的意义,因为他家文档做的太好了,不过还是为自己记记吧,为以后节省 ...
- 搞懂 SynchronizationContext
SynchronizationContext -MSDN 很让人失望 我不知道为什么,目前在.Net下关于这个类只有很少的资料.MSDN文档也只有很少的关于如何使用SynchronizationCon ...
- RealProxy深入
Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...
- python的编码判断_unicode_gbk/gb2312_utf8(附函数)
python中, 我们平常使用最多的三种编码为 gbk/gb2312, utf8 , unicode. 而python中并没有一个函数来进行 编码的判断.今天,主要对这三种编码进行讨论,并给出区分 ...
- jmeter 中的 Beanshell 使用
一.操作变量:通过使内置对象vars可以对变量进行存取操作 a) vars.get("name"):从jmeter中获得变量值 b) vars.put("key" ...
- jquery插件的引用和扩展应用
引入插件,引用的jquery吆喝开发插件的jquery版本一致,引用插件里面所用的图像img图片等,常用的jquery可以通过对其进行函数描述,直接调用,下例子就是改变特定的颜色,插件的使用是在仿网页 ...