最近动态

Web技术

两数相加问题.md

给定两个非空链表来代表两个非负数,位数按照逆序方式存储,它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

1
2
3
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

思路非常清晰,首先两个链表长度得一致,同时逆序操作,其次返回链表时也为逆序,首先输出的📖链表头两个元素之和。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""

head = ListNode((l1.val + l2.val) % 10)
current = head
carry = (l1.val + l2.val) // 10
while l1.next is not None or l2.next is not None:
num1 = 0
num2 = 0
if l1.next is not None:
l1 = l1.next
num1 = l1.val
if l2.next is not None:
l2 = l2.next
num2 = l2.val
current.next = ListNode((num1 + num2 + carry) % 10)
carry = (num1 + num2 + carry) // 10
current = current.next
if carry != 0:
current.next = ListNode(carry)
return head

给定一个整数数列,找出其中和为特定值的那两个数。

你可以假设每个输入都只会有一种答案,同样的元素不能被重用。

示例:

1
2
3
4
5

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

使用dict

1
2
3
4
5
6
7
8
9
10

class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
dict = {}
for i in xrange(len(num)):
x = num[i]
if target-x in dict:
return (dict[target-x]+1, i+1)
dict[x] = i

阅读剩下更多

默认配图
Web技术

python.enumerate用法

python中的enumerate语句

enumerate()用法说明

  • enumerate()用于遍历序列中的元素以及他们的下标。
  • enumerate()是python的内置函数。
  • enumerate的意思是枚举,列举的意思。
  • 对于一个可迭代的或者可遍历的对象,enumerate将其组成一个索引序列,利用它同时获得索引和值
  • enumerate多由于for语句中得到计数。

enumerate()的语法和用法

  • 语法
    enumerate(Iterable,start)

    • 语句中的第一个元素Iterable代表我们要进行遍历的序列(列表,字典,元组,……)。
    • 语句中的第二个元素start代表索引的起始值,默认是0。
  • 如果对一个列表,既要遍历索引又要遍历元素时,可以这样写:

    1
    2
    3
    4
    5
    6
    7
    8
    list1 = ["这", "是", "一个", "测试"]
    for index, item in enumerate(list1):
    print index, item
    >>>
    0 这
    1 是
    2 一个
    3 测试
  • enumerate还可以接收第二个参数,用于指定索引起始值,如:

    1
    2
    3
    4
    5
    6
    7
    8
    list1 = ["这", "是", "一个", "测试"]
    for index, item in enumerate(list1, 1):
    print index, item
    >>>
    1 这
    2 是
    3 一个
    4 测试

enumerate()例子

1
2
3
4
5
>>> L=[1,2,3,4,5]
>>> list(enumerate(L))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
>>> list(enumerate(L,4))
[(4, 1), (5, 2), (6, 3), (7, 4), (8, 5)]
  • 如果要统计文件的行数,可以这样写,速度会更快:
    1
    2
    3
    count = 0
    for index, line in enumerate(open(filepath,'r')):
    count += 1

阅读剩下更多

默认配图
未分类

Hello World

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

阅读剩下更多

默认配图
返回顶部