Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

最近做的题记录下。

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.

 1 int addDigits(int num) {
 2         char strint[12] = {0};
 3         sprintf(strint, "%d", num);
 4         int i=0,a = 0;
 5         while(strint[i+1]!= ‘\0‘)
 6         {
 7            a = (strint[i]-‘0‘)+(strint[i+1]-‘0‘);
 8            if(a>9)
 9            {
10                 a = a%10+a/10;
11            }
12            strint[++i] = a+‘0‘;
13         }
14         return strint[i]-‘0‘;
15 }

上边这是O(n)的复杂度。网上还有O(1)的复杂度的:return (num - 1) % 9 + 1;

104. Maximum Depth of Binary Tree

求二叉树的深度,可以用递归也可以用循环,如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     struct TreeNode *left;
 6  *     struct TreeNode *right;
 7  * };
 8  */
 9 int maxDepth(struct TreeNode* root) {
10     int left = 0,right = 0;
11     if (root == NULL) return 0;
12
13     left = maxDepth(root->left);
14     right = maxDepth(root->right);
15
16     return left>right? left+1:right+1;
17 }

******************************************************************************************************************

用队列存结点,记录每一层的结点数levelCount,每出一个结点levelCount--,如果减为0说明要进入下一层,然后depth++,同时队列此时的结点数就是下一层的结点数。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode* root) {
13         if (root == NULL) return 0;
14
15         //深度和每一层节点数
16         int depth = 0,levelCount = 1;
17         //队列保存每一层的节点数
18         queue<TreeNode*> node;
19
20         node.push(root);
21         while(!node.empty())
22         {
23             //依次遍历每一个结点
24             TreeNode *p = node.front();
25             node.pop();
26             levelCount--;
27
28             if(p->left) node.push(p->left);
29             if(p->right) node.push(p->right);
30
31             if (levelCount == 0)
32             {
33                 //保存下一层节点数,深度加1
34                 depth++;
35                 levelCount = node.size();
36             }
37         }
38         return depth;
39     }
40 };

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]

 1 vector<int> searchRange(vector<int>& nums, int target) {
 2         int size = nums.size();
 3         int l=0,r=size-1;
 4         while(l<=r)
 5         {
 6             int mid = (l+r)/2;
 7             if (nums[mid] >= target)
 8             {
 9                 r = mid-1;
10             }
11             else if(nums[mid] < target)
12             {
13                 l = mid+1;
14             }
15         }
16         int left = l;
17         l = 0;
18         r = size-1;
19         while(l<=r)
20         {
21             int mid = (l+r)/2;
22             if (nums[mid] <= target)
23             {
24                 l = mid+1;
25             }
26             else if(nums[mid] > target)
27             {
28                 r = mid-1;
29             }
30         }
31         int right = r;
32         vector<int> v;
33         v.push_back(left);
34         v.push_back(right);
35         if(nums[left] != target || nums[right] != target)
36         {
37             v[0] = -1;
38             v[1] = -1;
39         }
40         return v;
41     }

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

136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

直接用异或

 1 int singleNumber(vector<int>& nums) {
 2         int result=0;
 3         int len = nums.size();
 4         if (len <=0) return 0;
 5         for(int i=0;i<len;i++)
 6         {
 7             result ^= nums[i];
 8         }
 9         return result;
10     }

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快。

 1 char findTheDifference(string s, string t) {
 2         map<char, int> sumChar;
 3         char res;
 4         for(auto ch: s) sumChar[ch]++;
 5         for(auto ch: t)
 6         {
 7            if(--sumChar[ch]<0)
 8                res = ch;
 9         }
10         return res;
11     }

这道题也可以用异或 因为相同的会异或掉,剩下一个就是多余的char。

时间: 2024-10-12 07:51:49

Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference的相关文章

Maximum Depth of Binary Tree 树的最大深度

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search /** * Definition for binary tree * struct TreeNode { * i

重做104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路:知道有recursion的方法.但是想应用一下dfs和backtracking,啊哈哈终于做出来了. Solution1: /** * Definition for a bi

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 longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

Maximum Depth of Binary Tree 解答

Question Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Solution 1 -- Recursion Recursion way is easy to think. Similar with "Minimum Dep

[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 longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search 简单的深度搜索 #include <iostream> using namespace std; /** *

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 the longest path from the root node down to the farthest leaf node. Hide Tags Tree Depth-first Search 题意:输出二叉树的深度 thinking: 深度优先搜索,记录最大深度 code: /** *

【LeetCode从零单排】No104 Maximum Depth of Binary Tree

题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 代码 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode lef

leetcode——Maximum Depth of Binary Tree (递归,)

Maximum Depth of Binary Tree Total Accepted: 59837 Total Submissions: 132940My Submissions Question Solution Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the f

[LeetCode] 559. Maximum Depth of N-ary Tree

Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a 3-ary tree: We should return its max depth, which is 3. Note: The dept