
LeetCode String+Array
文章平均质量分 73
Spencer_Lin
If you fight for your dream, one day....
展开
-
Count and Say Java
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "o原创 2014-08-15 14:52:42 · 342 阅读 · 0 评论 -
Reverse Words in a String Java
Idea: the regular approach is to us split method in Java that split each word by space " ", then reverse each word from back to front. Watch out for the special cases. remember to elimi原创 2014-08-26 09:54:21 · 459 阅读 · 0 评论 -
Plus One Java
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.Have y原创 2014-08-28 08:57:26 · 420 阅读 · 0 评论 -
Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?原创 2014-08-29 10:42:23 · 305 阅读 · 0 评论 -
Spiral Matrix Java
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You原创 2014-08-11 16:01:37 · 347 阅读 · 0 评论 -
Pascal's Triangle Java
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Have you原创 2014-08-29 10:40:21 · 346 阅读 · 0 评论 -
Search a 2D Matrix Java
Search a 2D Matrix :Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The原创 2014-08-14 08:36:35 · 308 阅读 · 0 评论 -
Longest Common Prefix Java
Write a function to find the longest common prefix string among st an array of strings.原创 2014-08-15 16:27:36 · 391 阅读 · 0 评论 -
Longest Valid Parentheses Java
/*Key to solve: * A Valid Parentheses pair contain only one left and right bracket. * 1. store index of '(' in the stack * 2. check stack whether is empty when ')' was found: * two case as原创 2014-07-28 09:12:27 · 455 阅读 · 0 评论 -
Minimum Window Substring Java
public class Solution { public String minWindow(String S, String T) { String output=""; if(S.length()==0 || T.length()==0) return ""; HashMap map=new HashMap(); int matchCount=0;原创 2014-08-18 09:21:14 · 883 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2014-08-16 14:59:17 · 957 阅读 · 0 评论 -
Anagrams Java
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.public class Solution { public ArrayList anagrams(String[] st原创 2014-08-16 13:56:11 · 372 阅读 · 0 评论 -
Substring with Concatenation of All Words Java
public class Solution { public ArrayList findSubstring(String S, String[] L) { ArrayList result=new ArrayList(); if(S.length()==0 || L.length==0) return result; HashMap m原创 2014-08-18 15:08:16 · 353 阅读 · 0 评论 -
Rotate Image Java
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Have you been asked this question in an i原创 2014-08-29 11:14:56 · 459 阅读 · 0 评论 -
Palindrome Partitioning II Java
public static int minCut(String s) { if(s==null || s.length()==0) return -1; int len=s.length(); boolean[][] dict=palinDict(s); int[] res=new int[len+1]; res[0]原创 2014-09-17 14:38:38 · 456 阅读 · 0 评论 -
Palindrome Partitioning Java
Idea: Recursive-loop + dynamic programming + backtracking Combination of Longest Palindromic Substring & Word Break II There are two major steps: 1.palindromic dictionary that using sam原创 2014-09-17 14:15:10 · 332 阅读 · 0 评论 -
Word Break II Java
Two Approach: NP-problem Solution#1: brute force of Recursive-loop, The appraoch is pretty similar with N-Queens problem Maintain a result List, traverse all the sub-String list Ca原创 2014-09-15 17:40:24 · 551 阅读 · 0 评论 -
Add Binary Java
sadspublic class Solution { public String addBinary(String a, String b) { if(a.length()==0) return b; if(b.length()==0) return a; StringBuilder strBuilder=new StringBui原创 2014-08-21 17:09:40 · 406 阅读 · 0 评论 -
Word Break LeetCode Java
Idea: Dynamic programming + Nested Loop outer loop: i start from 0 to N inner loop: j start from 0 to i Use a boolean array in size of N to record the previous info res[i+1] pu原创 2014-09-15 17:42:20 · 494 阅读 · 0 评论 -
Longest Consecutive Sequence Java
public class Solution { public int longestConsecutive(int[] num) { if(num.length==0) return 0; int maxCount=0; HashSet set=new HashSet(); //initial the HashSet wit原创 2014-08-18 13:28:34 · 351 阅读 · 0 评论 -
Search in Rotated Sorted Array Java
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 return it原创 2014-08-14 08:44:34 · 284 阅读 · 0 评论 -
Median of Two Sorted Arrays Java
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Key to solve: Bisection + Recur原创 2014-08-15 14:48:23 · 414 阅读 · 0 评论 -
Remove Duplicates from Sorted Array Java
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2014-08-20 14:13:30 · 324 阅读 · 0 评论 -
Remove Element Java
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2014-08-20 14:53:21 · 375 阅读 · 0 评论 -
Set Matrix Zeroes Java
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2014-09-04 16:39:11 · 576 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
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,原创 2014-08-20 14:22:23 · 332 阅读 · 0 评论 -
Merge Sorted Array Java
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from原创 2014-08-20 14:56:08 · 395 阅读 · 0 评论 -
3Sum Java
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2014-08-10 08:44:35 · 373 阅读 · 0 评论 -
4Sum Java
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements原创 2014-08-10 14:44:45 · 511 阅读 · 0 评论 -
Two Sum Java
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where原创 2014-08-20 15:12:47 · 381 阅读 · 0 评论 -
3Sum Closest Java
Solution: Time O(n^2) same idea as 3 Sum find minimum difference: abs(sum-target)原创 2014-08-20 15:41:03 · 338 阅读 · 0 评论 -
Sort Colors Java
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2014-08-20 14:41:55 · 410 阅读 · 0 评论 -
First Missing Positive Java
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant原创 2014-08-22 16:56:17 · 451 阅读 · 0 评论 -
Length of Last Word Java
Goal: find Length of Last Word the length will depend on the index of last Character - last index of spacepublic class Solution { public int lengthOfLastWord(String s) { if(s.leng原创 2014-08-22 17:05:08 · 335 阅读 · 0 评论 -
Implement strStr() Java
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. Goal: Determine whether needle is substring of haystack or not &原创 2014-08-24 15:21:06 · 498 阅读 · 0 评论 -
Longest Palindromic Substring Java
Idea: Nested loop: Outer loop: i start from back to front inner loop: j start from i to the endStated more formally below:Define P[i][j] ← true iffthe substring Si … Sj is a palindrome原创 2014-09-15 16:22:03 · 400 阅读 · 0 评论