2014年1月14日

LeetCode: NQueens

摘要: n皇后问题。用的是back tracking以及递归的方法。这个算法的时间复杂度为O(n^2).首先确定第一行的皇后放在什么位置。(需要遍历这个皇后所有可以放的位置)。就是说第一个皇后可以在第一行的任一位置。然后遍历第二行的皇后放在哪个位置,如果满足条件就接着放第三个,否则移到下一位置。。。以此类推当放置完最后一个时,返回结果。注意的问题是ArrayList是引用传递,所以可以把所有值都加在里面。 1 public ArrayList solveNQueens(int n) { 2 int[] result = new int[n]; 3 ArrayList a... 阅读全文

posted @ 2014-01-14 13:40 longhorn 阅读(280) 评论(0) 推荐(0) 编辑

LeetCode: Word Break & Word Break II

摘要: Word BreakGiven a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"lee 阅读全文

posted @ 2014-01-14 13:24 longhorn 阅读(183) 评论(0) 推荐(0) 编辑

导航