随笔分类 - leetCode
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in or...
阅读全文
摘要:这道题也太简单了吧,可能我的方法不够简单1 public class Solution {2 public int search(int[] A, int target) {3 for(int i=0;i<A.length;i++){4 if(A[i]==target)5 return i;6 }7 return -1;8 }9 }
阅读全文
摘要:做这道题的时候很纠结,下面这个程序能够AC, 1 int n=A.length; 2 int i=0,j=0,cnt=0; 3 if(n==0||n==1) return n; 4 for(i=0;i=2){ 9 cnt++;10 }else if(A[i]!=A[i+1]&&cnt=2){14 cnt=0;15 }16 17 }18 if(A[n-1]!=A[n-2]||A[n...
阅读全文
摘要:有二叉树的前序遍历和后序遍历,构造二叉树/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { HashMap map = new HashMap (); f...
阅读全文
摘要:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.对这道题直接采用递归遍历树,来判断这两个树是否相同 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * ...
阅读全文
摘要:刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结。后来看了下大神的做法很受启发,改了改代码,最终提交了。public static ArrayList insert(ArrayList intervals, Interval newInterval) { ArrayList ips=null; ips=new ArrayList(); int p=0; int q=intervals.size()-1; if(intervals.size()==0){...
阅读全文
摘要:今天做了leetcode的Rotate List,刚开始头脑不清楚,写的乱七八糟的,后来改了下,提交了,能过,把代码贴出来。做题的时候头脑要清楚,我刚开始做完的时候才发现我把向左向右移动弄反了,后来修改了下。 1 public static ListNode rotateRight(ListNode head, int n) { 2 ListNode first=head; 3 ListNode second=head; 4 ListNode result=null; 5 ListNode t=null; 6 in...
阅读全文

浙公网安备 33010602011771号