leetcode-Array Operations-python< three >

Source: Internet
Author: User

Previous: Leetcode list add-python< two >

Not much has been done before the algorithm problem, come and go are those loops, so first from the array of simple questions to do.

The biggest experience of the two days:

    • Always poorly considered in the border, leading to errors
    • When dizzy, the variable name is the same as the parameter.
    • Sometimes it's too rough to start thinking about.

Fortunately, all found out the problem and passed. The following sticker code:

Title: Oldest string with no repeating characters

Given a string, find the length of the oldest string that does not contain repeating characters.

Example:

Given "abcabcbb" that the oldest string without repeating characters is "abc" , then the length is 3.

Given "bbbbb" , the longest substring is, the "b" length is 1.

Given "pwwkew" that the eldest string is "wke" , the length is 3. Note that the answer must be a substring , which is a subsequence "pwke" and not a substring .

By code:

classSolution:deflengthoflongestsubstring (self, s):""": Type S:str:rtype:int"""MaxLen=0 tmp=0 D={} Left=0 forJinchRange (len (s)):ifS[J]inchD andd[s[j]]>=Left:left= D[s[j]]+1tmp= J-left + 1MaxLen=Max (Tmp,maxlen) d[s[j]= J#Update the index to the latest even if you repeat it                returnMaxLen
Title: Valid parentheses

Given a string that only includes,,,, ‘(‘ ‘)‘ ‘{‘ ‘}‘ ‘[‘ , ‘]‘ determines whether the string is valid.

Valid strings must satisfy:

    1. The opening parenthesis must be closed with the same type of closing parenthesis.
    2. The opening parenthesis must be closed in the correct order.

Note An empty string can be considered a valid string.

Example 1:

    • Input: "()", Output: True
    • Input: "() []{}", Output: True
    • Input: "(]", Output: false
    • Input: "([)]", Output:false
    • Input: "{[]}", Output:true

By code:

classSolution:defIsleft (self,a):ifa=='(':            return1elifa=='[':            return1elifa=='{':            return1elifa.isspace ():return3Else:            return2defleftstr (self,a):ifa==')':            return '('        elifa==']':            return '['        elifa=='}':            return '{'        Else:            Print("Match Error")                defIsValid (self, s):""": Type S:str:rtype:bool"""Stack= []                forIinchRange (len (s)):ifSelf.isleft (S[i]) ==1: Stack.append (s[i])elifSelf.isleft (S[i]) ==2:                               if0>=Len (stack):returnFalseElse: tmp=Stack.pop ()Print(TMP) left=self.leftstr (s[i])Print(left)iftmp==Left :Pass                    Else:                        returnFalseElse:                Pass                ifLen (Stack) >0:returnFalseElse:            returnTrue

Topic: Deleting array duplicates

Given a sorted array, you need to delete the repeating elements in place so that each element appears only once, returning the new length of the array after removal.

Instead of using extra array space, you must modify the input array in place and complete it with O (1) Extra space.

Example 1:

Given the array nums = [1,1,2], the function should return a new length of 2, and the first two elements of the original array nums are modified to 1, 2. You do not need to consider elements that are beyond the new length in the array.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4], the function should return a new length of 5, and the first five elements of the original array nums are modified to 0, 1, 2, 3, 4. You do not need to consider elements that are beyond the new length in the array.

By code:

classSolution:defremoveduplicates (Self, nums):""": Type Nums:list[int]: Rtype:int"""              if(len (nums) = =0):return0 I=0 forJinchRange (1, Len (nums)):if(nums[j]!=nums[j-1]): I=i+1Nums[i]=Nums[j]returnI+1

To go home, and then write in the evening. The remaining few questions are not posted, the key to solve the idea has not been written.

leetcode-Array Operations-python< three >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.