
Recursive
文章平均质量分 81
violet_program
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode: Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region . For example, X X X X X O O X X X O原创 2013-07-16 11:39:20 · 707 阅读 · 0 评论 -
Valid Sukodu
Normal validation solution checking by row, column and square takes O(n^3) time, where n = 9. This solution takes O(n^2) time, but O(n^2) space. public boolean isValidSudoku(char[][] board) {原创 2013-05-23 05:03:38 · 606 阅读 · 0 评论 -
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinatio原创 2013-05-24 03:47:21 · 515 阅读 · 0 评论 -
Sudoku Solver
public class Solution { public boolean isValid(char[][] board, int x, int y){ int i, j; for(i = 0; i < 9; i++) if(i != x && board[i][y] == board[x][y]) return false; for(j = 0; j < 9;原创 2013-05-23 12:08:08 · 525 阅读 · 0 评论 -
Leetcode: Sum Root to Leaf Number
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum原创 2013-07-16 11:09:04 · 578 阅读 · 0 评论 -
Word Ladder
package Algorithm; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; public class WordLadder { public int ladderLength(String start, String end, HashSet dict)转载 2013-03-26 05:09:40 · 679 阅读 · 0 评论 -
3Sum Closest
Pay attention to this one. Passed after several bug fixes. public class Solution { public int threeSumClosest(int[] num, int target) { // Start typing your Java solution below // DO NOT w原创 2013-04-03 03:12:10 · 626 阅读 · 0 评论 -
3Sum
public class Solution { public ArrayList> threeSum(int[] num) { // Start typing your Java solution below // DO NOT write main() function ArrayList> res = new ArrayList>(); if(num.length ==原创 2013-04-02 05:21:52 · 577 阅读 · 0 评论 -
Search For A Range
public class Solution { public int[] searchRange(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function int[] res = {-1, -1}; if(A.length == 0)转载 2013-05-22 11:40:16 · 482 阅读 · 0 评论 -
Search in Rotated Sorted Array
public class Solution { public int search(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function if(A.length == 0) return -1; int begin = 0; i转载 2013-05-22 10:35:16 · 538 阅读 · 0 评论 -
Two Sum
import java.util.Hashtable; public class Solution { public int[] twoSum(int[] numbers, int target) { // Start typing your Java solution below // DO NOT write main() function int[] res = {0,原创 2013-04-01 09:07:13 · 782 阅读 · 4 评论 -
Leetcode: Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6. Pay attenti原创 2013-07-16 05:32:42 · 713 阅读 · 1 评论 -
Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr原创 2013-06-20 12:14:37 · 874 阅读 · 0 评论 -
Median of Two Sorted Arrays
This solution O(lg(m + n)) is not good for interview. Try kth smallest element solution extension. public class MedianTwoSortedArrays { private double findMedian(int A[], int B[], int left, i转载 2013-05-09 04:20:09 · 499 阅读 · 0 评论 -
Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number原创 2013-05-24 03:10:07 · 526 阅读 · 0 评论 -
NQueens II
Same idea. public class Solution { public int counter; // 检测前面行可以放Q的位置是不是和目前位置(place[row]=j)冲突,即位置[i][place[i]]和位置[row][j]是否冲突 public boolean check(int row, int[] place) { for (int i = 0; i <原创 2013-06-05 10:47:01 · 691 阅读 · 0 评论 -
Leetcode: Longest Consecutive Sequence
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,原创 2013-07-16 10:55:28 · 653 阅读 · 0 评论 -
SubsetII
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not contain duplica转载 2013-06-25 12:21:04 · 600 阅读 · 0 评论 -
Generate Parentheses
public class Solution { public ArrayList res; public ArrayList generateParenthesis(int n) { // Start typing your Java solution below // DO NOT write main() function res = new ArrayList();转载 2013-05-16 03:18:07 · 482 阅读 · 0 评论 -
Letter Combinations of a Phone Number
public class Solution { ArrayList res = null; public ArrayList letterCombinations(String digits) { // Start typing your Java solution below // DO NOT write main() function res = new A转载 2013-05-15 03:17:21 · 514 阅读 · 0 评论 -
Regular Expression Matching
public class Solution { public boolean isMatch(String s, String p) { // Start typing your Java solution below // DO NOT write main() function if (s.equals(p)) return true; else if (s.le原创 2013-05-14 03:32:43 · 524 阅读 · 0 评论 -
4Sum
First solution (based on 3 sum). O(n^3). public class Solution { public ArrayList> fourSum(int[] num, int target) { // Start typing your Java solution below // DO NOT write main()原创 2013-04-03 04:00:53 · 651 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations ofk numbers out of 1 ...n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]原创 2013-06-17 23:22:29 · 661 阅读 · 0 评论 -
N-Queens
Time Limit Exceeded, even for small judge. I guess it is because this solution needs n^2 space to store status for each Q placement. public class Solution { ArrayList res; public ArrayList原创 2013-06-05 05:36:10 · 685 阅读 · 0 评论 -
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].原创 2013-05-29 03:31:14 · 543 阅读 · 0 评论 -
Permutations
Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. public class Solut原创 2013-05-29 03:16:13 · 576 阅读 · 0 评论 -
Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets. For example, I原创 2013-06-17 23:57:10 · 513 阅读 · 0 评论 -
Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically n原创 2013-06-18 02:27:44 · 556 阅读 · 0 评论 -
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123""132""213""231""312""原创 2013-06-11 03:33:00 · 635 阅读 · 0 评论 -
Maximum Subarray
Kadane Algorithm O(n) Not working for all negative number arrays public class Solution { public int maxSubArray(int[] A) { // Start typing your Java solution below // DO NOT write main()原创 2013-06-06 05:10:34 · 573 阅读 · 0 评论