
Leetcode
文章平均质量分 58
AngelQian2
<a href="http://www.linkedin.com/in/lijuanqian">
<img src="https://static.licdn.com/scds/common/u/img/webpromo/btn_viewmy_160x33.png" width="160" height="33" border="0" alt="View Lijuan (Angel) Qian's profile on LinkedIn">
</a>
展开
-
*Leetcode - ZigZag Conversion
public class Solution { public String convert(String s, int nRows) { int l = s.length();if (nRows >= l)return s;StringBuilder[] sb = new StringBuilder[nRows];for (int z = 0; z原创 2015-01-07 07:48:15 · 311 阅读 · 0 评论 -
Leetcode-Median of Two Sorted Arrays 时间复杂度O(M+N)
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { int m = A.length, n = B.length;boolean even = (m + n) % 2 == 0;int median = (m + n) / 2;int[] C = me原创 2015-01-07 07:21:25 · 396 阅读 · 0 评论 -
Leetcode - Reverse Integer
public class Solution { public int reverse(int x) { if( x == Integer.MIN_VALUE)return 0; int a=Math.abs(x);int[] arr = new int[Integer.toString(a).length()];for(int i=0;原创 2015-01-08 04:28:28 · 325 阅读 · 0 评论 -
Word Break - Leetcode
public class Solution { public boolean wordBreak(String s, Set dict) { boolean[] f = new boolean[s.length()+1]; f[0]=true; for(int i=1; i<=s.length(); i++){ for原创 2015-01-27 06:20:12 · 383 阅读 · 0 评论 -
Remove Duplicates from Sorted Array - Leetcode
public class Solution { public int removeDuplicates(int[] A) { if (A.length == 0) return 0; int i=0; for (int j = 1;j < A.length;j++) { if (A[i] != A[j]) A[++i] = A[j]; } r原创 2015-01-27 12:27:25 · 278 阅读 · 0 评论 -
Leetcode - Palindrome Number
public class Solution { public boolean isPalindrome(int x) { if (x return false;int a = Math.abs(x);int l = Integer.toString(a).length();for (int i = 1, j = l; i int原创 2015-01-08 08:28:51 · 298 阅读 · 0 评论 -
Leetcode - Container With Most Water
------------------HINT:==========Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endp原创 2015-01-08 08:30:34 · 268 阅读 · 0 评论 -
Decode Ways - Leetcode
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2015-01-28 06:21:15 · 370 阅读 · 0 评论 -
Minimum Path Sum - Leetcode
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2015-01-28 13:32:16 · 321 阅读 · 0 评论 -
Word Break II - Leetcode
Given a string s and a dictionary of words dict, add spaces in s toconstruct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens =原创 2015-01-27 06:25:12 · 402 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II - Leetcode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,原创 2015-01-27 12:54:21 · 450 阅读 · 0 评论 -
Triangle - Leetcode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2015-01-29 08:37:36 · 381 阅读 · 0 评论 -
Longest Consecutive Sequence - Leetcode
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2015-01-30 05:00:37 · 377 阅读 · 0 评论 -
Leetcode[Easy] - Two Sum
public class Solution { public int[] twoSum(int[] numbers, int target) { int a, b; Map hm = new HashMap(); for(int i=0; i { a=numbers[i];原创 2015-01-03 09:01:43 · 351 阅读 · 0 评论 -
Edit Distance - Leetcode
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a wor原创 2015-01-28 14:37:15 · 375 阅读 · 0 评论 -
Interleaving String - Leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", r原创 2015-01-29 07:20:51 · 423 阅读 · 0 评论 -
Leetcode - 4Sum
public class Solution { public List> fourSum(int[] num, int target) { List> ll = new ArrayList>();if (num.length return ll;Arrays.sort(num);for(int i=0; iif(i>0 && num[i]==原创 2015-01-11 15:14:08 · 378 阅读 · 0 评论 -
Leetcode - 3Sum
public class Solution { public List> threeSum(int[] num) { List> ll = new ArrayList>();if (num.length return ll;int a1;Arrays.sort(num);int i=0,j = num.length - 1, s;for (i原创 2015-01-10 07:04:05 · 297 阅读 · 0 评论 -
*Leetcode - 3Sum Closest
public class Solution { public int threeSumClosest(int[] num, int target) { if (num.length return 0;Arrays.sort(num);int min = num[0] + num[1] + num[num.length - 1], sum, d;for原创 2015-01-11 12:32:07 · 312 阅读 · 0 评论 -
Leetcode - Valid Parentheses
public class Solution { public boolean isValid(String s) { Stack sc = new Stackfor (int i = 0; i Character c = s.charAt(i);switch (c) {case '(':case '[':case '{':sc.push(c)原创 2015-01-12 08:18:21 · 318 阅读 · 0 评论 -
Leetcode - Letter Combinations of a Phone Number
public class Solution { public List letterCombinations(String digits) { List ls = new ArrayList ls.add("");if (digits.length() return ls;String[] arr = { "", "", "abc"原创 2015-01-11 15:40:15 · 422 阅读 · 0 评论 -
Search in Rotated Sorted Array II - Leetcode
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.---原创 2015-01-31 09:32:32 · 387 阅读 · 0 评论 -
Search in Rotated Sorted Array - Leetcode
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur原创 2015-01-29 06:42:17 · 381 阅读 · 0 评论 -
Leetcode - Merge Two Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }原创 2015-01-14 09:28:03 · 287 阅读 · 0 评论 -
Leetcode - Swap Nodes in Pairs
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }原创 2015-01-14 11:34:58 · 356 阅读 · 0 评论 -
*Leetcode - Merge k Sorted Lists
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }原创 2015-01-14 10:19:13 · 384 阅读 · 0 评论 -
Jump Game - Leetcode
public class Solution { public boolean canJump(int[] A) { int cur=1; for(int i=0; i<cur&& cur<A.length;i++){ cur = Math.max(i+A[i]+1,cur); } return cur>原创 2015-02-04 05:02:25 · 330 阅读 · 0 评论 -
Valid Sudoku - Leetcode
1 ) 检查行; 2) 检查列; 3) 检查方格 => 用一个长度为9的bool数组判断是否有重复;public class Solution { public boolean isValidSudoku(char[][] board) { boolean[] valid = new boolean[9]; for(int i=0; i<board.len原创 2015-02-04 09:42:43 · 506 阅读 · 0 评论 -
Sqrt(x) - Leetcode
Implement int sqrt(int x).Compute and return the square root of x.----------------------public class Solution { public int sqrt(int x) { int left=1, right=x/2; if(right原创 2015-02-05 05:31:46 · 333 阅读 · 0 评论 -
Pow(x, n) - Leetcode
Implement pow(x, n).-----------实现X^n,用课本的语言说就是分治法X^n = X^(n/2)*X^(n/2)*X^(n%2);当n为偶数时,X^n = X^(n/2)*X^(n/2) ,【n%2 ==0】; 当n为奇数时,X^n = X^(n/2)*X^(n/2)*X,【n%2 ==1】考虑一下n为负数的情况。------------原创 2015-02-05 05:24:56 · 353 阅读 · 0 评论 -
Unique Paths - Leetcode
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2015-02-05 07:08:23 · 348 阅读 · 0 评论 -
Leetcode[Easy] - Merge Sorted Array
public class Solution { public void merge(int A[], int m, int B[], int n) { int currentA = m-1; int currentB = n-1; int k = m+n-1; while(currentA >= 0 && currentB >= 0)原创 2014-12-30 05:47:19 · 336 阅读 · 0 评论 -
Leetcode[Easy] -- Roman to Integer
public class Solution { public int romanToInt(String s) { int current = 0,prev,last; HashMap hm = new HashMap hm.put("I", 1); hm.put("V", 5); hm.put("X", 10); hm原创 2014-12-30 07:18:00 · 352 阅读 · 0 评论 -
Leetcode[Easy] - Valid Palindrome
public class Solution { public boolean isPalindrome(String s) { int n = s.length(); char x,y; for(int i=0,j=n-1; i x = s.charAt(i); y = s.charAt(j);原创 2014-12-30 08:37:27 · 467 阅读 · 0 评论 -
Leetcode[Easy] - Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { String pre = ""; if(strs.length return pre; else pre = strs[0];原创 2014-12-30 09:27:31 · 412 阅读 · 0 评论 -
Unique Paths II - Leetcode
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2015-02-05 08:10:17 · 362 阅读 · 0 评论 -
Rotate Image - Leetcode
掌握规律:90°旋转,两个方法:方法1 : 水平投影,交换副对角线;方法2: 垂直投影,交换主对角线;下面使用方法1:因为主对角线交换就是A[i][j] A[j][i]public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(原创 2015-02-06 07:49:42 · 366 阅读 · 0 评论 -
Leetcode[Easy] - Remove Nth Node From End of List
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }原创 2014-12-30 06:57:01 · 393 阅读 · 0 评论 -
Leetcode - Next Permutation
public class Solution { public void nextPermutation(int[] num) { int i = num.length - 2;if (i return;while (i > 0) {if (num[i] break;}i--;}int pivot = num[i];int原创 2015-01-16 11:47:37 · 353 阅读 · 0 评论 -
* Permutation Sequence - Leetcode
public class Solution { public String getPermutation(int n, int k) { int[] num = new int[n]; for(int i=1; i<=n; i++) num[i-1]=i; for(int原创 2015-02-06 07:25:31 · 403 阅读 · 0 评论