[leetcode-52]N-QueensII(java)
2015-08-03 09:32
561 查看
问题描述:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
分析:该题与上道基本一样,只是有些细节需要改变一下,比如这题要求返还的是count值,而这个值是不能通过传参进去来得到的。所以只能通过返回值来实现。
代码如下:248ms
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
分析:该题与上道基本一样,只是有些细节需要改变一下,比如这题要求返还的是count值,而这个值是不能通过传参进去来得到的。所以只能通过返回值来实现。
代码如下:248ms
[code] private int solve(int n,int index,List<Integer> tmpList){ if(n==index){ return 1; } int count = 0; for(int col = 0;col<n;col++){ int row = index; int rowList; for(rowList = 0;rowList<tmpList.size();rowList++){ int rowrow = rowList; int colcol = tmpList.get(rowrow); //同一列 if(col == colcol) break; //同一斜线 if(Math.abs(rowrow-row)==Math.abs(colcol-col)) break; } if(rowList==tmpList.size()) { tmpList.add(col); int val = solve(n, index + 1, tmpList); tmpList.remove(tmpList.size()-1); if(val>0) { count += val; } } } return count; } public int totalNQueens(int n){ List<Integer> tmpList = new LinkedList<>(); return solve(n, 0, tmpList); }
相关文章推荐
- Android ROM定制——界面美化基础(framework-res、SystemUI修改)
- UI_Target-Action
- 黑马程序员——图形用户界面GUI
- EasyUI 扩展自定义EasyUI校验规则 验证规则(常用的
- C#在非UI线程调用UI线程的控件
- [leetcode-51]N-Queens(java)
- 取得viewController展示view
- UI 03 UITextField
- UI 02 UIButton
- iOS UI03_登陆+注册 UI1~3系统归纳
- UIViewController视图控制器(移动self.view,随机颜色)
- UIImageView图片填充三种模式的区别
- HackerRank - "Snakes and Ladders: The Quickest Way Up"
- 为什么不推荐用UniversalImageLoader加载res/drawable
- UE4信息交互-关卡蓝图引用关卡中的对象
- wildfly使用入门
- IOS开发UI篇--IOS动画(Core Animation)总结
- 本教程针对HBuilder5.0.0,制作日期2014-12-31(从HBuilder工具上获得)
- UE4信息交互-命令行执行自定义事件ConsoleCommand
- 本教程针对HBuilder5.0.0,制作日期2014-12-31(从HBuilder工具上获得)