Skip to content

Update solution #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions solution/013.Roman to Integer/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
r2i={'M':1000,'CM':900,'D':500,'CD':400,'C':100,'XC':90,'L':50,'CL':40,'X':10,'IX':9,'V':5,'IV':4,'I':1}
i=0
result=0
while 1:
if i<=(len(s)-1):
try:
if r2i[s[i:(i+2)]]:
result += r2i[s[i:(i+2)]]
i=i+2
except:
result += r2i[s[i]]
i=i+1
else:
break
return result
28 changes: 28 additions & 0 deletions solution/024.Swap Nodes in Pairs/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if (not head) or (not head.next):
return head
pre=head.next
p=head
q=head.next

while q:
t=q.next
q.next=p
if (not t) or (not t.next):
p.next=t
break
p.next=t.next
p=t
q=p.next
return pre
20 changes: 20 additions & 0 deletions solution/203.Remove Linked List Elements/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def removeElements(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
if head == None:
return None
head.next=self.removeElements(head.next,val)
if head.val == val:
return head.next
else:
return head