在网上第一个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.
  1. class Solution:
  2.  
  3. # @return a string
  4.  
  5. def reverseWords(self, s):
  6. #s = raw_input("input string \n")
  7. #s = "a blue sky"
  8. s = s.strip() #clean the space in head and end
  9. #print s
  10. b = [] # save localtion of space
  11. a = ''
  12. i = 0 # index
  13. for j in s :
  14. if s[i] == " ":
  15. b.append(i)
  16. i = i + 1
  17. if b == []: #only one word
  18. return s
  19. #print b
  20. #print len(b)
  21. #print s[:2]+s[2:6]+s[6:]
  22. i = 0
  23.  
  24. for j in b[len(b)::-1]:
  25. # print j,
  26. # print i
  27. if i == 0: #the last word (begin)
  28. a=s[j+1:].strip()
  29. #print a
  30. elif j ==b[0]: #the first word (end)
  31. a = a.strip() +" "+s[j+1:i].strip()# +" " + s[:j].strip()
  32. a = a.strip() + " "+ s[:j].strip()
  33. #print a
  34. else:
  35. a = a.strip() +" " +s[j+1:i].strip()
  36. #print a
  37. if len(b) == 1: #only one space
  38. a = s[j+1:]+ " "+ s[:j]
  39.  
  40. #print a
  41. i = j
  42.  
  43. # print j,i
  44. 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. Ring0创建事件Ring3设置事件

    同步事件(synchronizationEvent)当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则自动变回未激发态通知事件(NotificationEvent)当事件对象为激发 ...

  2. word搜狗输入失效切换方法

  3. UTC时间和普通时间的区别

            UTC时间 [root@openstack01 ~]# timedatectl Local time: Sat 2018-08-18 23:04:24 CST Universal ti ...

  4. [UE4]利用取模运算达到循环遍历数组的目的

    X mod Y: 1.X<Y: X mod Y = X.计算记过永远都是等于X 2.X=Y:X mod Y = 0.重新回到数组第一个索引位置

  5. Nginx开启Gzip详解

    最近生产上发生了一些问题,原先所有的静态资源文件都是经过gzip压缩的,然而这几天突然都没有压缩了,经过一顿排查,发现是Nginx的配置有问题,借此机会详细了解了Nginx的Gzip配置. 1. Ng ...

  6. 在html中用js代替${pagecontext.request.getcontextpath}这样就不用使用jsp了

    var pathArray = window.location.pathname.split('/');  var secondLevelLocation = pathArray[1];  var l ...

  7. 配置ssh服务允许root管理员直接登录

    配置ssh服务允许root管理员直接登录 [root@linux-node2 ~]# grep PermitRootLogin /etc/ssh/sshd_config PermitRootLogin ...

  8. webpack入门详解

    webpack入门详解(基于webpack 3.5.4  2017-8-22) webpack常用命令: webpack --display-error-details    //执行打包 webpa ...

  9. windows server 2008通过任务计划程序定时访问网站

    1.新一个.bat文件,如: @echo offstart 网址exit 2.打windows server 2008,新建任务计划程序定时访问任务

  10. Linux使用NFS服务实现远程共享

    首先安装 apt install -y nfs-kernel-server nfs-common 编辑配置文件 vim /etc/exports 添加内容: /root/test *(rw,sync, ...