最近做的题记录下。

258. Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 111 + 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;
}
};
34. Search for a Range

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].

For example,

Given [5, 7, 7, 8, 8, 10] and target value 8,
return [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;
}

这个题就是要把握好边界,比如找左边界时 =号要给右边界的判断,找右边界则相反。这样才能保证不遗漏

136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
直接用异或
 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;
}
389. Find the Difference

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.

Find the letter that was added in t
 
这种方法挺慢,我提交显示16ms,没有那种用char数组来模拟hash快。
 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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. Maximum Depth of Binary Tree 解答

    Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...

  4. 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 ...

  5. 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...

  6. [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 ...

  7. [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 ...

  8. Maximum Depth of Binary Tree 树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. 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 ...

随机推荐

  1. Intellij IDEA 初级学习文章

    Intellij IDEA同时打开多个项目 IntelliJ IDEA 14.x 创建工作空间与多个Java Web项目 使用IntelliJ IDEA 14和Maven创建java web项目

  2. Android 标题栏菜单设置与应用(popupWindow的应用)

    效果图

  3. 信号量与PV操作

    在计算机操作系统中,PV操作是进程管理中的难点.首先应弄清PV操作的含义:PV操作由P操作原语和V操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下:    P(S):①将信号量S的 ...

  4. win7 IIS7环境下部署PHP 7.0

    最近在本机电脑win7 II7环境下部署PHP 7.0遇到一些问题,将之记录下来 简要步骤如下: 1.到php官网下载php,由于是IIS环境要下载非线程安全的版本,我下载的是7.0.13 2.解压到 ...

  5. Eclipse/IDEA使用小技巧

    使用IDEA,先将keymap改为eclipse形式 1.搜索技巧: f4:列举所有类树状结构 Ctrl+F:搜索特定词 Ctrl+T:列举所有子类 Ctrl+O:快速检索想要的方法 Ctrl+Shi ...

  6. EL与Velocity基本语法总结:

    El(expression language): 基本语法点: $与{}搭配使用是常态取值 . 与[]的区别,后者可以取特殊值:- .等 支持一些基本的逻辑运算: && || > ...

  7. akka优势

    1.提供可扩展的实时事务处理. 2.为以下目标设计: 垂直扩展(并发) 水平扩展(远程调用) 高容错 3.Akka的核心,Akka-actor非常小的,可以非常方便地放进你的应用中,提供你需要的异步无 ...

  8. Adobe Flash builder 4的序列号

    下载flex 4 之后,需要输入注册码(如果购买了正版就要选择第二个的单选框,无需输入注册码了) 1424-4258-9368-0713-8534-5128 1424-4806-8312-7960-9 ...

  9. java IO基础操作

    java IO基础,通熟易懂,好久没复习java 基础了.这里是传送门... http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.ht ...

  10. ListView的item里面控件文本颜色修改

    @SuppressLint("InflateParams") @Override public View getChildView(int groupPosition, int c ...