leetcode AC1 感受
在网上第一个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
".
- 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 感受的更多相关文章
- 初刷LeetCode的感受
自从上个月进入实验室的云安全项目组后,因为要接触到实际的代码,在实验室博士的建议下我们项目组的硕士开始刷LeetCode练习编程能力,保持每周抽空刷几道算法题.虽然刷的不多,到现在一共只刷了不到30题 ...
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- leetcode刷题总结
题外话 今年大三,现正值寒假时间,开学就开始大三下学期的生活了. 在大三临近结束的时间,也就是复习考试的时间里,我每天都会用早上的时间来刷codewars.刚开始玩的时候,一到8kyu的题目如果稍微难 ...
- leetcode算法题(JavaScript实现)
题外话 刷了一段时间的codewars的JavaScript题目之后,它给我最大的感受就是,会帮助你迅速的提升你希望练习的语言的API的熟悉程度,Array对象.String对象等原生方法,构造函数. ...
- LeetCode 287. Find the Duplicate Number (找到重复的数字)
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- Ring0创建事件Ring3设置事件
同步事件(synchronizationEvent)当事件对象为激发时,如遇到KeWaitForXX等内核函数,事件对象则自动变回未激发态通知事件(NotificationEvent)当事件对象为激发 ...
- word搜狗输入失效切换方法
- UTC时间和普通时间的区别
UTC时间 [root@openstack01 ~]# timedatectl Local time: Sat 2018-08-18 23:04:24 CST Universal ti ...
- [UE4]利用取模运算达到循环遍历数组的目的
X mod Y: 1.X<Y: X mod Y = X.计算记过永远都是等于X 2.X=Y:X mod Y = 0.重新回到数组第一个索引位置
- Nginx开启Gzip详解
最近生产上发生了一些问题,原先所有的静态资源文件都是经过gzip压缩的,然而这几天突然都没有压缩了,经过一顿排查,发现是Nginx的配置有问题,借此机会详细了解了Nginx的Gzip配置. 1. Ng ...
- 在html中用js代替${pagecontext.request.getcontextpath}这样就不用使用jsp了
var pathArray = window.location.pathname.split('/'); var secondLevelLocation = pathArray[1]; var l ...
- 配置ssh服务允许root管理员直接登录
配置ssh服务允许root管理员直接登录 [root@linux-node2 ~]# grep PermitRootLogin /etc/ssh/sshd_config PermitRootLogin ...
- webpack入门详解
webpack入门详解(基于webpack 3.5.4 2017-8-22) webpack常用命令: webpack --display-error-details //执行打包 webpa ...
- windows server 2008通过任务计划程序定时访问网站
1.新一个.bat文件,如: @echo offstart 网址exit 2.打windows server 2008,新建任务计划程序定时访问任务
- Linux使用NFS服务实现远程共享
首先安装 apt install -y nfs-kernel-server nfs-common 编辑配置文件 vim /etc/exports 添加内容: /root/test *(rw,sync, ...