Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a35a89

Browse files
python: add problem 92 and unittest
1 parent b433b40 commit 3a35a89

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Python/sln_1_100/solution_91_100.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from typing import List
3+
from common_utils import ListNode
34

45

56
class Solution_91_100(object):
@@ -23,6 +24,49 @@ def numDecodings(self, s: str) -> int:
2324
prefix2num[candidate] = count
2425
return prefix2num[s]
2526

27+
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
28+
"""
29+
92
30+
:param head:
31+
:param m:
32+
:param n:
33+
:return:
34+
"""
35+
if m == n:
36+
return head
37+
infix_tail = None
38+
infix_head = None
39+
node = None
40+
idx = 1
41+
while idx < m:
42+
if node is None:
43+
node = head
44+
else:
45+
node = node.next
46+
idx += 1
47+
prefix_tail = node
48+
49+
while idx <= n:
50+
if node is None:
51+
node = head
52+
else:
53+
node = node.next
54+
infix_node = ListNode(node.val)
55+
if infix_head is None:
56+
infix_head = infix_tail = infix_node
57+
else:
58+
infix_node.next = infix_head
59+
infix_head = infix_node
60+
61+
idx += 1
62+
if prefix_tail is None:
63+
new_head = infix_head
64+
else:
65+
new_head = head
66+
prefix_tail.next = infix_head
67+
infix_tail.next = node.next
68+
return new_head
69+
2670
def restoreIpAddresses(self, s: str) -> List[str]:
2771
"""
2872
93

Python/sln_1_100/test_solution_91_100.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from unittest import TestCase
3+
from common_utils import *
34
from sln_1_100.solution_91_100 import Solution_91_100
45

56

@@ -15,6 +16,16 @@ def test_numDecodings(self):
1516
self.assertEqual(self.sln.numDecodings("12"), 2)
1617
self.assertEqual(self.sln.numDecodings("226"), 3)
1718

19+
def test_reverseBetween(self):
20+
ret = self.sln.reverseBetween(build_linked_list([1, 2, 3, 4, 5]), 2, 4)
21+
assert print_linked_list(ret) == '1->4->3->2->5'
22+
23+
ret = self.sln.reverseBetween(build_linked_list([1, 2, 3, 4, 5]), 1, 4)
24+
assert print_linked_list(ret) == '4->3->2->1->5'
25+
26+
ret = self.sln.reverseBetween(build_linked_list([1, 2, 3, 4, 5]), 2, 5)
27+
print_linked_list(ret)
28+
1829
def test_restoreIpAddresses(self):
1930
ret = self.sln.restoreIpAddresses("25525511135")
2031
self.assertEqual(ret, ['255.255.11.135', '255.255.111.35'])

0 commit comments

Comments
 (0)