这是一个简单的问题,描述为:
给你一个 0 索引的字符串单词数组和一个字符 x。
返回表示包含字符 x 的单词的索引数组。
请注意,返回的数组可以是任意顺序。
示例1:
输入:words = ["leet","code"], x = "e"
输出:[0,1]
解释:“e”出现在两个单词中:“leet”和“code”。因此,我们返回索引 0 和 1。示例2:
输入:words = ["abc","bcd","aaaa","cbc"], x = "a"
输出:[0,2]
解释:“a”出现在“abc”和“aaaa”中。因此,我们返回索引 0 和 2。示例3:
输入:words = ["abc","bcd","aaaa","cbc"], x = "z"
输出:[]
解释:“z”未出现在任何单词中。因此,我们返回一个空数组。限制:
1
1
x 是小写英文字母。
words[i] 仅由小写英文字母组成。
要解决这个问题,您需要迭代单词列表,在每个单词上检查是否包含字符,如果是,则将其索引存储到响应列表中:
class Solution { public List<integer> findWordsContaining(String[] words, char x) { // create response final List<integer> indexes = new ArrayList(); // iterate words string array for(int i=0;i<words.length check if char exists into the word indexes.add yes add index response return searched indexes> <blockquote> <p>运行时间:1ms,比Java在线提交的100.00%快。</p> <p>内存使用:44.95 MB,低于 Java 在线提交的 49.76%。</p> </blockquote> <p>—</p> <p>如果您想采用 lambda/函数方法,这种方法通常更干净,但对性能的影响更大,它看起来像这样:<br> </p> <pre class="brush:php;toolbar:false">class Solution { public List<integer> findWordsContaining(String[] words, char x) { return IntStream.range(0, words.length) .boxed() // convert primitive into Class related (int -> Integer) .map(i -> getIndexIfCharExistsInWord(words[i], i, x)) .filter(Objects::nonNull) // to remove null ones from mapping .collect(Collectors.toList()); } public Integer getIndexIfCharExistsInWord(final String word, final int i, final char x) { return word.indexOf(x) != -1 ? i : null; } } </integer>
运行时间:9ms,比 Java 在线提交的 2.72% 快。
内存使用:44.90 MB,低于 Java 在线提交的 66.32%。
—
就是这样!如果还有什么要讨论的,请随时发表评论,如果我错过了任何内容,请告诉我,以便我进行相应更新。
直到下一篇文章! :)
以上是Leetcode — 查找包含字符的单词的详细内容。更多信息请关注PHP中文网其他相关文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。