Menu Sidebar
Menu

LintCode

[LintCode] Maximal Square

public int maxSquare(int[][] matrix) { // write your code here if(matrix == null || matrix.length == 0) return 0; int[] height = new int[matrix[0].length]; int res = 0; for(int i = 0 ; i < matrix.length; i++) { for(int j = 0; j < matrix[0].length; j++) { if(matrix[i][j] == 1) height[j]++; else height[j] = 0; […]

[LintCode] Linked List Cycle

public boolean hasCycle(ListNode head) { // write your code here if(head == null) return false; ListNode fast = head; ListNode slow = head; while(fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; }

[LintCode] Nuts & Bolts Problem

public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) { // write your code here sort(nuts,bolts,0,nuts.length-1, compare); } public void sort(String[] nuts, String[] bolts, int l, int h, NBComparator compare) { if(l < h){ int p = partition(nuts, l,h, bolts[h], compare); partition(bolts, l,h,nuts[p], compare); sort(nuts, bolts, l, p-1,compare); sort(nuts, bolts, p+1, h,compare); } } public int […]

[LintCode] Longest Palindromic Substring

public String longestPalindrome(String s) { // Write your code here if(s.length() == 0 || s == null) return “”; int start = 0; int end = 0; for(int i = 0; i < s.length(); i++) { int len1 = expand(s, i, i); int len2 = expand(s, i, i+1); int len = Math.max(len1, len2); if(len > […]

[LintCode] Number of Islands

public int numIslands(boolean[][] grid) { // Write your code here if(grid == null) return 0; int res = 0; for(int i = 0 ; i < grid.length; i++) { for(int j = 0; j < grid[0].length; j++) { if(grid[i][j]) res++; dfs(grid,i,j); } } return res; } public void dfs(boolean[][] m, int i, int j) { […]

[LintCode] Valid Parentheses

public boolean isValidParentheses(String s) { // Write your code here Stack<Character> st = new Stack<Character>(); if(s.length() == 0 || s == null) return false; for(int i = 0 ; i < s.length(); i++) { if(s.charAt(i) == ‘(‘ || s.charAt(i) == ‘[‘ || s.charAt(i) == ‘{‘) st.push(s.charAt(i)); else{ if(st.isEmpty()) return false; if(s.charAt(i) == ‘)’ && st.peek() […]

Newer Posts
Older Posts

书脊

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

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031