## [LintCode] Update Bits

class Solution { /** *@param n, m: Two integer *@param i, j: Two bit positions *return: An integer */ public int updateBits(int n, int m, int i, int j) { // write your code here for(int k = i ; k <= j; k++) { n = n & ~(1 << k); // set kth […]

## Recover Rotated Sorted Array

public void recoverRotatedSortedArray(ArrayList<Integer> nums) { // write your code for(int i = 0; i < nums.size()-1; i++){ if(nums.get(i) > nums.get(i+1)){ reverse(nums,0,i); reverse(nums,i+1,nums.size()-1); reverse(nums,0,nums.size()-1); break; } } } public void reverse(ArrayList<Integer> nums, int i, int j) { while(i < j) { Integer tmp = nums.get(i); nums.set(i,nums.get(j)); nums.set(j,tmp); i++; j–; } }

## [LintCode] Copy Books

public int copyBooks(int[] pages, int k) { // write your code here int l = 0; int r = 9999999; while( l <= r){ int mid = l + (r – l) / 2; if(search(mid,pages,k)) r = mid-1; else l = mid+1; } return l; } public boolean search(int total, int[] page, int k) { […]

## [LintCode] Longest Increasing Continuous subsequence II

public int longestIncreasingContinuousSubsequenceII(int[][] A) { // Write your code here if(A == null || A.length == 0 || A[0].length == 0) return 0; int res = 0; n = A.length; m = A[0].length; int[][] dp = new int[n][m]; for(int i = 0 ; i < n; i++) for(int j = 0 ; j < m; […]

## [LintCode] Longest Increasing Continuous subsequence

public int longestIncreasingContinuousSubsequence(int[] A) { // Write your code here if(A == null || A.length == 0) return 0; if(A.length == 1)// corn case; return 1; int count = 1; int max = 0; for(int i = 1; i < A.length; i++) { if(A[i] > A[i-1]){ count++; }else{ count = 1; } max = Math.max(max,count); […]

## [LintCode] Count and Say

public String countAndSay(int n) { // Write your code here String s = “1”; for(int i = 1; i < n; i++) { StringBuffer sb = new StringBuffer(); int count = 1; for(int j = 1; j < s.length(); j++) { if(s.charAt(j) == s.charAt(j-1)) count++; else{ sb.append(count); sb.append(s.charAt(j-1)); count = 1; } } sb.append(count); sb.append(s.charAt(s.length()-1)); […]

## [LintCode] Segment Tree Query II

public int query(SegmentTreeNode root, int start, int end) { // write your code here if(root == null || root.end < start || root.start > end) return 0; if(root.start == root.end) return root.count; return query(root.left, start,end)+ query(root.right, start, end); }

## [LintCode] Segment Tree Query

public int query(SegmentTreeNode root, int start, int end) { // write your code here if(root == null || root.start > end || root.end < start) return 0; if(root.start == root.end) return root.max; return Math.max(query(root.left,start,end),query(root.right,start,end)); }