
lintcode
文章平均质量分 62
Missbubu
这个作者很懒,什么都没留下…
展开
-
632-二叉树遍历
/*public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; */public c原创 2017-03-20 09:54:18 · 263 阅读 · 0 评论 -
547-两数组的交
3.27 首先学到了这么几点吧:1. 原来是可以定义变长的数组的,就是 int[] a = new int[n];这种形式,n是变量是可以的哈哈哈哈哈哈哈。2.就是熟练ArrayList 和 HashMap 的用法吧。3.学会了怎么ArrayList和数组之间的相互转换。先用了这种普通先排序再比较的方法,果然超时了public static int[] inter原创 2017-03-27 17:35:16 · 379 阅读 · 0 评论 -
452-删除单链表中的元素
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { /** * @param he原创 2017-03-20 17:30:10 · 308 阅读 · 0 评论 -
548-两数组的交2
3.27 这个就是不能除去相同的元素没啥特别的感觉 public static int[] intersection2(int[] nums1, int[] nums2){ // Write your code here HashMap map = new HashMap(); for(int i =原创 2017-03-27 18:10:48 · 295 阅读 · 0 评论 -
539-移动零
3.27感觉自己有一个神奇的脑回路,不知道怎么就AC了。这代码估计明天看我就不懂了吧。public class Solution { /** * @param nums an integer array * @return nothing, do this in-place */ public static void moveZeroes(i原创 2017-03-27 18:51:42 · 260 阅读 · 0 评论 -
496- 玩具工厂
3.28这个题并没有什么意思啊就当熟练了一下接口吧。继承一个接口就是要实现它里边的方法。/** * Your object will be instantiated and called as such: * ToyFactory tf = new ToyFactory(); * Toy toy = tf.getToy(type); * toy.talk(); */inte原创 2017-03-28 16:22:34 · 306 阅读 · 0 评论 -
524-左填充
3.28 没啥好写的public class StringUtils { /** * @param originalStr the string we want to append to with spaces * @param size the target length of the string * @return a string */原创 2017-03-28 16:35:40 · 277 阅读 · 0 评论 -
517 - 丑数
3.28 犯了一个很傻的错误取余和除法 傻傻分不清楚以至于总是bug浪费了不少的时间public class Solution { /** * @param num an integer * @return true if num is an ugly number or false */ public boolean isUgly(i原创 2017-03-28 16:59:37 · 239 阅读 · 0 评论 -
514-栅栏染色
3.28这个题自己的错误,废了很多时间。如果是单纯地相邻不许同色的话,应该就是这样的public static int numWays1(int n, int k) { if( n ==1){ return k;// Write your code here } if(k == 1 && n > 2){原创 2017-03-28 17:56:19 · 382 阅读 · 0 评论 -
366-斐波那契数列
class Solution { /** * @param n: an integer * @return an integer f(n) */ public int fibonacci(int n) { if (n == 1){ return 0; } if(n原创 2017-03-20 17:38:42 · 790 阅读 · 0 评论 -
451 - 两两交换链表中的节点
4.7/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { /** * @par原创 2017-04-07 10:17:49 · 293 阅读 · 0 评论 -
457 - 经典二分查找问题
4.7 二分查找问题并不难,但是要记得对边界值的处理,不然可能时间复杂度过不了。public class Solution { /** * @param nums: An integer array sorted in ascending order * @param target: An integer * @return an inte原创 2017-04-07 10:05:33 · 315 阅读 · 0 评论 -
445 - 余弦相似度
4.7太膨胀了,太膨胀了,居然觉得上午刷的这几道题并没有什么意思。还是要反省的,总是不够细心,犯弱智的错误。class Solution { /** * @param A: An integer array. * @param B: An integer array. * @return: Cosine similarity. */原创 2017-04-07 11:19:30 · 287 阅读 · 0 评论 -
488-快乐数
主要是字符串和整型的相互转换原创 2017-03-21 21:38:38 · 328 阅读 · 0 评论 -
1-A+B 关于位运算(加减法)
3.22 学会了用位运算做加减法以后有了时间再学习一下乘除法,以及位运算的一些妙用。class Solution { /* * param a: The first integer * param b: The second integer * return: The sum of a and b */ public int ap原创 2017-03-22 11:06:08 · 597 阅读 · 0 评论 -
569-获取整数的每一位
3.22 得到整数的每一位,目前想到的方法只是转换成为字符串,感觉有点儿笨如果以后有了好的方法,要多多学习这个题还是挺简单的。public class Solution { /** * @param num a non-negative integer * @return one digit */ public int addDigits(in原创 2017-03-22 11:19:38 · 392 阅读 · 0 评论 -
365-计算二进制中1 的个数
3.22 这道题,我就是用的很普通的思路先把一个整数化成二进制,然后一位一位的对比来做的。有点儿笨,但总归也实现了。public class Solution { /** * @param num: an integer * @return: an integer, the number of ones in num */ public原创 2017-03-22 16:29:54 · 532 阅读 · 0 评论 -
407-数组表示的整数+1
3.23 讲道理,我这个真的不是什么优秀的代码。但是也奇迹般的A 了,public class Solution { /** * @param digits a number represented as an array of digits * @return the result */ public int[] plusOne(int[]原创 2017-03-23 15:02:31 · 281 阅读 · 0 评论 -
469 - 等价二叉树
3.30 二叉树的题,反正递归是很好用的至于其他的方法,现在并不想仔细研究。呵呵/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { *原创 2017-03-30 14:54:01 · 236 阅读 · 0 评论 -
453- 将二叉树拆成链表
3.30即使昨天联系了半天的二叉树的基本操作,今天做起题目来依旧不顺手。想着边遍历边修改左右子树,但是好像不太现实。只好把先序遍历的结果存在queue中然后再遍历queue,再修改子树。/** * Definition of TreeNode: * public class TreeNode { * public int val; * public Tr原创 2017-03-30 11:14:03 · 331 阅读 · 0 评论 -
175-翻转二叉树
3.30 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * th原创 2017-03-30 15:00:35 · 293 阅读 · 0 评论 -
97 - 二叉树的最大深度
3.30刚开始用三目表达式就超时了慎用啊一定要避免重复计算/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * t原创 2017-03-30 17:39:31 · 267 阅读 · 0 评论 -
155 - 二叉树的最小深度
3.30本来以为只要左子树或者是右子树为空 直接返回1 就可以了,事实并不是这样的有的时候真的是太想当然了/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode原创 2017-03-30 17:47:09 · 518 阅读 · 0 评论 -
480 - 二叉树的所有路径
3.30 以为自己做不出来呢 没想到居然做出来了还是利用递归的思想。虽然做题慢了点,但是似乎找到那么一些感觉了呢。/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public Tr原创 2017-03-30 14:44:25 · 296 阅读 · 0 评论 -
66 - 二叉树的前序遍历
3.31以为很简单,果然就大意了。LinkedList 的 add 和push 还是有很大差别的啊。还有add 和offer原来也是有一些差别的。受教了呢。/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left原创 2017-03-31 20:15:33 · 370 阅读 · 0 评论 -
67 - 二叉树的中序遍历
3.31没毛病。老铁/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; *原创 2017-03-31 20:24:51 · 254 阅读 · 0 评论 -
69 - 二叉树的层次遍历
3.31当把一个对象的值,赋给另外一个对象的时候,只是赋值地址。也就是说,二者是指向同一片内存区域的。如果要创建两个不同的对象,可以使用clone这个函数。在这个问题上确实困扰了很久原创 2017-03-31 19:54:05 · 506 阅读 · 0 评论 -
68 - 二叉树的后序遍历
3.31 果然二叉树的后序遍历掌握的很是不好啊。用非递归的方法不知道为啥总超时。最后没有办法用了递归的方法。/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeN原创 2017-03-31 20:46:14 · 292 阅读 · 0 评论 -
375 -二叉树的克隆
4.1没啥感觉,呵呵就是用递归啊非递归的懒得写,我真的是懒。/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { *原创 2017-04-01 09:45:14 · 252 阅读 · 0 评论 -
212-空格替换
4.11 想复杂了,没有看清楚,可以假设string是无限长的。char的包装类是character就这些吧public class Solution { /** * @param string: An array of Char * @param length: The true length of the string * @retur原创 2017-04-11 10:09:09 · 226 阅读 · 0 评论 -
133 - 最长单词
4.11用一个map映射,通过长度对单词分组。最后直接返回 length最长的就可以。class Solution { /** * @param dictionary: an array of strings * @return: an arraylist of strings */ ArrayList longestWords(String[原创 2017-04-11 10:37:07 · 363 阅读 · 0 评论 -
433-岛屿的个数
4.11数组统一赋值的函数Arrays.fill();感觉自己的想法很野啊,用总的点的个数去减。就是要注意边界值,数组是从下标0开始的,一定要注意。public class Solution { /** * @param grid a boolean 2D matrix * @return an integer */ public stati原创 2017-04-11 14:53:05 · 304 阅读 · 0 评论 -
174 - 删除链表中倒数第n个节点
4.12还是要注意边界值的处理啊总不是能一次考虑好/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.ne原创 2017-04-12 11:30:07 · 355 阅读 · 0 评论 -
173 - 链表的插入排序
4.12真的快被这题弄疯了。坐了好久。思路一直不清,一直死循环,或者是错误。应该将排序好的部分和未排序的部分区分出来,这样就比较容易实现。/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val)原创 2017-04-12 17:14:57 · 439 阅读 · 0 评论 -
423 - 有效括号序列
4.12public class Solution { /** * @param s A string * @return whether the string is a valid parentheses */ public boolean isValidParentheses(String s) { LinkedList st原创 2017-04-12 17:33:27 · 346 阅读 · 0 评论 -
185 - 矩阵的之字型遍历
4.13这道题简直要写懵逼了啊。要搞清楚运动方向。public class Solution { /** * @param matrix: a matrix of integers * @return: an array of integers */ public static int[] printZMatrix(int[][] matrix) {原创 2017-04-13 20:31:12 · 528 阅读 · 0 评论 -
2-尾部的零
4.18看到是输入数据是 long型的,就知道不能笨笨的算数出来再数数了。想到了找相乘得0的情况,比如45=20,56=30,58=40等。但是依旧没有什么好的思路。看了网上的答案,原来重点是回到分解因数有多少个5的问题之上。很巧妙,我没有想到。有了想法,代码的实现还是很简单的。class Solution { /* * param n: As des原创 2017-04-18 09:39:19 · 260 阅读 · 0 评论 -
4.18-合并数组
4.18没毛病class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) {原创 2017-04-18 09:56:03 · 243 阅读 · 0 评论 -
8 - 旋转字符
4.18public class Solution { /** * @param str: an array of char * @param offset: an integer * @return: nothing */ public void rotateString(char[] str, int offset) {原创 2017-04-18 10:08:13 · 285 阅读 · 0 评论 -
9 - Fizz Buzz 问题
4.18好奇怪,我并不记得自己做过这道题,但是打开之后居然代码已经写好了,而且和我的想法也是一致的呢。好奇怪。class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList fizzBuzz(int n) {原创 2017-04-18 10:16:38 · 1545 阅读 · 0 评论