Leetcode记录
Leetcode笔记
xinglu31
好记性不如烂笔头
展开
-
Merge Sorted Array
public class Solution { public void merge(int A[], int m, int B[], int n) { if (n == 0) { return; } if (m == 0) { for (int tmp = 0; tmp < n; tmp++) { A[tmp] = B[tmp]; }...原创 2018-04-28 15:36:29 · 130 阅读 · 0 评论 -
Remove Element
public class Solution { public int removeElement(int[] A, int elem) { int length = A.length; if(length == 0){ return 0; }else{原创 2014-04-22 19:54:00 · 3484 阅读 · 0 评论 -
Two Sum
public class Solution { public int[] twoSum(int[] values, int target) { Map<Integer , Integer[]> map = new HashMap<Integer , Integer[]>(); for(int i = 0 ; i < valu...原创 2014-04-22 19:56:04 · 417 阅读 · 0 评论 -
Search Insert Position
public class Solution { public int searchInsert(int[] A, int target) { int index = A.length; if(A.length == 1){ if(target <= A[0]) return 0;原创 2014-04-22 19:56:46 · 453 阅读 · 0 评论 -
Single Number
public class Solution { public int singleNumber(int[] A) { Map tmpMap = new HashMap(); for(int i = 0 ; i < A.length ; i++){ if(tmpMap.get(A[i]) == null)原创 2014-04-22 20:49:45 · 470 阅读 · 0 评论 -
Sort Colors
public class Solution { public void sortColors(int[] A) { int countRed = 0; int countBlue = 0; int countBlack = 0; for(int i = 0 ;i < A.length ;i++){ sw原创 2014-04-25 16:21:32 · 465 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
public class Solution { public int maxProfit(int[] A) { int maxProfit = 0; if(A.length < 2){ return 0; } int low = A[0];原创 2014-04-25 17:42:47 · 488 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] A) { int maxProfit = 0; if(A.length <= 1){ return 0; } int low = A[0]; for (int i = 1; i < A原创 2014-04-25 17:45:49 · 542 阅读 · 0 评论 -
Pascal's Triangle
public class Solution { public ArrayList> generate(int k) { ArrayList> result = new ArrayList>(); for (int i = 0; i < k; i++) { ArrayList tmpResult = new ArrayL原创 2014-04-25 19:15:05 · 525 阅读 · 0 评论 -
Pascal's Triangle II
public class Solution { public ArrayList getRow(int k) { if(k < 0){ k = 0; } k++; ArrayList> result = new ArrayList>(); for (int i = 0; i < k;原创 2014-04-25 19:23:24 · 523 阅读 · 0 评论 -
Path Sum
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-04-25 19:47:40 · 468 阅读 · 0 评论 -
Climbing Stairs
public class Solution { public int climbStairs(int n) { int res = n - 1; if(n == 0){ return 0; } int[] mem = new int[n]; if(n <= 1){原创 2014-04-25 20:17:53 · 1015 阅读 · 0 评论 -
Merge Two Sorted Lists
public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode s1 , s2; if(l1 == null && l2 != null){ return l2; }else if(l1 != null && l2 == null){ r原创 2014-04-28 20:03:29 · 499 阅读 · 0 评论 -
Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if(x < 0){ return false; } if(x < 10 ){ return true; } String xstr = String.valueOf(x); //System.out.println(xstr原创 2014-04-28 20:38:52 · 533 阅读 · 0 评论 -
Maximum Depth of Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-04-29 19:21:39 · 3439 阅读 · 0 评论 -
Same Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-04-29 20:32:16 · 598 阅读 · 0 评论 -
Swap Nodes in Pairs
public class Solution { public ListNode swapPairs(ListNode head) { ListNode p = head; ListNode q = head; if(head == null){ return head; }else if(head.ne原创 2014-04-30 19:21:43 · 476 阅读 · 0 评论 -
Pow(x, n)
public class Solution { public double pow(double x, int n) { double res = 1; if(n == 0){ return res; }else if(n > 0){ if(n == 1){ re原创 2014-05-04 16:12:31 · 501 阅读 · 0 评论 -
Maximum Subarray
public class Solution { public int maxSubArray(int[] A) { int res = 0; if(A.length == 0){ return res; } int max = A[0]; for(int i = 0 ; i < A.le原创 2014-05-04 17:24:38 · 419 阅读 · 0 评论 -
Binary Tree Traversal
Binary Tree Preorder Traversal原创 2014-05-05 19:27:08 · 467 阅读 · 0 评论 -
Balanced Binary Tree
public class Solution { public boolean isBalanced(TreeNode root) { if(root == null){ return true; } int depth = maxDepth(root.left) - maxDepth(root.right);原创 2014-05-05 20:03:21 · 619 阅读 · 0 评论 -
Length of Last Word
public class Solution { public int lengthOfLastWord(String s) { int res = 0; if(s.length() == 0){ return res; } char[] sArray = s.toCharArray(); ...原创 2014-05-06 09:02:53 · 452 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi...原创 2014-05-06 18:57:09 · 726 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi原创 2014-05-06 18:59:05 · 462 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Arraypublic class Solution { public int removeDuplicates(int[] A) { boolean isEqual = false; int count = 0; int tmp = 0; if(A == null...原创 2014-05-12 09:03:53 · 499 阅读 · 0 评论 -
Remove Duplicates from Sorted List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public...原创 2014-05-12 15:16:20 · 681 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
public class Solution { public int removeDuplicates(int[] A) { int count = 0; int tmp = 0; if(A == null || A.length == 0){ return 0; } if(A.leng...原创 2014-05-12 16:56:30 · 482 阅读 · 0 评论 -
Single Number II
public class Solution { public int singleNumber(int[] A) { Arrays.sort(A);// for(int i = 0 ; i < A.length ; i++){// System.out.println(A[i]);// } int...原创 2014-05-14 13:59:39 · 535 阅读 · 0 评论 -
Plus One
public class Solution { public int[] plusOne(int[] A) { if(A.length >= 1) { int[] B = new int[A.length + 1]; int added = 1; for(int i = A.length - 1 ; i >= 0 ; i--){ i...原创 2014-05-14 19:31:29 · 509 阅读 · 0 评论 -
Longest Consecutive Sequence
public class Solution { public int longestConsecutive(int[] num) { Arrays.sort(num); int count = 1; int res = 1; int tmp = 0 ; int countTmp = 0; for(int i = 1 ; i < num.len...原创 2014-05-15 20:10:46 · 501 阅读 · 0 评论 -
Binary Tree Level Order Traversal
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi...原创 2014-05-15 20:20:07 · 529 阅读 · 0 评论 -
Sum Root to Leaf Numbers
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi...原创 2014-05-20 09:42:06 · 558 阅读 · 0 评论 -
Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { int len = strs.length; if(len == 0){ return ""; } if(len == 1){ return strs[0]; } int length =...原创 2014-05-20 10:20:19 · 493 阅读 · 0 评论 -
Minimum Depth of Binary Tree
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { publi...原创 2014-05-20 11:39:58 · 545 阅读 · 0 评论