国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

leetcodeWordBreakpython

2019-11-14 17:09:16
字體:
來源:轉載
供稿:網友

Word Break

 

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

 

python code:

class Solution:
# @param s, a string
# @param wordDict, a set<string>
# @return a boolean
def wordBreak(self, s, wordDict):
  if not s or not wordDict:    #邊界條件處理
    return False
  d=[0]              #這個list用來記錄前多少個字符可以匹配到,如[0,2,4,6],表示前0,2,4,6個字符構成的子串可以成功匹配
  for i in range(1,len(wordDict)+1):  #檢驗s,從第一個字符到第一個字符直至第一個字符到最后一個字符的子串是否匹配
    for j in range(len(d)-1,-1,-1):  #s的前i個字符構成的子串怎樣才算匹配?上一個匹配的位置到i構成的子串加上前面某個能匹配的子串后可以

                     #  在wordDict中找到
      if s[d[j]:i] in wordDict:
        d.append(i)
        break
  return len(s)==d[-1]           #如果長為len(s)的子串能匹配成功,那么return True


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定陶县| 乌苏市| 康马县| 广水市| 苍山县| 西乌珠穆沁旗| 新民市| 东乡族自治县| 丹巴县| 宁城县| 芦山县| 信宜市| 广水市| 龙海市| 株洲县| 曲水县| 凌云县| 永吉县| 明溪县| 搜索| 信丰县| 安宁市| 昔阳县| 乳山市| 柯坪县| 通河县| 鹤峰县| 合肥市| 九江县| 视频| 彝良县| 得荣县| 鄂伦春自治旗| 海伦市| 图木舒克市| 黄龙县| 改则县| 龙山县| 颍上县| 新建县| 阿图什市|