2014年1月28日

LeetCode: Sort Color

摘要: 这道题断断续续想了好长时间。今天看说这个要用two points做。所以尝试做了一下。我的想法是先把0都移到数组的前面。然后再把1都移到2的前面。首先用一个指针first,我们要做到的是first之前的元素都是0. 还有一个扫描指针end,用来扫描数组并交换元素。在first到end之间不会有0出现,因为在end扫描的时候已经把0元素交换到first之前了。。。 1 public static void sortColors(int[] A) { 2 int first = 0, end = 0; 3 for (; end first the element b... 阅读全文

posted @ 2014-01-28 13:39 longhorn 阅读(241) 评论(0) 推荐(0) 编辑

LeetCode: Rotate Image

摘要: 最开始还用了一种递归的方法。。。也不知道是聪明还是傻。先将最外圈旋转,然后向里一圈,旋转。。。。最后到达矩阵中心。这里比较麻烦的是矩阵的下标,调了好久才写对。 1 public static void rotate(int[][] matrix) { 2 rotate(matrix, 0); 3 } 4 public static void rotate(int[][] matrix, int start) { 5 if(start == matrix.length/2) return; 6 int first=0, second... 阅读全文

posted @ 2014-01-28 04:45 longhorn 阅读(259) 评论(0) 推荐(0) 编辑

导航