摘要: 广度搜索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 阿牧遥 阅读(352) 评论(0) 推荐(0)
摘要: 简单题。但做得不简洁。里面用了一个count来计数,还是不错的。因为有可能只有一行(或子状态中),那么m_low == m_high,这时,当m_low++后,m_high大于m_low,可能会从左往右和回扫都是同一行。 public class Solution { public ArrayLis 阅读全文
posted @ 2013-08-16 19:27 阿牧遥 阅读(210) 评论(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 阿牧遥 阅读(193) 评论(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 阿牧遥 阅读(223) 评论(0) 推荐(0)
摘要: 经过昨日训练,中序遍历基本会了。所以这个就比较顺利,对二分查找树来说,中序遍历就是递增的。所以遍历一下,遇到反例返回false即可。 public class Solution { public boolean isValidBST(TreeNode root) { Stack<TreeNode> 阅读全文
posted @ 2013-08-16 18:05 阿牧遥 阅读(184) 评论(0) 推荐(0)
摘要: 水题。http://community.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854一开始错了是因为理解错题意。还有就是初始化min应该设为Integer.MAX_VALUE,而不是相反。public class TallPeople { public int[] getPeople(String[] people) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; int len1 = people.length; if (len1 == 0) 阅读全文
posted @ 2013-08-16 13:55 阿牧遥 阅读(194) 评论(0) 推荐(0)
摘要: 水题,不值一提。http://community.topcoder.com/stat?c=problem_statement&pm=1585&rd=6535import java.util.*;public class BusinessTasks { public String getTask(String[] list, int n) { ArrayList al = new ArrayList(); for (int i = 0; i < list.length; i++) { al.add(list[i]); } int len = al.size(); if (l 阅读全文
posted @ 2013-08-16 13:19 阿牧遥 阅读(202) 评论(0) 推荐(0)