zoukankan      html  css  js  c++  java
  • 167两数之和II-输入有序数组

    from typing import List
    # 这道题很容易能够想到,只需要遍历两边列表就可以了
    # 两层循环
    class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
    # 第一次遍历列表
    for index1 in range(len(numbers)):
    # 注意这里,如果有重复的值可以直接跳过
    if index1 != 0 and numbers[index1] == numbers[index1 - 1]:
    continue
    # 遍历index1后边的值,与numbers[index1]相加,判断是否等于目标值
    for index2 in range(index1 + 1,len(numbers)):
    if numbers[index1] + numbers[index2] == target:
    return [index1 + 1,index2 + 1]
  • 相关阅读:
    leetcode165
    leetcode63
    leetcode92
    leetcode86
    捣鼓Haskell
    递归操作链表
    treap(堆树)
    贪心策略 — 分数背包
    LeetCode.21
    LeetCode.94
  • 原文地址:https://www.cnblogs.com/cong12586/p/13347755.html
Copyright © 2011-2022 走看看