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]之间的所有 ...
随机推荐
- 练习,自定义TextView(1.1)
重新自定义TextView是非常有趣的事情,跟着Android4高级编程,通过自定义TextView,来敲一下代码: 这个是那么的简单,自定义TextView,新建CustomTextView继承Te ...
- D3.js 力导向图的拖拽(drag)与缩放(zoom)
不知道大家会不会跟我一样遇到这样的问题,在之前做的力导向图的基础上加上缩放功能的时候,拖动节点时整体会平移不再是之前酷炫的效果(失去了拉扯的感觉!).天啊,简直不能接受如此丑X的效果.经过不懈的努力终 ...
- L25词嵌入进阶GloVe模型
词嵌入进阶 在"Word2Vec的实现"一节中,我们在小规模数据集上训练了一个 Word2Vec 词嵌入模型,并通过词向量的余弦相似度搜索近义词.虽然 Word2Vec 已经能够成 ...
- C++获取char值
直接获取内存地址,不需要定义指针类型的方法,(当然也就不需要释放了)USES_CONVERSION; if (myFun1) { CString _input; ...
- this 关键字的用法
用法一 this代表当前类的实例对象 class Program { static void Main(string[] args) { tr ...
- ORM之单表、多表操作
参考1 参考2 表与表之间的关系: 一对一(OneToOneField):一对一字段无论建在哪张关系表里面都可以,但是推荐建在查询频率比较高的那张表里面 一对多(ForeignKey):一对多字段建在 ...
- Exercise
""" 问:执行完下面的代码后, l,m的内容分别是什么? """ def func(m): for k,v in m.items(): m ...
- 曹工杂谈--只用一个命令,centos系统里装了啥软件,啥时候装的,全都清清楚楚
前言 一直以来,对linux的掌握就是半桶水的状态,经常yum装个东西,结果依赖一堆东西:然后再用源码装个东西,只知道make.make install,背后干了啥也不清楚了,卸载也不方便. 这几天工 ...
- tp5--路由的使用(初级)
在配置文件夹下的route.php文件配置路由: 控制器: 运行结果:
- Mac home 目录下创建文件夹
example:sudo vim /etc/auto_master before: # Automounter master map +auto_master # Use directory serv ...