在网上第一个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

leetcode AC1 感受的更多相关文章

  1. 初刷LeetCode的感受

    自从上个月进入实验室的云安全项目组后,因为要接触到实际的代码,在实验室博士的建议下我们项目组的硕士开始刷LeetCode练习编程能力,保持每周抽空刷几道算法题.虽然刷的不多,到现在一共只刷了不到30题 ...

  2. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  3. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  4. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  5. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. leetcode刷题总结

    题外话 今年大三,现正值寒假时间,开学就开始大三下学期的生活了. 在大三临近结束的时间,也就是复习考试的时间里,我每天都会用早上的时间来刷codewars.刚开始玩的时候,一到8kyu的题目如果稍微难 ...

  7. leetcode算法题(JavaScript实现)

    题外话 刷了一段时间的codewars的JavaScript题目之后,它给我最大的感受就是,会帮助你迅速的提升你希望练习的语言的API的熟悉程度,Array对象.String对象等原生方法,构造函数. ...

  8. LeetCode 287. Find the Duplicate Number (找到重复的数字)

    Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...

  9. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

随机推荐

  1. PowerDesigner最基础的使用方法入门学习(一)

    1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自行学习) 我的PowerDesigner版本是16.5的,如若版本不一样,请自行参考学习即可.(打开软件即是 ...

  2. SCCM 2012 R2实战系列之十三:辅助站点部署

    由于最近几个月一直处于AD升级项目中,很久没有更新SCCM的技术文档了.SCCM 2012中的辅助站点部署方法还是比较特别的,需要注意的地方也非常多,今天跟大家分享辅助站点的具体部署和配置方法. 1. ...

  3. pandas中一列含有多种数据类型的转换:科学计算法转浮点数、字符映射

    import pandas as pd import re def getNum(x): """ 科学计数法和字符转浮点数 """ if r ...

  4. iis ajax post 跨域问题解决

    iis ajax post时会遇到跨域的问题 只需要在IIS中http响应头中增加:Access-Control-Allow-Origin:*,即可解决问题

  5. GIT命令行笔记

    一次常规的初始化+推送: git initgit config user.email "you@example.com"git config user.name "asm ...

  6. ubuntu 16.04 静态ip的配置

    nssa-sensor1@nssa-sensor1:~$ vim /etc/network/interfaces 以下是编辑文件的内容# interfaces(5) file used by ifup ...

  7. python爬虫之登录

    #-*-coding:utf--*- import cookielib, urllib, urllib2 import json import threading,time class Order(o ...

  8. ActiveMQ核心技术

                                        消息中间件技术 一.ActiveMQ 1>生产消息,消费消息流程图

  9. uiautomator 代码记录 :BT接收测试

    package rom; import java.lang.*; import java.util.Random; import java.io.File; import com.android.ui ...

  10. [Unity算法]斜抛运动(变种)

    之前的斜抛运动,如果运动到游戏中,显然是太呆板了,那么可以试着加入一些效果,让它看起来更生动一些,类似游戏中的击飞或者掉落效果: 1.在达到最高点的时间点±X的时间段内,会有“减速”效果,形成一种在空 ...