📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2319| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

Permutations 和 Word ladder 和 Word break

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
大家这三道题有什么好的解法吗。自己做了和看了网上的答案,感觉都不是很好地能记下来,很容易忘


Permutations:
  1. public class Solution {
  2.     public ArrayList<ArrayList<Integer>> permute(int[] nums) {
  3.         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
  4.         
  5.         result.add(new ArrayList<Integer>());
  6.         
  7.         for(int i = 0; i < nums.length; i++)
  8.         {
  9.             ArrayList<ArrayList<Integer>> curr = new ArrayList<ArrayList<Integer>>();
  10.             for(ArrayList<Integer> l : result)
  11.             {
  12.                 for(int j = 0; j < l.size()+1; j++)
  13.                 {
  14.                     l.add(j, nums[i]);
  15.                     ArrayList<Integer> temp = new ArrayList<Integer>(l);
  16.                     curr.add(temp);
  17.                     l.remove(j);
  18.                 }
  19.             }
  20.             result = new ArrayList<ArrayList<Integer>>(curr);
  21.         }
  22.         return result;
  23.     }
  24. }
复制代码
Word ladder:
  1. public class Solution {
  2.     public int ladderLength(String beginWord, String endWord, Set<String> wordDict) {
  3.         if (wordDict.size() == 0)
  4.                 return 0;

  5.         wordDict.add(endWord);

  6.         LinkedList<String> wordQueue = new LinkedList<String>();
  7.         LinkedList<Integer> distanceQueue = new LinkedList<Integer>();

  8.         wordQueue.add(beginWord);
  9.         distanceQueue.add(1);

  10.         //track the shortest path
  11.         int result = Integer.MAX_VALUE;
  12.         while (!wordQueue.isEmpty()) {
  13.                 String currWord = wordQueue.pop();
  14.                 Integer currDistance = distanceQueue.pop();

  15.                 if (currWord.equals(endWord)) {
  16.                         result = Math.min(result, currDistance);
  17.                 }

  18.                 for (int i = 0; i < currWord.length(); i++) {
  19.                         char[] currCharArr = currWord.toCharArray();
  20.                         for (char c = 'a'; c <= 'z'; c++) {
  21.                                 currCharArr[i] = c;

  22.                                 String newWord = new String(currCharArr);
  23.                                 if (wordDict.contains(newWord)) {
  24.                                         wordQueue.add(newWord);
  25.                                         distanceQueue.add(currDistance + 1);
  26.                                         wordDict.remove(newWord);
  27.                                 }
  28.                         }
  29.                 }
  30.         }

  31.         if (result < Integer.MAX_VALUE)
  32.                 return result;
  33.         else
  34.                 return 0;
  35.     }
  36.    
  37.    
  38. }

复制代码
Word break:
  1. public class Solution {
  2.     public boolean wordBreak(String s, Set<String> wordDict) {
  3.         boolean[] t = new boolean[s.length()+1];
  4.         t[0] = true;
  5.         
  6.         for(int i = 0; i<s.length(); i++)
  7.         {
  8.             if(!t[i])
  9.             {
  10.                 continue;
  11.             }
  12.             for(String a : wordDict)
  13.             {
  14.                 int len = a.length();
  15.                 int end = i + len;
  16.                
  17.                 if(end > s.length())
  18.                 {
  19.                     continue;
  20.                 }
  21.                
  22.                 if(t[end])
  23.                 {
  24.                    continue;
  25.                 }
  26.                
  27.                 if(s.substring(i,end).equals(a))
  28.                 {
  29.                     t[end]=true;
  30.                 }
  31.             }
  32.         }
  33.         return t[s.length()];
  34.     }
  35. }
复制代码

上一篇:Longest consecutive sequence
下一篇:弱弱的问一个子树问题的空间复杂度怎么算
🔗
e5399014 2015-6-12 14:27:06 | 只看该作者
全局:
word ladder --》 BFS
word break --》 DP
permutation --》 DFS
回复

使用道具 举报

🔗
 楼主| love1point 2015-6-27 21:59:22 | 只看该作者
全局:
e5399014 发表于 2015-6-12 14:27
word ladder --》 BFS
word break --》 DP
permutation --》 DFS

感谢讨论哈。

目前我只剩下word ladder需要再看一下,另外两个掌握了
回复

使用道具 举报

🔗
zhuli19901106 2015-6-27 22:40:54 | 只看该作者
全局:
本帖最后由 zhuli19901106 于 2015-6-27 22:59 编辑

word ladder这题写起来挺麻烦,但是思路就是边长为1的无向图里,求单源最短路径,所以用BFS或者Dijkstra都可解决。
如果要更快的话,可以双向BFS。


你发的代码应该已经是单向BFS的最优解了吧?不过,又不是原创吧,搜到了N个一模一样的。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表