Jaccorot 日事日毕日清日高

ARTS-002

2020-06-05

一、Algorithm

167.Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Note:

Your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.
Example:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

class Solution:
    # my answer
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        set_numbers = sorted(list(set(numbers)))
        t = [[i, j] for i in set_numbers for j in set_numbers if i+j == target][0]
        if t and t[0] != t[1]:
            return list(map(lambda x: numbers.index(x) + 1, t))
        else:
            return [numbers.index(t[0]) + 1, numbers.index(t[0]) + 2]

    # top answer1
    # two-pointer
    def twoSum1(self, numbers, target):
        l, r = 0, len(numbers)-1
        while l < r:
            s = numbers[l] + numbers[r]
            if s == target:
                return [l+1, r+1]
            elif s < target:
                l += 1
            else:
                r -= 1
    # top answer2
    # dictionary           
    def twoSum2(self, numbers, target):
        dic = {}
        for i, num in enumerate(numbers):
            if target-num in dic:
                return [dic[target-num]+1, i+1]
            dic[num] = i

二、Review

《I Thought I Was Mastering Python Until I Discovered These Tricks》

b, a = a, b
  • 逗号是构建元组的语法
  • 先执行右侧,进行元组创建(tuple packing)
  • 再执行左侧,元组赋值(tuple unpacking)

三、Tip

any

any(x):如果可迭代对象iterables中任意存在每一个元素为True则返回True。

特例:若可迭代对象为空,比如空列表[],则返回False。

伪代码(内置的any是由C写的)

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

all

all(x):如果可迭代对象iterables中所有元素都为True则返回True。

特例:若可迭代对象为空,比如空列表[],则返回True。

伪代码(内置的any是由C写的)

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

举例:

>>> any('123')
True
>>> any([0,1])
True
>>> any([0,'0',''])
True
>>> any([0,''])
False
>>> any([0,'','false'])
True
>>> any([0,'',bool('false')])
True
>>> any([0,'',False])
False
>>> any(('a','b','c'))
True
>>> any(('a','b',''))
True
>>> any((0,False,''))
False
>>> any([])
False
>>> any(())
False
>>> any({})
False
>>> all(['a', 'b', 'c', 'd'])  #列表list,
True
>>> all(['a', 'b', 'c', 'd'])  #列表list,元素都不为空或0
True
>>> all(['a', 'b', '', 'd'])  #列表list,存在一个为空的元素
False
>>> all([0, 1,2, 3])  #列表list,存在一个为0的元素
False
>>> all(('a', 'b', 'c', 'd'))  #元组tuple,元素都不为空或0
True
>>> all(('a', 'b', '', 'd'))  #元组tuple,存在一个为空的元素
False
>>> all((0, 1,2, 3))  #元组tuple,存在一个为0的元素
False
>>> all([]) # 空列表
True
>>> all(()) # 空元组
True
>>> all({})
True
>>> #注意:空元组、空列表返回值为True,这里要特别注意
>>> #注意:空元组、空列表返回值为True,这里要特别注意
>>> #注意:空元组、空列表返回值为True,这里要特别注意

>>> all(('', '', '', ''))  #元组tuple,全部为空的元素
False
>>> all('')
True
>>> #如果all(x)参数x对象的所有元素不为0、''、False或者x为空对象,则返回True,否则返回False
>>> 

四、Share

任何岗位都是为解决业务问题而存在的一种阶段性的、组织层面的解决方案

所以不应该只思考当前岗位要求你做什么,更应该思考当前岗位是为了解决什么问题而存在


Similar Posts

上一篇 ARTS-001

下一篇 ARTS-003