Fork me on GitHub
Rosevil

Technical reflection, little happiness.


  • 首页

  • 标签

  • 分类

  • 时间轴

  • 关于

  • 秘密基地(/ω\)

  • 站内搜索

86 - 分隔链表

发表于 2018-04-13 | 分类于 LeetCode

题目描述

阅读全文 »

83 - 删除排序链表中的重复元素

发表于 2018-04-13 | 分类于 LeetCode

题目描述

阅读全文 »

II'

发表于 2018-04-13

61 - 旋转链表

发表于 2018-04-13 | 分类于 LeetCode

题目描述

阅读全文 »

双指针

发表于 2018-04-13 | 分类于 算法思想

一、链表

1. 链表的环

判断是否存在环:

  1. 多个结点的后继指针重复;
  2. 使用两个移动速度不同的指针,若相遇则存在环。循环条件(fast != None) && (fast.next != None)

寻找环入口:

  1. 快慢指针相遇出就是环的入口;
  2. fast指针的移动速度为slow指针的两倍。

2. 移除重复元素

  1. 指针1指向不重复的最后一个结点;
  2. 指针2依次扫描,若不重复则更新指针1,否则移除。

3. 倒数第n个结点

  1. fast指针先走n步;
  2. fast与slow指针同时以步长 为1的速度向前走;
  3. fast走到链表尾时slow指针指向倒数第n个结点。

4. 拆分链表

二、数组

寻找连续子序列

使用start,end两个指针记录下标

2.移除重复元素

  1. 指针1指向不重复的最后一个元素;
  2. 指针2依次扫描,若不重复则更新指针1,否则移除。

3. 寻找中点或中位数

两个指针由两端向中间移动。

Error集锦

发表于 2018-04-12 | 分类于 小结

1. 缩进错误

  1. IndentationError: unindent does not match any outer indentation level
  2. TabError: inconsistent use of tabs and spaces in indentation

