Nuggets team number online, help you Offer impromptu! Click on theCheck the details

preface

The first time to participate in the Nuggets punch card activity, among other things, mainly rush to reward. 4.12 Start to rush to achieve the small goal of question 14!! This is problem number nine.

Topic describes

Title link: The longest public prefix

For the title description, I mainly use the screenshot leetcode, so the title is as follows

Thought analysis

The prefix must be a common subset of all items in the array including and indexOf

1. Special case: return ” if there is an empty array, return ” if there are children.

2. Take out the first item and cut through it to determine whether the cut string contains all items

The first item is to start from the beginning. Change the includes to indexOf===0 and be the first one

And it turns out that that satisfies the condition

AC code

/** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { if(! strs.length||strs.includes('')){ return '' } let str1 = strs[0] let len = str1.length let result='' for(let i=0; i<len; i++){ let str2 = str1.slice(0,i+1) let flag=strs.every(item=>{ return item.indexOf(str2)===0 }) if(flag){ result=str2 } } return result };Copy the code

conclusion

Persistence is victory. Problem 9 algorithm complete, persistence is victory!!

Left left left

→ Algorithm series link ←

Write write write

You can order it here! You can order it here! You can order it here!