Skip to content

Commit

Permalink
345
Browse files Browse the repository at this point in the history
  • Loading branch information
RealHacker committed Apr 23, 2016
1 parent ac23e98 commit f327df3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
63 changes: 63 additions & 0 deletions 341_flatten_nested_list_iterator/flatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger(object):
# def isInteger(self):
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# :rtype bool
# """
#
# def getInteger(self):
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# :rtype int
# """
#
# def getList(self):
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# :rtype List[NestedInteger]
# """

class NestedIterator(object):

def __init__(self, nestedList):
"""
Initialize your data structure here.
:type nestedList: List[NestedInteger]
"""
self.list = self.flatten(nestedList)
self.index = 0

def flatten(self, nestedList):
result = []
for item in nestedList:
if item.isInteger():
result.append(item.getInteger())
else:
result += self.flatten(item.getList())
return result

def next(self):
"""
:rtype: int
"""
if self.index >= len(self.list):
return None
v = self.list[self.index]
self.index += 1
return v

def hasNext(self):
"""
:rtype: bool
"""
return self.index < len(self.list)

# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())
17 changes: 17 additions & 0 deletions 345_reverse_vowels/vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def reverseVowels(self, s):
"""
:type s: str
:rtype: str
"""
vowels = "aeiou"
poses = []
for i,c in enumerate(s):
if c.lower() in vowels:
poses.append(i)

chars = [c for c in s]
for k in range(len(poses)/2):
chars[poses[k]], chars[poses[-k-1]] = chars[poses[-k-1]], chars[poses[k]]

return ''.join(chars)

0 comments on commit f327df3

Please sign in to comment.