LeetCode--Jewels and Stones && Range Sum of BST (Easy)
771. Jewels and Stones (Easy)#
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
S and J will consist of letters and have length at most 50.
The characters in J are distinct.
solution##
class Solution {
public int numJewelsInStones(String J, String S) {
int count=0;
for(int i=0; i<J.length(); i++)
{
for(int j=0; j<S.length(); j++)
{
if(J.charAt(i) == S.charAt(j))
count++;
}
}
return count;
}
}
总结##
此题思路较简单,用两个循环加一个计数器count解决即可,外层循环遍历J,内层循环遍历S。先取J里面的一个字符与S里面的字符挨个比较,如果两字符相同,计数器count++,否则什么都不做,这样直至循环结束。
938. Range Sum of BST(Easy)#
Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
The binary search tree is guaranteed to have unique values.
Example 1:
Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
Output: 32
Example 2:
Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
Output: 23
Note:
The number of nodes in the tree is at most 10000.
The final answer is guaranteed to be less than 2^31.
solution##
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
int leftsum = 0; int rightsum = 0;
if (root.left != null)
leftsum = rangeSumBST(root.left,L,R);
if (root.right != null)
rightsum = rangeSumBST(root.right,L,R);
if (L <= root.val && root.val <= R)
return root.val + leftsum + rightsum;
return leftsum + rightsum;
}
}
总结##
此题为二叉树结点值求和的升级版。整体思想为先求左子树结点值大于L小于R的和,再求右子树结点值大于L小于R的和,最后,如果根结点值大于L小于R,则加上根结点值。
首先定义两个int变量leftsum和rightsum用来分别存放左右子树的和,如果左子树非空,则递归求左子树结点值的和leftsum,如果右子树非空,则递归求右子树结点值的和rightsum。最后,如果根结点值大于L小于R,则加上根结点值并返回,否则,只返回leftsum+rightsum的值。
求二叉树结点里面值(假设为int)的和
public int sumBST(TreeNode root)
{
int leftsum = 0; int rightsum = 0;
if (root.left != null)
leftsum = sumBST(root.left);
if (root.right != null)
rightsum = sumBST(root.right);
return root.val + leftsum + rightsum;
}
LeetCode--Jewels and Stones && Range Sum of BST (Easy)的更多相关文章
- 【Leetcode_easy】938. Range Sum of BST
problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- leetcode@ [327] Count of Range Sum (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- [LeetCode] 327. Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] 303. Range Sum Query - Immutable (Easy)
303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...
- LeetCode(304)Range Sum Query 2D - Immutable
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)
这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938).给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有 ...
随机推荐
- python3爬虫 爬取动漫视频
起因 因为本人家里有时候网速不行,所以看动漫的时候播放器总是一卡一卡的,看的太难受了.闲暇无聊又F12看看.但是动漫网站却无法打开控制台.这就勾起了我的兴趣.正好反正无事,去寻找下视频源. 但是这里事 ...
- python+selenium实现网页自动化与爬虫技术
举例某购物网站,通过selenium与python,实现主页上商品的搜索,并将信息爬虫保存至本地excel表内. 一.python环境与selenium环境安装 python在官网下载并安装并且设置环 ...
- MySQL笔记总结-其他
数据库相关概念 一.数据库的好处 1.可以持久化数据到本地 2.结构化查询 二.数据库的常见概念 ★ 1.DB:数据库,存储数据的容器 2.DBMS:数据库管理系统,又称为数据库软件或数据库产品,用于 ...
- AJAX教程——检视阅读
AJAX教程--检视阅读 参考 AJAX 教程--菜鸟 AJAX 教程--w3cschool AJAX 教程--w3school.cn AJAX 教程--易百 AJAX = Asynchronous ...
- sort()实现排序的原理
很多人都只知道sort()是通过快速排序实现,但它并不只是简单的快排:首先它对普通的快速排序进行了优化:此外,它还结合了插入 排序和堆排序.系统根据数据形式和数据量,来选择合适的排序方法,这并不是说每 ...
- [Qt] Release模式下产生调试信息
分两步,设置Qt配置文件,设置VS. https://blog.csdn.net/itas109/article/details/83652387 F:\Qt\Qt5.7.1\5.7\msvc2015 ...
- mac OS nvm 常用命令
nvm install stable ## 安装最新稳定版 node,当前是node v10.15.0 (npm v6.4.1) nvm install <version> ## 安装指定 ...
- Shiro(三):Spring-boot如何集成Shiro(下)
上一篇文章介绍了shiro在spring-boot中通过filter实现authentication流程(通过设置filterMaps也可以达到authorization的目的):这篇文章主要介绍sp ...
- Java 线程池(ThreadPoolExecutor)原理分析与实际运用
在我们的开发中"池"的概念并不罕见,有数据库连接池.线程池.对象池.常量池等等.下面我们主要针对线程池来一步一步揭开线程池的面纱. 有关java线程技术文章还可以推荐阅读:< ...
- 数学--数论-- AtCoder Beginner Contest 151(组合数+数学推导)好题(๑•̀ㅂ•́)و✧
思路统计最大值出现的次数,和最小值出现的次数.虽然是每次都是MAX-MIN,我们先求MAX的和,然后再求MIN的和,做差. 这次代码写的真的很漂亮 题目地址: #include <bits/st ...