492. Construct the Rectangle

Input: 4
Output: [2, 2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
//[L,W]  L要大于W,所以以W为基础,设W为一半迭代下去
//1.Because question requires L, D as close as possible, I start the finding from the middle point which is sqrt(area).
//2.when the Area divide Width have remainder , it should be the solution vector<int> constructRectangle(int area) {
for(int mid = sqrt(area); mid>; mid--)
if (!(area%mid))
return {area/mid, mid};
}

501. Find Mode in Binary Search Tree

BST众数

思路:由小到大inorder,O(n) time and O(1) space by inorder traversal

class Solution {
public:
int maxFreq = , currFreq = , precursor = INT_MIN;
vector<int> res; vector<int> findMode(TreeNode *root)
{
inorderTraversal(root);
return res;
} void inorderTraversal(TreeNode *root)
{
if (root == NULL) return; // Stop condition
inorderTraversal(root->left); // Traverse left subtree
if (precursor == root->val) currFreq++;
else currFreq = ;
if (currFreq > maxFreq)
{// Current node value has higher frequency than any previous visited
res.clear();
maxFreq = currFreq;
res.push_back(root->val);
}
else if (currFreq == maxFreq)
{// Current node value has a frequency equal to the highest of previous visited
res.push_back(root->val);
}
precursor = root->val; // Update the precursor
inorderTraversal(root->right); // Traverse right subtree
}
};

520. Detect Capital

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".
class Solution(object):
def detectCapitalUse(self, word):
c =
for i in word: //统计大字母
if i == i.upper():
c +=
return c == len(word) or (c == and word[] == word[].upper()) or c == ##三种情况,随便一种都行,全部为大/只有头字母为大/全部为小

530. Minimum Absolute Difference in BST

小到大树,从左下角开始递归,每次算当前节点的值减去前一个节点的值,根据该值来更新min

void inorderTraverse(TreeNode* root, int& val, int& min_dif) {
if (root->left != NULL) inorderTraverse(root->left, val, min_dif);
if (val >= ) min_dif = min(min_dif, root->val - val);
val = root->val;
if (root->right != NULL) inorderTraverse(root->right, val, min_dif);
}
int getMinimumDifference(TreeNode* root) {
auto min_dif = INT_MAX, val = -;
inorderTraverse(root, val, min_dif);
return min_dif;
}

538. Convert BST to Greater Tree

右边开始dfs,利用二叉树的右边比左边大的性质,递归加上右边的值

class Solution {
private:
int cur_sum = ;
public:
void travel(TreeNode* root){
if (!root) return;
if (root->right) travel(root->right); root->val = (cur_sum += root->val);
if (root->left) travel(root->left);
}
TreeNode* convertBST(TreeNode* root) {
travel(root);
return root;
}
};

543. Diameter of Binary Tree

找最长的路径,不一定包含头节点,可以是从左下到右下

class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
if(root == nullptr) return ;
int res = depth(root->left) + depth(root->right);
return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right))); //左右两个红框
} int depth(TreeNode* root){
if(root == nullptr) return ;
return + max(depth(root->left), depth(root->right)); //黑框
}
};

leetcode 492-543 easy的更多相关文章

  1. LeetCode: 492 Construct the Rectangle(easy)

    题目: or a web developer, it is very important to know how to design a web page's size. So, given a sp ...

  2. 21. leetcode 492

    492: 给定一个面积值,求它的长l和宽w.长和宽需满足:长大于等于宽,长和宽的差值尽可能小,长乘宽等于面积. 思路:先将l和w初始化为sqrt(area),然后看l*w是否等于面积,如果等于则返回l ...

  3. Leetcode 492. 构造矩形

    1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...

  4. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. leetcode第一题(easy)

    第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  6. Java实现 LeetCode 492 构造矩形

    492. 构造矩形 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 你设 ...

  7. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  8. LeetCode - 492. Construct the Rectangle

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

  9. Leetcode——Two Sum(easy)

    题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...

  10. [LeetCode] 492. Construct the Rectangle_Easy tag: Math

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

随机推荐

  1. mongodb 3.2 yaml 配置详解及范例

    mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...

  2. [USACO2005 nov] Grazing on the Run【区间Dp】

    Online Judge:bzoj1742,bzoj1694 Label:区间Dp 题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我 ...

  3. loj2513 治疗之雨

    题意:你的英雄一开始血量为p,你还有m个队友,血量无穷.血量上限为n,下限为0.如果血量满了就不能加血.每次启动操作,随机给m+1个英雄加1点血,然后等概率随机k次每次对于英雄扣1点血.求期望操作几次 ...

  4. nicescroll 使用与配置

    使用// 1. 简单模式,设置html元素滚动 $(document).ready(function() { $("html").niceScroll(); }); // 2. 返 ...

  5. csps模拟测试7576一句话题解

    题面:https://www.cnblogs.com/Juve/articles/11694454.html 75考了数学,化学和物理... T1: 假设有一个A和B,那么对于每一个j!=i,都有$\ ...

  6. Django如何自定义漂亮的404页面

    目录 在templates 中添加404.html 修改settings.py 在templates 中添加404.html <!DOCTYPE html PUBLIC "-//W3C ...

  7. JavaScript中字符串类型

    字符串类型 字符串介绍 这是程序里面使用最为广泛的一-种类型.在JavaScript里面, 可以使用单引号,也可以使用双引号: 字符串这种数据类型非常霸道,它和其他数据类型相加都会被转换后才为字符串类 ...

  8. iOS之CGPath相关属性(一)

    #ifndef CGPATH_H_ #define CGPATH_H_ForeverGuard博客园 #include <CoreFoundation/CFBase.h> #include ...

  9. python中关于传递参数模块argprase的一些小坑

    今天在写代码的时候遇到了一个关于parser的一些小坑,记录在此备用. 我们知道在python中可以用argprase来传递一些参数给代码执行,来看下面的例子,假设现在有一个test文件夹,下面有3个 ...

  10. <每日一题>题目10:求斐波拉契数列

    def func(x): m,n = 0,1 i = 0 while i < x: yield m m,n = n,m+n i += 1 fib = [] get_func = func(100 ...