上一页 1 ··· 30 31 32 33 34 35 36 37 38 ··· 43 下一页
摘要: 先序遍历的一个变化。记录下上次的节点就行了。public class Solution { public void flatten(TreeNode root) { Stack stack = new Stack(); TreeNode n = root; TreeNode last = null; while (n != null || !stack.empty()) { if (n != null) { if (last != null) { ... 阅读全文
posted @ 2013-08-19 13:48 阿牧遥 阅读(190) 评论(0) 推荐(0)
摘要: 动态规划。不过一开始老是把循环中的i写成n。有的时候会忘记i是循环中的边界。public class Solution { public int numTrees(int n) { if (n ==0) return 0; int sum[] = new int[n+1]; sum[1] = 1; for (int i = 2; i <= n; i++) { int total = 0; for (int j = 1; j <=i; j++) { if (j ... 阅读全文
posted @ 2013-08-19 00:24 阿牧遥 阅读(206) 评论(0) 推荐(0)
摘要: 简单题。不过又出现了一次初始状态设置的错误,当numRows==0时所作的设置,numRows==1时也需要。但一开始放到if block里面去了。public class Solution { public ArrayList> generate(int numRows) { ArrayList> ans = new ArrayList>(); if (numRows == 0) return ans; ArrayList tmp = new ArrayList(); tmp.add(1); ans.add(tmp)... 阅读全文
posted @ 2013-08-19 00:00 阿牧遥 阅读(218) 评论(0) 推荐(0)
摘要: 简单动态规划。public class Solution { public int minimumTotal(ArrayList> triangle) { int len = triangle.size(); if (len == 0) return 0; int[] sum = new int[len]; sum[0] = triangle.get(0).get(0); for (int i = 1; i arr = triangle.get(i); int tmp = sum[0]; ... 阅读全文
posted @ 2013-08-18 23:43 阿牧遥 阅读(184) 评论(0) 推荐(0)
摘要: http://ac.jobdu.com/problem.php?pid=1384基本思路很简单,从最右上角找起。九度的OJ做得还是不太行啊。必须要int main()才行,这道题时间卡得太紧,用cin和java都不行。#include using namespace std;int main(){ int m, n, t; int a[1000][1000]; while(scanf("%d %d",&m,&n) != EOF) { scanf("%d",&t); for (int i = 0; i = 0) { if ... 阅读全文
posted @ 2013-08-17 10:31 阿牧遥 阅读(181) 评论(0) 推荐(0)
摘要: 广度搜索BFS,要用Queue。还不是很熟,这道题帮助理清一些思绪了。其实这道题是求最短路径,所以BFS遇到第一个就可以返回了,所以后面有些现有大小和历史大小的判断可以省却。过程中拿数组存step还是很有用的。其他的解法中我看到有把四位char编码返回整数的a*26*26*26+b*26*26+c*26+d,很不错,本质就是26进制的数。import java.util.*;public class SmartWordToy{ public int minPresses(String start, String finish, String[] forbid) { int[][][][] s. 阅读全文
posted @ 2013-08-16 23:40 阿牧遥 阅读(351) 评论(0) 推荐(0)
摘要: 简单题。但做得不简洁。里面用了一个count来计数,还是不错的。因为有可能只有一行(或子状态中),那么m_low == m_high,这时,当m_low++后,m_high大于m_low,可能会从左往右和回扫都是同一行。 public class Solution { public ArrayLis 阅读全文
posted @ 2013-08-16 19:27 阿牧遥 阅读(209) 评论(0) 推荐(0)
摘要: 简单题。public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode root = null; ListNode n = null; while (l1 != null & l2 != null) { ListNode tmp = null; if (l1.val < l2.val) { tmp = new ListNode(l1.val); ... 阅读全文
posted @ 2013-08-16 18:58 阿牧遥 阅读(192) 评论(0) 推荐(0)
摘要: 此题以前看过,就是中序遍历。相当于有一个排序的数组里面有两个数字调换了,有两种情况,调换的数字相邻和不相邻。public class Solution { public void recoverTree(TreeNode root) { Stack stack = new Stack(); TreeNode n = root; TreeNode last = null; TreeNode current = null; TreeNode n1 = null; TreeNode n2 = null; ... 阅读全文
posted @ 2013-08-16 18:37 阿牧遥 阅读(221) 评论(0) 推荐(0)
摘要: 经过昨日训练,中序遍历基本会了。所以这个就比较顺利,对二分查找树来说,中序遍历就是递增的。所以遍历一下,遇到反例返回false即可。 public class Solution { public boolean isValidBST(TreeNode root) { Stack<TreeNode> 阅读全文
posted @ 2013-08-16 18:05 阿牧遥 阅读(183) 评论(0) 推荐(0)
上一页 1 ··· 30 31 32 33 34 35 36 37 38 ··· 43 下一页