Menu Sidebar
Menu

Archive: August 9, 2015

[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; }

[LintCode] Longest Common Substring

public int longestCommonSubstring(String A, String B) { // write your code here int max = 0; int[][] dp = new int[A.length()+1][B.length()+1]; for(int i = 1 ; i <= A.length(); i++) { for(int j = 1 ; j <= B.length(); j++) { if(A.charAt(i-1) == B.charAt(j-1)) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = 0; max = […]

[LintCode] Anagrams

public List<String> anagrams(String[] strs) { // write your code here List<String> res = new ArrayList<String>(); if(strs.length == 0 || strs == null) return res; HashMap<Integer,ArrayList<String>> maps = new HashMap<Integer,ArrayList<String>>(); for(int i = 0 ; i < strs.length; i++) { String s = strs[i]; int[] count = new int[26]; for(int j = 0; j < s.length(); […]

[LintCode] strStr

/** * Returns a index to the first occurrence of target in source, * or -1 if target is not part of source. * @param source string to be scanned. * @param target string containing the sequence of characters to match. */ public int strStr(String source, String target) { //write your code here if(target == […]

[LintCode] Compare Strings

public boolean compareStrings(String A, String B) { // write your code here int[] count_A = new int[26]; for(int i = 0; i < A.length(); i++) count_A[A.charAt(i)-‘A’]++; int[] count_B = new int[26]; for(int i = 0; i < B.length(); i++) count_B[B.charAt(i)-‘A’]++; for(int i = 0 ; i < 26; i++) { if(count_A[i]<count_B[i]) return false; } return […]

Older Posts

书脊

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

August 2015
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31