leetcode 771. 宝石与石头

LeetCode 771. 宝石与石头Jewels and Stones 题目描述 阅读详情

leetcode 771. 宝石与石头.


题目描述

  1. 宝石与石头

给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。

字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。

示例 1:

输入:jewels = "aA", stones = "aAAbbbb" 输出:3 示例 2:

输入:jewels = "z", stones = "ZZ" 输出:0

提示:

1 <= jewels.length, stones.length <= 50 jewels 和 stones 仅由英文字母组成 jewels 中的所有字符都是 唯一的

解题思路

法1

方法1哈希表\

  1. 遍历宝石的种类,储存在map中

  2. 遍历石头,再宝石map中进行映射,如果有值表示该石头是宝石,否则就不是,记录宝石的数量

  3. 输出执行结果

  • 时间复杂度(O(n))
  • 空间复杂度(O(n))

执行结果

法1

函数使用一个整数变量 count 来计算拥有的石头中宝石的数量。另外,使用一个 jewelSet 的映射来存储宝石的类型。

首先,我们将 jewels 中的每个字符添加到 jewelSet 中。然后,遍历 stones 中的每个字符,如果字符在 jewelSet 中存在,说明它是宝石,将 count 增加1。

最后,返回 count 即可得到拥有的石头中宝石的数量。

func numJewelsInStones(jewels string, stones string) int {
 count := 0
 jewelSet := make(map[rune]bool)

 // 将宝石类型存储到一个集合中
 for _, jewel := range jewels {
  jewelSet[jewel] = true
 }

 // 检查拥有的石头,计算宝石数量
 for _, stone := range stones {
  if jewelSet[stone] {
   count++
  }
 }

 return count
}

执行结果: 通过 显示详情 查看示例代码 添加备注

执行用时: 0 ms , 在所有 Go 提交中击败了 100.00% 的用户 内存消耗: 1.9 MB , 在所有 Go 提交中击败了 22.92% 的用户 通过测试用例: 255 / 255 炫耀一下:


本文由 mdnice 多平台发布

LeetCode:771.宝石与石头 771 宝石与石头 阅读详情

相关推荐

基于FPGA的DDS信号发生器

之前的博客讲到了DDS的基本原理,现在用Verilog代码实现DDS,能够产生四种波形,方波,三角波,正弦波,锯齿波,用按键来控制频率和选择波形。其中按键消抖模块来自小梅哥的FPGA自学笔记设计与验证这本书。 module DDS_Module( Clk, Rst_n, EN, Fword, Key1, Pword, DA_Clk, DA_Data ); input Clk...

qq_43534451的博客 9953

每日一题——力扣——宝石与石头

题目来源于力扣——771. 宝石与石头 - 力扣(LeetCode) (leetcode-cn.com) 给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。 stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 字母区分大小写,因此 "a" 和 "A" 是不同类型的石头。 示例 1: 输入:jewels = "aA", stones = "aAAbbbb" 输出:3 示例 2: 输入:jewels =

爱编程的晖哥的博客 1066

某商场G区火灾自动报警与控制系统设计

某商场G区火灾自动报警与控制系统设计 摘要:随着我国经济建设的迅速发展,人民生活水平不断提高,城市用地日益紧张,促使建筑物正朝着高层化、密集化方向发展。高层建筑的特点决定其火灾的危险性和高层建筑的火灾自动报警系统的重要性,一套完整的火灾自动报警及联动控制系统是高层建筑发生火灾时人们生命财产的有利保障,是能否快速准确地发现火情,把火灾扑灭在萌芽状态的关键所在。文章通过消防设计,论述了火灾自动报警及消...

weixin_44026784的博客 5375

Leetcode6-宝石与石头(771)

给你一个字符串 jewels 代表石头中宝石的类型,另有一个字符串 stones 代表你拥有的石头。stones 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。字母区分大小写,因此 “a” 和 “A” 是不同类型的石头。示例 1:输入:jewels = “aA”, stones = “aAAbbbb”输出:3示例 2:输入:jewels = “z”, stones = “ZZ”输出:0提示:jewels 和 stones 仅由英文字母组成。

qq_37346140的博客 573

如何使用obspy下载地震波形数据

如果你是地震研究学者,或者需要进行地震方面的研究,那么地震数据下载是一个必不可少的流程,本文将告诉你如何使用obspy下载想要的地震数据

monsterk1的博客 2081

Vibe-Trading深度体验:AI如何用自然语言一键生成量化交易策略

量化交易通过数学模型和计算机程序执行投资决策,其核心在于将市场规律转化为可回测的交易信号。传统策略开发需融合金融理论、统计学与编程,门槛较高。随着大语言模型技术的发展,自然语言处理(NLP)为策略生成提供了新范式。通过领域微调,模型能理解“均线突破”、“RSI超卖”等专业术语,并将其编译为结构化逻辑。这项技术的工程价值在于极大降低了策略构思与验证的周期,实现了策略开发的快速迭代与民主化。其应用场景广泛,从个人投资者的想法验证到专业团队的策略灵感筛选均能受益。本文探讨的Vibe-Trading项目,正是这一趋

