
Other Algorithms
文章平均质量分 67
violet_program
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguous.转载 2013-06-12 05:32:31 · 561 阅读 · 0 评论 -
Leetcode: Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2013-12-31 03:45:44 · 621 阅读 · 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 评论 -
Longest Substring Without Repeating Characters
public class Solution { public int lengthOfLongestSubstring(String s) { // Start typing your Java solution below // DO NOT write main() function int start = 0, res = 0; int[] pos = new int转载 2013-05-10 04:40:49 · 421 阅读 · 0 评论 -
Palindrome Number
public class Solution { public boolean isPalindrome(int x) { // Start typing your Java solution below // DO NOT write main() function if(x < 0) return fals转载 2013-05-13 12:29:48 · 486 阅读 · 0 评论 -
Container With Most Water
public class Solution { public int maxArea(int[] height) { // Start typing your Java solution below // DO NOT write main() function int maxArea = 0; int left = 0; int right = height.leng转载 2013-05-14 05:12:34 · 484 阅读 · 0 评论 -
Longest Valid Parentheses
public class Solution { public int longestValidParentheses(String s) { // Start typing your Java solution below // DO NOT write main() function int len = s.length(); int max = 0; Stack s转载 2013-05-16 01:47:32 · 491 阅读 · 0 评论 -
Search Insert Position
public class Solution { public int searchInsert(int[] A, int target) { // Start typing your Java solution below // DO NOT write main() function if(A.length == 0) return 0; int left = 0;原创 2013-05-23 04:39:02 · 493 阅读 · 0 评论 -
First Missing Positive
public class Solution { public int firstMissingPositive(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if(len < 1) return 1; f转载 2013-05-24 04:47:27 · 502 阅读 · 0 评论 -
Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],转载 2013-05-24 05:18:05 · 526 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi原创 2013-06-10 21:53:38 · 537 阅读 · 0 评论 -
Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.public class Solution { public int sqrt(int x) { // Start typing your Java solution below // DO NOT write main() function转载 2013-06-13 05:13:45 · 506 阅读 · 0 评论 -
Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using原创 2013-10-04 01:09:55 · 1538 阅读 · 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 评论 -
Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if(prices.length == 0) return 0; int profit = 0;原创 2013-04-05 02:08:22 · 733 阅读 · 0 评论 -
Leetcode: Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr转载 2013-06-21 03:42:48 · 553 阅读 · 0 评论 -
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.Solution 1. Using Largest Rectangle in Histogram, O(n^2). Great solution... Hav原创 2013-06-19 04:55:57 · 562 阅读 · 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,2,3]原创 2013-06-18 04:44:58 · 508 阅读 · 0 评论 -
Leetcode: Restore IP Address
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order does原创 2013-06-27 13:24:09 · 957 阅读 · 0 评论 -
Maximum Subarray
Kadane Algorithm O(n) Not working for all negative number arrayspublic 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 评论 -
Valid a Palindrome
public class ValidPalindrome { /** * Given a string, determine if it is a palindrome, considering only * alphanumeric characters and ignoring cases. * * For example, "A man, a plan, a canal转载 2013-02-21 11:48:35 · 391 阅读 · 0 评论 -
Word Ladder II
public class Solution { public ArrayList> res; public String end; public int target_length; public ArrayList temp_list; public ArrayList> findLadders(String start, String end, HashSet dict)原创 2013-03-31 13:23:05 · 803 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
Left[i] means for current day i, best buy and sell value between 0~i;right[i] means for current day i, best buy and sell value between i~length-1.public class Solution { public int maxProfit原创 2013-04-05 03:58:54 · 798 阅读 · 0 评论 -
Reverse Integer
public class Solution { public int reverse(int x) { // Start typing your Java solution below // DO NOT write main() function boolean neg = false; if (x < 0) { x = -x; neg = tr转载 2013-05-11 05:02:32 · 469 阅读 · 0 评论 -
Valid Parentheses
public class Solution { public boolean isValid(String s) { // Start typing your Java solution below // DO NOT write main() function Stack stack = new Stack(); for(int i = 0; i < s.length()转载 2013-05-15 22:45:14 · 467 阅读 · 0 评论 -
Divide Two Integers
dividend = divisor*2^0 + divisor*2^1 + ... + divisor*2^n + mod; (modpublic class Solution { public int divide(int dividend, int divisor) { // Start typing your Java solution below // DO NO原创 2013-05-21 23:21:35 · 587 阅读 · 0 评论 -
Spiral Matrix
public class Solution { public ArrayList spiralOrder(int[][] matrix) { // Start typing your Java solution below // DO NOT write main() function ArrayList res = new ArrayList(); int m = mat转载 2013-06-07 12:54:38 · 668 阅读 · 0 评论 -
Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 ton2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7,原创 2013-06-11 02:13:34 · 504 阅读 · 0 评论 -
Plus One
Given a number represented as an array of digits, plus one to the number.public class Solution { public int[] plusOne(int[] digits) { // Start typing your Java solution below // DO NOT wri原创 2013-06-13 03:43:49 · 518 阅读 · 0 评论 -
Text Justification
Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is原创 2013-06-13 04:52:49 · 557 阅读 · 0 评论 -
Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case where path = "/原创 2013-06-13 11:28:10 · 554 阅读 · 0 评论 -
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC".原创 2013-06-15 05:11:23 · 812 阅读 · 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 评论 -
Zigzag Conversion
public class Solution { public String convert(String s, int nRows) { // Start typing your Java solution below // DO NOT write main() function StringBuilder sb = new StringBuilder("");转载 2013-05-11 04:18:17 · 523 阅读 · 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 评论 -
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 评论 -
Best Time to Buy and Sell Stock II
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if (prices.length < 2) return 0; int profit = 0;原创 2013-04-05 03:25:41 · 576 阅读 · 0 评论 -
Longest Palindromic Substring
O(n^2)public class Solution { public String longestPalindrome(String s) { // Start typing your Java solution below // DO NOT write main() function int len = s.length(); if(len == 0)转载 2013-05-10 22:40:53 · 572 阅读 · 0 评论 -
String to Integer (atoi)
public class Solution { public int atoi(String str) { // Start typing your Java solution below // DO NOT write main() function if (str == null || str.length() == 0) return 0; int index原创 2013-05-13 11:25:31 · 506 阅读 · 0 评论 -
Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { // Start typing your Java solution below // DO NOT write main() function if (strs.length == 0) return ""; for转载 2013-05-15 02:49:03 · 427 阅读 · 0 评论