吐槽swift

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.

For “(()”, the longest valid parentheses substring is “()”, which has length = 2.

Another example is “)()())”, where the longest valid parentheses substring is “()()”, which has length = 4.

在刷leetcode这道题,本想用swift练练手,但是遇到了非常麻烦的地方,就是如果我要遍历一个字符串,swift提供的substring方法中需要用Index类型,如:

let s = "abcdefg" 
let start = s.index(s.startIndex, offsetBy: 1) // 获取第一个索引后一个的索引
let end = s.index(start, offsetBy: 2) 
let sub = s[start..<end] //bc

是不是用起来很麻烦,如果是java,直接substring(1,3)就行了.这样一想又想用java写了,毕竟leetcode很多地方都是要操纵string的,java在这方面优势巨大.

而且swift还有很不适合刷leetcode的地方,你会记得那个方法里面的offsetBy吗?真是一款活在xcode里的语言,想到汤哥哥上课说的,大意是开源公司的代码不会给你很多限制,商业的会对你的编辑器限制很多.

等我做完了在更新这一篇吧…