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++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- TextView-- 测量文字宽度
https://my.oschina.net/lengwei/blog/637380; http://blog.csdn.net/mare_blue/article/details/51388403; ...
- (转)TP-LINK WR720N v3 刷OpenWrt
之前买了一台改过硬件的TP-Link WR841N-V7路由器,并且成功刷机OpenWrt也完成了FQ,WR841N-V7的更多详情可以看这里,但是可能卖家焊接的有问题,导致老是听到滋滋滋高频率的赤耳 ...
- CentOS之Shell文件编写基础
shell文件以.sh结尾,这是一种习惯而已.第一行以#! /bin/bash开头:表示该文件使用的是bash语法: 如果不设置该行,你的shell脚本也可以执行,但是不符合规范.#表示注释. # v ...
- U3D学习003——编辑器使用
1.skybox 原来的render setting 在2017版本中是lighting标签environment中设置: 或者在摄像机对象中添加skybox组件,进行设置. 2.6张图实现自定义sk ...
- C语言:结构体,共用体
结构体: 一个变量,存储不同类型的数据项共用体:一个变量,存储不同类型的数据项,相同的内存位置,存储不同的数据类型 #include <stdio.h> #include <stri ...
- RecyclerView.Adapter封装,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;
之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法: 现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装: Base ...
- windows下git的使用
1.安装git 下载地址:https://git-scm.com/download/win
- Java之ConcurrentHashMap
由于工作中使用到了ConcurrentHashMap,然后查了一波资料,最后整理如下: 1. 描述: ConcurrentHashMap是在Java1.5作为HashTable的替代选择新引入的,是c ...
- thinkphp 5 wherein
$details = Db::name('food') -> alias('a') -> field('a.food_code,a.food_name,a.food_u1,a.food_p ...
- 1.如何在Cloud Studio上执行Python代码?
1.在python文件下新建python文件,输入文件名后按Enter键生成,比如: one.py . 2.简单输入python代码: print "haha" 3.打开左下角的终 ...