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 five

Topic describes

Title link: the oldest string without repeating characters

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

Thought analysis

A string is the longest continuous string that has no repetition. A string is the longest continuous string that has no repetition

Then cut s into an array to iterate to get the length of the string without repetition each time, and then get the maximum length value at the end of the iterate. To achieve the following

Clicking on the test was awkward and timed out, so we need to optimize itAt that time did not think of a good optimization method (frustration), and then chose Baidu,Refer to the linkThe final test passed, found several methods to solve the sliding window or array + double pointer violence solution and so on

AC code

/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var lengthOfLongestSubstring = function(s)  { let arr = []; let m = 0 for(let i = 0; i < s.length; i++) { let index = arr.indexOf(s[i]) if(index ! == -1) { arr.splice(0, index+1); } arr.push(s[i]) m = Math.max(arr.length, m) } return m };Copy the code

conclusion

After reading others’ solutions, I feel that I know nothing about algorithms, nor about the optimization of spatial complexity and time complexity. I will never stop learning, and I hope I can develop the habit of brushing questions. Then I believe I will make progress. Today’s solution is based on others more hands, more thinking. The fifth algorithm is half 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!