摘要: public class Solution { public ArrayList solveNQueens(int n) { ArrayList res = new ArrayList(); dfs(n,0,new int[n],res); retur... 阅读全文
posted @ 2014-02-06 14:19 krunning 阅读(141) 评论(0) 推荐(0)
摘要: 1 public class Solution { 2 public double pow(double x, int n) { 3 if(x==0||x==1) return x; 4 if(n<0)return 1/helper(x,-1*n); 5 else return helper(x,n); 6 } 7 public double helper(double x,int n){ 8 if(n==0) return 1; 9 double res = helper(x,n/2);... 阅读全文
posted @ 2014-02-06 14:18 krunning 阅读(111) 评论(0) 推荐(0)
摘要: Given two numbers represented as strings, return multiplication of the numbers as a string. 1 public class Solution { 2 public String multiply(String num1, String num2) { 3 if(num1.equals("0")||num2.equals("0")) return "0"; 4 int len1 = num1.length(); 5 int len2 = num2. 阅读全文
posted @ 2014-02-06 14:17 krunning 阅读(121) 评论(0) 推荐(0)
摘要: 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]. 1 public class Solution { 2 public ArrayList> permute(int[] num) { 3 int len = num.length; 4 ArrayList> res = ne... 阅读全文
posted @ 2014-02-06 14:16 krunning 阅读(141) 评论(0) 推荐(0)
摘要: Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool i 阅读全文
posted @ 2014-02-06 14:15 krunning 阅读(96) 评论(0) 推荐(0)
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination. 1 public class Solution { 2 public ArrayList> combinationSum2(int[] candidates, int target) { 3 ... 阅读全文
posted @ 2014-02-06 14:13 krunning 阅读(223) 评论(0) 推荐(0)
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space. 1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 if(A.length0){18 ... 阅读全文
posted @ 2014-02-06 14:13 krunning 阅读(114) 评论(0) 推荐(0)
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times. 1 public class Solution { 2 public ArrayList> combinationSum(int[] candidates, int target) {... 阅读全文
posted @ 2014-02-06 14:12 krunning 阅读(241) 评论(0) 推荐(0)
摘要: Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'. 1 public class Solution { 2 public void solveSudoku(char[][] board) { 3 solve(board); 4 } 5 public boolean solve(char[][]board){ 6 for(int i=0;i<9;i++){ 7 ... 阅读全文
posted @ 2014-02-06 14:11 krunning 阅读(199) 评论(0) 推荐(0)
摘要: Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the ord... 阅读全文
posted @ 2014-02-06 14:10 krunning 阅读(111) 评论(0) 推荐(0)
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand. 1 public class Solution { 2 //all is equal in sorted array 3 public int search(int[] A, int target) { 4 if(A.length=A[start]){//don't forget the equal10 if(target=A[start]){11 ... 阅读全文
posted @ 2014-02-06 14:09 krunning 阅读(127) 评论(0) 推荐(0)
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2014-02-06 14:08 krunning 阅读(135) 评论(0) 推荐(0)
摘要: Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack. 1 public class Solution { 2 public String strStr(String haystack, String needle) { 3 if(haystack==null) return null; 4 int len1= haystack.length(); 5 int len2 = needl... 阅读全文
posted @ 2014-02-06 14:07 krunning 阅读(119) 评论(0) 推荐(0)
摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6. 1 public class Solution { 2 public int maxSubArray(int[] A) { 3 if(A.length... 阅读全文
posted @ 2014-02-06 14:06 krunning 阅读(177) 评论(0) 推荐(0)
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 1 public class Solution { 2 public TreeNode sortedArrayToBST(int[] num) { 3 if(num.length==0) return null; 4 return get(num,0,num.length-1); 5 } 6 public TreeNode get(int []n... 阅读全文
posted @ 2014-02-06 14:05 krunning 阅读(146) 评论(0) 推荐(0)
摘要: 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. 1 public class Solution { 2 public int removeElement(int[] A, int elem) { 3 int p1 = 0; 4 ... 阅读全文
posted @ 2014-02-06 14:00 krunning 阅读(85) 评论(0) 推荐(0)
摘要: Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is.You may not alter the values in the nodes, only nodes itself may be changed.Only constant memory is allowed 阅读全文
posted @ 2014-02-06 13:59 krunning 阅读(240) 评论(0) 推荐(0)
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2014-02-06 13:58 krunning 阅读(111) 评论(0) 推荐(0)
摘要: Divide two integers without using multiplication, division and mod operator. 1 public class Solution { 2 public int divide(int dividend, int divis... 阅读全文
posted @ 2014-02-06 05:41 krunning 阅读(113) 评论(0) 推荐(0)
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed. 1 public class Solut 阅读全文
posted @ 2014-02-06 05:39 krunning 阅读(87) 评论(0) 推荐(0)
摘要: 1 /* Min heap*/ 2 public class Solution { 3 public ListNode mergeKLists(ArrayList lists) { 4 if(lists.size() comparator = new Comparator(){ 7 public int compare(ListNode m,ListNode n){ 8 return m.val-n.val; 9 }10 };11 PriorityQueue... 阅读全文
posted @ 2014-02-06 05:37 krunning 阅读(104) 评论(0) 推荐(0)
摘要: Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()" 1 public class Solution { 2 public ArrayList generateParen 阅读全文
posted @ 2014-02-06 05:35 krunning 阅读(110) 评论(0) 推荐(0)
摘要: Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not. 1 阅读全文
posted @ 2014-02-06 05:34 krunning 阅读(94) 评论(0) 推荐(0)
摘要: Given a linked list, remove thenthnode from the end of list and return its head. 1 public class Solution { 2 public ListNode removeNthFromEnd(ListNode head, int n) { 3 ListNode safe = new ListNode(-1); 4 safe.next = head; 5 ListNode p1 = safe; 6 for(int i=0;i<=n;i... 阅读全文
posted @ 2014-02-06 05:33 krunning 阅读(99) 评论(0) 推荐(0)
摘要: Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q 阅读全文
posted @ 2014-02-06 05:30 krunning 阅读(281) 评论(0) 推荐(0)
摘要: Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero. 1 public class Solution { 2 public ArrayList> threeSum(int[] num) { 3 ArrayList> res = new ArrayList>(); 4 if(num.lengthsum){16 ... 阅读全文
posted @ 2014-02-06 05:28 krunning 阅读(120) 评论(0) 推荐(0)
摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 1 public class Solution { 2 public int romanToInt(String s) { 3 if(s.length()0 && get(s.charAt(i-1))<cur) 8 res += cur-2*get(s.charAt(i-1)); 9 else{10 ... 阅读全文
posted @ 2014-02-06 05:03 krunning 阅读(99) 评论(0) 推荐(0)
摘要: Given an integer, convert it to a roman numeral. 1 public class Solution { 2 public String intToRoman(int num) { 3 char[] sym = {'I','V','X','L','C','D','M'}; 4 int scale = 1000; 5 StringBuilder sb = new StringBuilder(); 6 for(int i=6;i>=0;i 阅读全文
posted @ 2014-02-06 05:01 krunning 阅读(108) 评论(0) 推荐(0)
摘要: Implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char 阅读全文
posted @ 2014-02-06 04:59 krunning 阅读(236) 评论(0) 推荐(0)
摘要: Implementatoito convert a string to an integer. 1 public class Solution { 2 public int atoi(String str) { 3 char[] s = str.trim().toCharArray(); 4 if(s.length==0){ 5 return 0; 6 } 7 long num = 0; 8 int sign =1; 9 int len = s.length;10 ... 阅读全文
posted @ 2014-02-06 04:58 krunning 阅读(118) 评论(0) 推荐(0)
摘要: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 1 public class Solution { 2 public int reverse(int x) { 3 int sign = 1; 4 if(x0){ 9 res = 10*res+x%10;10 x /=10;11 }12 if(res<0) return -1;13 re... 阅读全文
posted @ 2014-02-06 04:57 krunning 阅读(91) 评论(0) 推荐(0)
摘要: Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring. 1 public class Solution { 2 public String longestPalindrome(String s) { 3 if(s.length()=0;i--){10 for(int... 阅读全文
posted @ 2014-02-06 04:55 krunning 阅读(104) 评论(0) 推荐(0)
摘要: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8 1 public class Solut 阅读全文
posted @ 2014-02-06 04:53 krunning 阅读(93) 评论(0) 推荐(0)
摘要: 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)). 1 public class Solution { 2 public double findMedianSortedArrays(int A[], int B[]) { 3 int len1 = A.length; 4 int le... 阅读全文
posted @ 2014-02-06 04:52 krunning 阅读(264) 评论(0) 推荐(0)
摘要: 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 index1 must be less than index2. Please note that your returned answers (both index1 and index2) are 阅读全文
posted @ 2014-02-06 04:51 krunning 阅读(132) 评论(0) 推荐(0)