
leetcode
lz846525719
这个作者很懒,什么都没留下…
展开
-
List与ArrayList
List是一个接口,而ArrayList是List接口的一个实现类。 ArrayList类继承并实现了List接口。 因此,List接口不能被构造,也就是我们说的不能创建实例对象,但是我们可以像下面那样为List接口创建一个指向自己的对象引用,而ArrayList实现类的实例对象就在这充当了这个指向List接口的对象引用。...原创 2020-08-05 20:39:29 · 204 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
public class BinaryTreeLevelOrderTraversalII { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> ret =new ArrayList<>(); if(root==null) { return r原创 2020-08-05 20:37:28 · 99 阅读 · 0 评论 -
Same Tree
首先可以用DFS深搜的递归方法来做public class SameTree { public boolean isSameTree(TreeNode p, TreeNode q) { if(!p&&!q) { return true; } if(!p&&q||!q&&p||p.val!=q.val) { return false; }原创 2020-08-03 20:17:31 · 102 阅读 · 0 评论 -
RemoveDuplicates
数据结构中比较简单的链表题 public ListNode deleteDuplicates(ListNode head) { ListNode pre = head; ListNode cur=head.next; if(head==null||head.next==null) {return head;} while (pre.next != null) { if (pre.val == cur.原创 2020-07-26 21:21:51 · 483 阅读 · 0 评论 -
MergeSortedArray
这个题还是比较简单的public void merge(int[] nums1, int m, int[] nums2, int n) { int[] nums3; nums3=new int[m+n]; for(int i=0,j=0,k=0;i<m+n;i++) { if(j<m&&k<n)原创 2020-07-26 21:20:51 · 117 阅读 · 0 评论 -
爬楼梯
这个题一开始看有点晕,后来把1,2,3,4,5挨个算出来,越算越惊奇,这不就是传说中的斐波那契吗,知道是这个就好做了,代码如下public class Stair { public static void main(String[] args) { Stair stair=new Stair(); System.out.println(stair.goStair(2)); } public int goStair(int n) {原创 2020-07-25 10:02:09 · 105 阅读 · 0 评论 -
Sqrt(x)
这个题的坑真的是太多了,感觉踩了好多的坑,首先,暴力枚举是不可能的,肯定会超时,然后又尝试了下二分法查,结果发现溢出,因为一开始选择的方法是a*a=x,这样确实会溢出,后来有尝试了下除法,结果还是过不去,仔细一想,应该是mid作为除数不能有为零的情况,所以把mid,max,min初始值最好都不要设为0,都设为从1开始,这样x为0的情况也统计进去了。代码如下:public class SqrtX { public static void main(String[] args) {原创 2020-07-23 23:16:29 · 347 阅读 · 0 评论 -
AddBinary
没做出来,去查了一下,感觉这种方法挺好的。public class AddBinary { public static void main(String[] args) { String a="11",b="1"; AddBinary addBinary=new AddBinary(); System.out.println(addBinary.myaddBinary(a,b)); } public String myaddBinary(St原创 2020-07-23 20:31:55 · 141 阅读 · 0 评论 -
Pllus one
这道题一开始方向想错了,是按加1是否等于10 弄得,这样弄太复杂了,考虑的太多,耽误了很长时间也没弄出来,后来按是否小于9处理很快就弄出来了。代码如下:public class PlusOne { public static void main(String[] args) { PlusOne plusOne = new PlusOne(); int[] a = {9, 9, 9, 9}; int[] b = plusOne.myplusOne(a)原创 2020-07-23 19:20:34 · 157 阅读 · 0 评论 -
LengthOfLastWord
这题乍一看挺好做的,但是还要考虑的详细一点,坑挺多的,尤其是空格的位置要考虑好。`public class LengthofLastWord {public static void main(String[] args) {String s="";LengthofLastWord lastWord=new LengthofLastWord();System.out.println(lastWord.LastWord(s));}public int LastWord(String s) {i原创 2020-07-21 22:28:12 · 210 阅读 · 0 评论