weixin_34269583的博客 416

LeetCode-每日一题-宝石和石头

中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。代表石头中宝石的类型,另有一个字符串。字母区分大小写,因此。

weixin_61810048的博客 306

RTDETR最强结构图 | 包含模型整体结构图 | 全模块展开图 | AiFi模块展开图

RT-DETR 最全结构图

Coding... 9607

力扣(LeetCode)771.宝石与石头

力扣(LeetCode)771.宝石与石头 题目 宝石与石头 内容描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1 输入: J = “aA”, S = “aAA...

渡航的博客 307

【中科院一区Top-源码复现】Day-ahead photovoltaic power forecastingwith multi-source temporal-featureconvoluti

【中科院一区Top-源码复现】Day-ahead photovoltaic power forecastingwith multi-source temporal-featureconvoluti

744

Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程

本文主要介绍了Codex 调试记录获取完全指南:日志查看、工具调用与实战排查教程,希望能对使用Codex的同学们有所帮助。 文章目录 1. 背景 2. 实战教程

weixin_43178406的博客 7万+

宝石与石头——leetcode771

leetcode771——字符串比较问题,宝石与石头 #include<iostream> #include<string> #include<istream> using namespace std; int main(){ string J;//代表石头中宝石的类型 string S;//代表拥有的石头,其中每个char代表了一种拥有的石头类型 //...

chicken3wings的博客 223

从源码编译CesiumForUnreal

从源码编译CesiumForUnreal插件

专注GIS开发 5644

LeetCode 771. 宝石与石头 (Java)

宝石与石头 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = “aA”, S = “aAAbbbb” 输出: 3 来源:力扣(LeetCode) 链接:https...

nuts_and_bolts的博客 248

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode 771. 宝石与石头

通过使用集合来存储宝石类型,我们可以高效地解决这个问题。这种方法不仅简洁,而且运行效率高,适合处理大量数据。希望这篇博客对你有所帮助!

makeke123456的博客 619

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode 771 宝石与石头 附源码

LeetCode 771 宝石与石头 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = “aA”, S = “aAAbbbb” 输出: 3 示例 2: 输入: J = “z”, S = “ZZ” 输出: 0 注意: S 和 J 最多含有50个字母。 J 中的字符不重复。 暴力法

qq_42788968的博客 251

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode—771. 宝石与石头

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = “aA”, S = “aAAbbbb” 输出: 3 示例 2: 输入: J = “z”, S = “ZZ” 输出:...

没错就是阿罗的博客 281

LeetCode 771:宝石与石头

目录LeetCode 771:宝石与石头题目描述解题 LeetCode 771:宝石与石头 题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = “aA”, S = “aAAbbbb” 输出: 3 示例 2: 输入: J = “z”, S = “ZZ” 输出: 0

s_Shune 138

Leetcode 771 宝石与石头

Leetcode 771 宝石与石头 题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 解题 拿到题的第一个想法是能不能空间换时间 然后想了想发现不太可能 因为即使把字符串拆分成单个的字符 也需要用循环一个个的遍历 所以这题大概可以直接用循环暴力解 时间复杂度O(MN) public class

qq_45864365的博客 159

leetcode771:宝石与石头

question1: leetcode771:宝石与石头 题目描述:给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。输入: J = “aA”, S = "aAAbbbb"输出: 3 这道题比较基...

qq_44757201的博客 245

Leetcode 771 宝石与石头编程题

Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了。 771 宝石与石头 简单题 应该用集合来做 给定字符串J代表石头中宝石的类型,和字符串S代表你拥有的石头。S中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J中的字母不重复,J和S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 ...

weixin_30770783的博客 299

【LeetCode每日一题】[简单]771. 宝石与石头

【LeetCode每日一题】[简单]771. 宝石与石头 771. 宝石与石头 题目来源 算法思想:搜索二叉树 题目: java代码 class Solution { public int numJewelsInStones(String J, String S) { boolean[] m = new boolean[128];//标记数组 char[] a = J.toCharArray(); char[] b = S.toCharArra

qq_39457586的博客 225

leetcode-771宝石与石头-easy

771 宝石与石头 题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 链接:https://leetcode-cn.com/problems/jewels-and-stones 示例 1: 输入: J = "aA", S = "aAAbbbb" 输出: 3 示例 2: 输入: J = "z

Yang_1998的博客 213

力扣(LeetCode) 771. 宝石与石头

给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。 J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。 示例 1: 输入: J = "aA", S = "aAAbbbb" 输出: 3 示例 2: 输入: J = "z", S = "ZZ" 输出: ...

weixin_30606669的博客 101
上一篇: leetcode 766. 托普利茨矩阵
下一篇: leetcode 806. 写字符串需要的行数
dengjiayue
博客等级 码龄5年 543粉丝 247原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址