这个错误就像全角半角符号一样让人抓狂,要是想整死一个程序员,就悄悄把他代码里的半角字符换成全角的,他就会开始漫长又愉快的debug~
你永远不知道自己什么时候会出现缩进错误,即使你认为所有地方都使用了tab键ヽ(`Д´)ノ︵ ┻━┻ ┻━┻

解决办法

  1. 老老实实调整,把报错那行重新缩进,确定使用tab 【偶尔有效】
  2. 好好看看sublime右下角,怎么是space啊喂! 点击这里,变成tab,文档里的空格就自动变成tab了,你会看到有些小地方闪了一下,就是那些地方的空格让你出错/微笑。

链表

发表于 2018-04-12 | 分类于 数据结构

一、单链表

1. 结点类

1
2
3
4
class ListNode:
def __init__(self, n):
self.val = n
self.next = None

2. 单链表类

实现基本操作:

  1. 初始化
  2. 判断是否为空
  3. 清空链表
  4. 取得链表长度
  5. 在链表末尾添加结点
  6. 在指定位置添加结点
  7. 删除指定位置的结点
  8. 替换指定位置结点值
  9. 查找指定值所在结点位置
    注:[下标均以0开始]

使用表头结点:
表头结点是链表的第一个结点,除了值被忽略以外和其它结点一致,但它不被当做链表中的实际元素。
头结点的存在使得空链表与非空链表处理一致,也方便对链表的开始结点的插入或删除操作。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class SinLinkedList:
def __init__(self):
self.head = ListNode(None)

# 是否为空
def isEmpty(self):
return self.length() == 0

# 清空
def clear(self):
self.head = ListNode(None)

# 链表长度
def length(self):
cnt = 0
curr = self.head
while curr.next is not None:
cnt += 1
curr = curr.next
return cnt

# 在链表末尾添加结点
def append(self, val):
node = ListNode(val)
if self.head is None:
self.head = node
else:
p = self.head
while p.next:
p = p.next
p.next = node

# 在指定位置添加结点
def insert(self, idx, value):
if self.isEmpty():
print('链表为空')

p = self.head
new = ListNode(value)
while p and idx > 0:
p = p.next
idx -= 1
# 说明没有那么多结点
if idx > 0:
print('超出链表长度')
else:
q = p.next
p.next = new
new.next = q

# 删除指定位置的结点
def delete(self, idx):
if self.isEmpty():
print('链表为空')

p = self.head
while p and idx > 0:
p = p.next
idx -= 1
# 说明没有那么多结点
if idx > 0:
print('超出链表长度')
else:
p.next = p.next.next

# 替换指定位置结点值
def replace(self, idx, newValue):
if self.isEmpty():
print('链表为空')

p = self.head
new = ListNode(newValue)
while p and idx > 0:
p = p.next
idx -= 1
# 说明没有那么多结点
if idx > 0:
print('超出链表长度')
else:
q = p.next.next
p.next = new
new.next = q

# 查找第idx个结点值
def search(self, idx):
if self.isEmpty():
print('链表为空')
return
p = self.head
while p.next and idx > 0:
p = p.next
idx -= 1
if idx > 0:
print('超出链表长度')
else:
return p.val

# 查找指定值所在结点位置
def index(self, value):
if self.isEmpty():
print('链表为空')
return
p = self.head
idx = 0
while p.next and not p.val == value:
p = p.next
idx += 1
return idx - 1

3. 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
l = SinLinkedList()
l.append(12)
l.append(233)
l.insert(1, 666)
l.replace(1, 6666)
l.delete(1)
print(l.index(12))
print(l.search(1))

h = l.head.next
while h:
print(h.val)
h = h.next

二、单向循环链表

特点:从表中任意结点出发均可找到链表中其它结点,查找效率高。

1. 节点类-将结点属性设为私有变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class ListNode:
def __init__(self, n):
self.__val = n
self.__next = None

def __getVal__(self):
return self.__val

def setVal(self, newVal):
self.__val = newVal

def getNext(self):
return self.__next

def setNext(self, newNext):
self.__next = newNext

2.单向循环链表类

实现基本操作:

  1. 初始化
  2. 判断是否为空
  3. 清空链表
  4. 取得链表长度
  5. 在头结点后插入结点:头插法
  6. 删除指定值的结点
  7. 替换指定结点值
  8. 查找指定结点并返回下标,不存在返回-1
    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    class SinCycLinkedList:
    def __init__(self):
    self.head = ListNode(None)
    self.head.setNext(self.head)

    # 判断是否为空
    def isEmpty(self):
    return self.head.getNext() == self.head

    # 清空链表
    def clear(self):
    self.head.setNext(self.head)

    # 取得链表长度
    def length(self):
    cnt = 0
    curr = self.head.getNext()
    while curr != self.head:
    curr = curr.getNext()
    cnt += 1
    return cnt

    # 在头结点后插入结点:头插法
    def unshift(self, val):
    new = ListNode(val)
    new.setNext(self.head.getNext())
    self.head.setNext(new)

    # 删除指定值的结点
    def delete(self, val):
    prev = self.head
    while prev.getNext() != self.head:
    curr = prev.getNext()
    if curr.getVal() == val:
    prev.setNext( curr.getNext() )
    prev = prev.getNext()

    # 替换指定结点值
    def raplce(self, val, newVal):
    prev = self.head
    while prev.getNext() != self.head:
    curr = prev.getNext()
    if curr.getVal() == val:
    curr.setVal(newVal)
    prev = prev.getNext()

    # 查找指定结点并返回下标,不存在返回-1
    def search(self, val):
    idx = 0
    curr = self.head.getNext()
    while curr != self.head:
    if curr.getVal() == val:
    return idx
    idx += 1
    return -1

3. 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
l = SinCycLinkedList()
l.unshift(111)
l.unshift(222)
l.unshift(666)
l.raplce(111, 233)
l.delete(222)
print('666 is in: ', l.search(666))
# l.clear()s
print('length: %d' % l.length())

print('\nitems:')
h = l.head
while h.getNext() != l.head:
print(h.getNext().getVal())
h = h.getNext()

三、双指针

链表的环

判断是否存在环:

  1. 多个结点的后继指针重复;
  2. 使用两个移动速度不同的指针,若相遇则存在环。循环条件(fast != None) && (fast.next != None)

寻找环入口:

  1. 快慢指针相遇出就是环的入口;
  2. fast指针的移动速度为slow指针的两倍。

寻找连续子序列

  1. 使用start,end两个指针记录下标

移除重复元素

倒数第n个结点

拆分链表

25 - 每k个一组翻转链表

发表于 2018-04-11 | 分类于 LeetCode

题目描述

阅读全文 »

24 - 交换相邻结点

发表于 2018-04-11 | 分类于 LeetCode

题目描述

阅读全文 »

23 - 合并K个元素的有序链表

发表于 2018-04-11 | 分类于 LeetCode

题目描述

阅读全文 »
12…4
rosevil1874

rosevil1874

The meaning of life is every single second.

36 日志
7 分类
8 标签
RSS
GitHub CSDN
Creative Commons
© 2017 — 2018 rosevil1874
本站访客数:
|
博客全站共14.7k字