首页 > 其他 > 详细

Leetcode刷题笔记(部分非原创)(131-140题)

时间:2015-02-20 08:36:08      阅读:180      评论:0      收藏:0      [点我收藏+]

139. Word Break

     leetcode链接:https://oj.leetcode.com/problems/word-break/

     DP的思想。设置boolean数组存储,canBreak[i]表示0~i的字符是否满足

public boolean wordBreak(String s, Set<String> dict) {
     int length = s.length();
     boolean[] canBreak = new boolean[length];
     for(int i = 0; i < length; i++) {
          canBreak[i] = dict.contains(s.substring(0, i + 1));
          if(canBreak[i]) {
               continue;
          }
          for(int j = 0; j < i; j++) {
               if(canBreak[j] && dict.contains(s.substring(j + 1, i + 1))) {
                      canBreak[i] = true;
                      break;
               }
          }
    }  
    return canBreak[length - 1];
}

 

 
 

Leetcode刷题笔记(部分非原创)(131-140题)

原文:http://www.cnblogs.com/jianghewade/p/4296396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!