美文网首页
Leetcode Day1

Leetcode Day1

作者: 清川kiyokawa | 来源:发表于2018-09-17 17:40 被阅读0次

NO1. Two Sum:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

1. 遇到一个报错:non-default argument follows default argument

Solution: 把含有默认值的参数放在了不含默认值的参数的前面,调换参数的位置即可

2. 所用方法:

enumerate: 用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中

NO206. Reverse Linked List

主要思想是使用current,current.next,prev等指针。

current首先指向head节点,前没有节点,prev = None。

循环内首先使用temp指针指向current.next方便之后移动指针。然后令current.next指向prev的位置,prev指向current的位置,最后令current指向temp

原: prev current current.next(temp)

现:current.next prev current(temp)

这种变化可以让每一个当前链表元素插入到链表的头部位置

相关文章

网友评论

      本文标题:Leetcode Day1

      本文链接:https://www.haomeiwen.com/subject/yvmknftx.html