在网上第一个AC还是蛮高兴的,之前试了不少练习编程的方法,感觉不怎么适合自己,在OJ上做题的确是一个能够锻炼的方法。 之前一直研究学习的方法,往简单的说是认知、练习,往复杂的说是,保持足够input,input内容足够narrow,难度适合,逐渐+i ,总结并输出。

  不过第一次成功很高兴,leetcode不愧是入门级,适合没怎么自己写过代码的人练手。

题目:地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

click to show clarification.

Clarification:

 

  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

 

 

class Solution:
    
    # @return a string
    
    def reverseWords(self, s):
        #s = raw_input("input string \n")
        #s = "a blue sky"
        s = s.strip() #clean the space in head and end
        #print s
        b = [] # save localtion of space
        a = ''
        i = 0  # index
        for j in s :
            if s[i] == " ":
                b.append(i)
            i = i + 1
        if b == []: #only one word
            return s
        #print b
        #print len(b)
        #print s[:2]+s[2:6]+s[6:]
        i = 0

        for j in b[len(b)::-1]:
        #    print j,
        #    print i 
            if i == 0:  #the last word (begin)
                a=s[j+1:].strip()
                #print a
            elif j ==b[0]: #the first word (end)
                a = a.strip() +" "+s[j+1:i].strip()# +" " + s[:j].strip()
                a = a.strip() + " "+ s[:j].strip()
                #print a
            else:
                a = a.strip() +" " +s[j+1:i].strip()
                #print a
            if len(b) == 1: #only one space
                a = s[j+1:]+ " "+ s[:j]
        
            #print a
            i = j
        
            #    print j,i
        return a

代码写的丑的很,其实本身内容比较好实现,但是由于不熟悉oj且不了解python,故写的磕磕巴巴,甚至内容感觉就不怎么符合软件工程的易读性,写下来以免将来忘掉。

我的想法就是,1、记录原字符串的空格的位置;2、以空格位置为索引倒着输出s

 

先说收获:

  1、略微熟悉了python的循环的运作,有时候感觉还是挺难用的,脑海还是有c的想法,这个无所谓吧,语言特性这个玩意现在还没有功夫去研究

  2、 str 和 list 都可以进行切片,s[i:j] 是表示的从i到j,但是s[j]并不包括在里面

  s1[0:5:2]          # 从下标0到下标4 (下标5不包括在内),每隔2取一个元素 (下标为0,2,4的元素)

  s1[2:0:-1]         #从下标2到下标1

  从上面可以看到,在范围引用的时候,如果写明上限,那么这个上限本身不包括在内。

  尾部元素引用

  s1[-1]             # 序列最后一个元素

  s1[-0]             # 就是s1[0]

  + 连接字符串;相邻的两个字符串文本自动连接在一起   'str' 'ing' # 'string' ; str.strip()可以把字符串头和尾的多余空格删掉

再说教训:

  1、对python和oj本身不熟悉

  2、考虑边界情况很少

  3、变量命名非常随意,以后肯定会看不懂

  4、if 和elsif 是不能同时运作的

  5、循环只有一遍的时候,出现了问题,不得不加上最后的if,算不算是另一种边间没考虑好?(分别是:输入内容本身的复杂、循环的边界问题)

最后说感受:

  估计是看python代码太少,现在对于编程也不能说是一种入门的感觉

参考:

  http://www.pyth(去掉)ontab.com/html/pythonshouce27/controlflow.html#tut-functions

  http://www.python(去掉)tab.com/html/pythonshouce27/controlflow.html#if

  http://www.pytho(去掉)ntab.com/html/pythonshouce27/introduction.html#tut-strings

  http://www.cnblogs.com/vamei/archive/2012/05/28/2522677.html

 

 

posted on 2014-07-26 20:20  混沌奇迹  阅读(327)  评论(0编辑  收藏  举报