GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/151_Reverse_Words_in_a_String.py at master · qiyuangong/leetcode

Expand file tree

/

151_Reverse_Words_in_a_String.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
39 lines (36 loc) · 1.13 KB
/

151_Reverse_Words_in_a_String.py

Copy path

File metadata and controls

39 lines (36 loc) · 1.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class Solution(object):
# def reverseWords(self, s):
# """
# :type s: str
# :rtype: str
# """
# # O(n) and O(n) space
# if len(s) == 0:
# return s
# temp = s.split(' ')
# temp = [t for t in temp if len(t) > 0]
# temp = reversed(temp)
# return ' '.join(temp)
def reverseWords(self, s):
# remove tail space
s = s.strip(' ')
array_s = []
last = ' '
# remove multiple spaces
for i in range(len(s)):
if s[i] != ' ':
array_s.append(s[i])
else:
if last != ' ':
array_s.append(s[i])
last = s[i]
array_s = array_s[::-1]
ls, pos = len(array_s), 0
for i in range(ls + 1):
if i == ls or array_s[i] == ' ':
self.reverse(array_s, pos, i)
pos = i + 1
return ''.join(array_s)
def reverse(self, array_s, begin, end):
for i in range((end - begin) / 2):
array_s[begin + i], array_s[end - i - 1] = array_s[end - i - 1], array_s[begin + i]
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.