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)的更多相关文章

  1. 【Leetcode_easy】938. Range Sum of BST

    problem 938. Range Sum of BST 参考 1. Leetcode_easy_938. Range Sum of BST; 完

  2. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  3. 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 ...

  4. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  5. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  6. 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 ...

  7. 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 ...

  8. 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)

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

  9. LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)

    这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938).给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有 ...

随机推荐

  1. GCD - Extreme (II) UVA - 11426 欧拉函数与gcd

    题目大意: 累加从1到n,任意两个数的gcd(i,j)(1=<i<n&&i<j<=n). 题解:假设a<b,如果gcd(a,b)=c.则gcd(a/c,b ...

  2. Spring5:IOC注解

    使用注解须知: 1:导入约束:导入context的命名空间 2:配置注解的支持:<context:annotation-config/> <?xml version="1. ...

  3. Spring Boot中只能有一个WebMvcConfigurationSupport配置类

    首先将结论写文章的最前面,一个项目中只能有一个继承WebMvcConfigurationSupport的@Configuration类(使用@EnableMvc效果相同),如果存在多个这样的类,只有一 ...

  4. PHP的yield是个什么玩意

    来源:https://segmentfault.com/a/1190000018457194 其实,我并不是因为迭代或者生成器或者研究PHP手册才认识的yield,要不是协程,我到现在也不知道PHP中 ...

  5. Apache Rewrite实现URL的跳转和域名跳转

    Apache Rewrite实现URL的跳转和域名跳转   Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基 于服务器级的(httpd.conf)和目录级的 (.h ...

  6. ansible的role(6)

    roles的优点 roles能够根据层次型结构自动的装载变化量文件,task,以及handles等. 要使用肉了是只需要在playbook中使用include引入即可. 主要使用场景复制代码较高的情况 ...

  7. 如何在 Windows Event Log 中查找系统重启的信息

    事件ID:12 事件ID 13: 事件ID 41: 事件ID 6008: 事件ID 1074:事件ID 1074: ========================================== ...

  8. Docker简单操作(二)

    1.docker容器简单操作 docker search 镜像名 #搜索镜像.如docker search nginx docker pull alpine #拉取镜像.alpine是比较小的镜像 d ...

  9. Linux网络服务第三章远程访问及控制

    1.笔记 655355:端口限制 监听地址:对外提供服务的地址 AllowUsers:仅允许用户登录 DenyUsers:仅禁止用户登录 AllowUsers-用户名-公网地址 ssh/id_rsa. ...

  10. CF1285 --- Dr. Evil Underscores

    CF1285 --- Dr. Evil Underscores 题干 Today as a friendship gift, Bakry gave Badawy \(n\) integers \(a_ ...