Menu Sidebar
Menu

LintCode

[LintCode] Rotate String

public void rotateString(char[] str, int offset) { // write your code here if(offset == 0 || str.length == 0) return; offset = offset % str.length; reverse(str, 0, str.length – 1 – offset); reverse(str, str.length – offset, str.length – 1); reverse(str, 0 , str.length – 1); } public void reverse(char[] c, int i, int j) { […]

[LintCode] Majority Number

public int majorityNumber(ArrayList<Integer> nums) { // write your code int index = -1; int count = 0; for(int i = 0 ; i < nums.size(); i++) { if(count == 0) { index = i; count ++; } else { if(nums.get(i) == nums.get(index)) count++; else count–; } } return nums.get(index); }

[LintCode] Longest Words

ArrayList<String> longestWords(String[] dictionary) { // write your code here ArrayList<String> res = new ArrayList<String>(); if(dictionary == null || dictionary.length == 0) return res; int len = dictionary[0].length(); for(int i = 0; i < dictionary.length; i++) { if(dictionary[i].length() == len) res.add(dictionary[i]); else if(dictionary[i].length() > len){ len = dictionary[i].length(); res.clear(); res.add(dictionary[i]); } } return res; }

[LintCode] Invert Binary Tree

public void invertBinaryTree(TreeNode root) { // write your code here if(root == null) return; invertBinaryTree(root.left); invertBinaryTree(root.right); TreeNode left = root.left; root.left = root.right; root.right = left; }

[LintCode] Search a 2D Matrix

public boolean searchMatrix(int[][] matrix, int target) { // write your code here if(matrix == null || matrix.length == 0) return false; int i = 0; int j = matrix[0].length-1; while(i < matrix.length && j >=0) { if(target == matrix[i][j]) return true; else if(target < matrix[i][j]) j–; else i++; } return false; }

[LintCode] Permutations

/** * @param nums: A list of integers. * @return: A list of permutations. */ public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(nums == null || nums.size() == 0) return res; boolean[] visited = new boolean[nums.size()]; ArrayList<Integer> tmp = new ArrayList<Integer>(); dfs(res,visited, tmp, nums); return res; } […]

[LintCode] Subsets

public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> S) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(S == null || S.size() == 0) return res; Collections.sort(S); ArrayList<Integer> tmp = new ArrayList<Integer>(); res.add(tmp); dfs(res, S, tmp, 0); return res; } public void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> S, ArrayList<Integer> tmp, int pos) { for(int i = pos; i < […]

[LintCode] Fizz Buzz

public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++) { if (i % 15 == 0) { results.add(“fizz buzz”); } else if (i % 5 == 0) { results.add(“buzz”); } else if (i % 3 == 0) { results.add(“fizz”); } else { results.add(String.valueOf(i)); } } […]

[LintCode] Fibonacci

public int fibonacci(int n) { // write your code here int first = 0; if(n == 1) return first; int second = 1; if(n == 2) return second; int third = 0; for(int i = 3; i <=n; i++) { third = first+ second; first = second; second = third; } return third; }

Newer Posts
Older Posts

书脊

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

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