637. Average of Levels in Binary Tree\633. Sum of Square Numbers\643. Maximum Average Subarray I
来源:互联网 发布:俄罗斯知乎 编辑:程序博客网 时间:2024/06/14 00:20
- Average of Levels in Binary Tree
- Problem
- Implementation
- Sum of Square Numbers
- Problem Description
- Implementation
- Maximum Average Subarray I
- Description
- Implementation
637. Average of Levels in Binary Tree
Problem
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
Example 1:
Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
Note:
The range of node’s value is in the range of 32-bit signed integer.
Implementation
Use BFS to solve this problem.
/** * 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: vector<double> averageOfLevels(TreeNode* root) { vector<double> res; queue<TreeNode*> que; if(root) que.push(root); else return res; while(!que.empty()) { int sz = que.size(); double level_sum = 0; for(int idx = 0; idx < sz; idx++) { TreeNode* tp = que.front(); que.pop(); level_sum += tp->val; if(tp->left) que.push(tp->left); if(tp->right) que.push(tp->right); } res.push_back(level_sum/sz); } return res; }};
633. Sum of Square Numbers
Problem Description
Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.
Example 1:
Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5Example 2:Input: 3Output: False
Implementation
class Solution {public: bool judgeSquareSum(int c) { int end_ele = ceil(sqrt(c)); int stt_ele = floor(sqrt(c/2.0)); for(int idx = end_ele; idx >= stt_ele; idx--) { int left = c - idx*idx; if(ceil(sqrt(left)) == floor(sqrt(left))) { return true; } } return false; }};
643. Maximum Average Subarray I
Description
Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value.
Example 1:
Input: [1,12,-5,-6,50,3], k = 4Output: 12.75Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
Note:
1 <= k <= n <= 30,000.
Elements of the given array will be in the range [-10,000, 10,000].
Implementation
class Solution {public: double findMaxAverage(vector<int>& nums, int k) { int size = nums.size(); int res = 0; if(size < k || k == 0) { return res; } for(int idx = 1; idx < size; idx++) { nums[idx] += nums[idx-1]; } res = nums[k-1]; for(int idx = k; idx < size; idx++) { if(nums[idx]-nums[idx-k]> res) { res = nums[idx] - nums[idx-k]; } } return double(res)/k; }};
阅读全文
0 0
- 637. Average of Levels in Binary Tree\633. Sum of Square Numbers\643. Maximum Average Subarray I
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 使用python进行ridge回归
- springmvc第一天
- Meshlab源码初探
- web窗体中连接按钮随心所欲放
- tomcat绑定域名
- 637. Average of Levels in Binary Tree\633. Sum of Square Numbers\643. Maximum Average Subarray I
- swift4.0 UIButton小解
- JavaScript学习(五)
- JAVA微信扫码支付模式二功能实现以及回调
- 机器学习中导数最优化方法
- shiro MD5
- 存储过程简单实例
- 微信小程序页面间的通信
- Linux设备驱动模型3——platform平台总线工作原理