当前位置:网站首页>js . Find the first palindrome string in the array
js . Find the first palindrome string in the array
2022-07-03 14:06:00 【Cupid510】
Here's an array of strings words , Find and return... In the array First palindrome string . If there is no string that meets the requirements , Return to one An empty string “” .
Palindrome string For the definition of : If a string is read in the same way as it is read in the opposite direction , Then the string is a Palindrome string .
Example 1:
Input :words = [“abc”,“car”,“ada”,“racecar”,“cool”]
Output :“ada”
explain : The first palindrome string is “ada” .
Be careful ,“racecar” It's also a palindrome string , But it's not the first .
Example 2:
Input :words = [“notapalindrome”,“racecar”]
Output :“racecar”
explain : The first and only palindrome string is “racecar” .
source : Power button (LeetCode)
/** * @param {string[]} words * @return {string} */
var firstPalindrome = function(words) {
for(let i =0;i<words.length;i++){
let str = words[i]
let len = str.length
// Intermediate index
let index = parseInt(len / 2)
// The left character
let str1 = str.substring(0,index)
// On the right side
let str2=str.substring(len-index,len)
if(str2.split("").reverse().join("")==str1){
return str
}
}
return ""
};
边栏推荐
- FPGA test method takes mentor tool as an example
- Richview trvstyle liststyle list style (bullet number)
- Generate directories from web content
- Go language unit test 4: go language uses gomonkey to test functions or methods
- Redis: operation command of string type data
- Go 1.16.4: manage third-party libraries with Mod
- JS general form submission 1-onsubmit
- Uniapp skills - dom display and hiding
- Analysis of the characteristics of page owner
- Go language web development series 26: Gin framework: demonstrates the execution sequence of code when there are multiple middleware
猜你喜欢
[email"/>
Folic acid modified metal organic framework (zif-8) baicalin loaded metal organic framework composite magnetic material (AU- [email
Dlopen() implements dynamic loading of third-party libraries
Mastering the cypress command line options is the basis for truly mastering cypress
FPGA test method takes mentor tool as an example
Comprehensive case of MySQL data addition, deletion, modification and query
Page generation QR code
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
消息订阅与发布
Qt学习17 对话框及其类型
Logback log sorting
随机推荐
QT learning 22 layout manager (I)
Current situation, analysis and prediction of information and innovation industry
How to delete an attribute or method of an object
Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
从零开始的基于百度大脑EasyData的多人协同数据标注
Rasp implementation of PHP
Thrift threadmanager and three monitors
Dlopen() implements dynamic loading of third-party libraries
Metal organic framework MOFs loaded with non steroidal anti-inflammatory drugs | zif-8 wrapped Prussian blue loaded quercetin (preparation method)
Redis:字符串类型数据的操作命令
JVM family - overview, program counter day1-1
Reflection -- basic usage
信创产业现状、分析与预测
Record 405 questions about bank callback post request
RocksDB LRUCache
How to use lxml to judge whether the website announcement is updated
MySQL 数据处理值增删改
Spring cup eight school league
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Common network state detection and analysis tools