Menu Sidebar
Menu

LintCode

[LintCode] 3 Sum

public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(numbers.length == 0 || numbers == null) return res; Arrays.sort(numbers); for(int i = 0 ; i < numbers.length – 2; i++) { if(i != 0 && numbers[i] == numbers[i-1]) continue; int l = i+1; int r = numbers.length – 1; […]

[LintCode] 3 Sum Closest

public int threeSumClosest(int[] numbers ,int target) { // write your code here Arrays.sort(numbers); if(numbers.length == 0 || numbers == null) return 0; int close = Integer.MAX_VALUE; for(int i = 0; i < numbers.length – 2; i++) { int j = i + 1; int k = numbers.length – 1; while(j < k) { int sum […]

[LintCode] First Missing Positive

public int firstMissingPositive(int[] A) { // write your code here if(A == null || A.length == 0) return 1; int i = 0; int n = A.length; while(i < n) { if(A[i] >= 0 && A[i] < n && A[A[i]] != A[i]) swap(A, i, A[i]); else i++; } int k = 1; while(k < n […]

[LintCode] Product of Array Exclude Itself

public ArrayList<Long> productExcludeItself(ArrayList<Integer> A) { ArrayList<Long> res = new ArrayList<Long>(); if(A.size()==0 || A == null) return res; long p = 1; for(int i = 0 ; i < A.size(); i++) { res.add(p); p *= A.get(i); } p = A.get(A.size()-1); for(int i = A.size()-2; i >= 0; i–) { res.set(i,p*res.get(i)); p *= A.get(i); } return res; […]

[LintCode] Merge Sorted Array

public void mergeSortedArray(int[] A, int m, int[] B, int n) { // write your code here int i = m – 1; int j = n – 1; int index = m+n – 1; while(i >= 0 && j >= 0) { if(A[i] < B[j]) { A[index] = B[j]; j–; index –; } else{ A[index] […]

[LintCode] Subarray Sum

public ArrayList<Integer> subarraySum(int[] nums) { // write your code here ArrayList<Integer> res = new ArrayList<Integer>(); if(nums.length == 0 || nums == null) return res; HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); int sum = 0; for(int i = 0 ; i < nums.length; i++) { sum += nums[i]; if(nums[i] == 0){ res.add(i); res.add(i); break; } if(sum == […]

[LintCode] Remove Element

public int removeElement(int[] A, int elem) { // write your code here int res = 0; if(A.length == 0 || A == null) return res; for(int i = 0 ; i < A.length; i++) { if(A[i] != elem) A[res++] = A[i]; } return res; }

[LintCode] String to Integer(atoi)

public int atoi(String str) { // write your code here if(str == null || str.length() == 0) return 0; str = str.trim(); int i = 0; char sign = ‘+’; if(str.charAt(0) == ‘+’) i++; else if(str.charAt(0) == ‘-‘){ i++; sign = ‘-‘; } int res = 0; while(i < str.length() && str.charAt(i) >=’0′ && str.charAt(i)<=’9’) […]

[LintCode] Longest Common Prefix

public String longestCommonPrefix(String[] strs) { // write your code here if(strs == null || strs.length == 0) return “”; String prefix = strs[0]; for(int i = 1; i < strs.length; i++) { int j = 0; while(j < prefix.length() && j < strs[i].length() && prefix.charAt(j)==strs[i].charAt(j)) j++; prefix = prefix.substring(0,j); } return prefix; }

Newer Posts
Older Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930