[Algorithm] Algorithm question-20231129


Insert image description here

1. 15. Sum of three numbers

Tips
Medium
6.5K
Related companies
Give you one Integer array nums, determine whether there is a triplet [nums[i], nums[j], nums[k]] that satisfies i != j, i != k and j != k, and also satisfies nums[i] + nums[j] + nums[k] == 0 . Please
you return all triples whose sum is 0 and are not repeated.
Note: Answers cannot contain duplicate triples.

Example 1:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1 ,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 .
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 .
The different triples are [-1,0,1] and [-1,-1,2] .
Note that the order of the output and the order of the triples is not important.
Example 2:

Input: nums = [0,1,1]
Output: []
Explanation: The only possible sum of triples is not 0 .
Example 3:

Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible The triplet sum is 0.

class Solution:
    def threeSum(self,nums):
        n=len(nums)
        res=[]
        if n<3:
            return []
        nums.sort()

        for i in range(n):
            if nums[i]>0:
                return res
            # if (i > 0 and nums[i] == nums[i - 1]):
            #     continue

            L=i+1
            R=n-1
            while L<R:
                if nums[i]+nums[L]+nums[R]==0:
                    res.append([nums[i],nums[L],nums[R]])
                    while L<R and nums[L]==nums[L+1]:
                        L=L+1
                    while L<R and nums[R]==nums[R-1]:
                        R=R-1
                    L=L+1
                    R=R-1
                elif nums[i]+nums[L]+nums[R]>0:
                    R=R-1
                else:
                    L=L+1
        return res

nums = [-1,0,1,2,-1,-4]
res=Solution()
rrr=res.threeSum(nums)
print(rrr)

2. 205. Isomorphic strings

Simple
Given two strings s and t, determine whether they are isomorphic.
If the characters in s can be replaced to obtain t according to a certain mapping relationship, then the two strings are isomorphic.
Each occurrence of a character should be mapped to another character without changing the order of the characters. Different characters cannot be mapped to the same character, the same character can only be mapped to the same character, and characters can be mapped to themselves.

Example 1:

Import: s = “egg”, t = “add”
Export: true
Example 2:

Input: s = “foo”, t = “bar”
Output: false
Example 3:

Input: s = “paper”, t = “title”
Output: true

from collections import Counter, defaultdict


def isIsomorphic(s, t):
    if not s:
        return True
    dic = {
    
    }
    for i in range(len(s)):
        if s[i] not in dic:
            if t[i] in dic.values():
                return False
            else:
                dic[s[i]] = t[i]
        else:
            if dic[s[i]] != t[i]:
                return False
    return True

3. 383. Ransom letter

Simple

gives you two strings: ransomNote and magazine. Determine whether ransomNote can be composed of the characters in magazine.
Returns true if possible; otherwise returns false .
Each character in magazine can only be used once in ransomNote.

Example 1:

Import: ransomNote = “a”, magazine = “b”
Export: false
Example 2:

Import: ransomNote = “aa”, magazine = “ab”
Export: false
Example 3:

Input: ransomNote = “aa”, magazine = “aab”
Output: true

Problem-solving ideas
1. Traverse the set collection to avoid repeated traversal
2. If the number of times the character i appears in the magazine string is less than the number of times it appears in The number of times in ransomNote will return False
3. When the collection traversal is completed, True will be returned

def canConstruct(ransomNote, magazine):
    ransomNote_set = set(ransomNote)
    for i in ransomNote_set:
        if magazine.count(i) < ransomNote.count(i):
            return False
    return True


ransomNote = "a"
magazine = "b"
print(canConstruct(ransomNote, magazine))

Insert image description here

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/134680938