Menu Sidebar
Menu

LintCode

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

Partition Array by Odd and Even

给一个数组, 把奇数放左边, 偶数放右边. 其实就是一个quicksort的partition的改写, 改写的地方很少, 就是判断条件从 nums[i] 对 nums[k]的比较改成nums[i] % 2 != 0的比较. public void partitionArray(int[] nums) { if(nums.length == 0 || nums == null) return; int i = 0; int j = nums.length – 1; while(i < j) { while(i<j && nums[i] % 2 != 0) // 如果左指针的位置是奇数 i++; while(i< j && nums[j] % […]

Binary Tree Paths

打印所有的从root到leaf的path. 开始用的queue做的..后来看了下别的人的答案, 感觉都很简练.po一个别人的答案   public List<String> binaryTreePaths(TreeNode root) { List<String> res = new ArrayList<String>(); if(root == null) return res; if(root.left == null && root.right == null) res.add(root.val + “”); else{ if(root.left != null) res.addAll(binaryTreePaths(root.left)); if(root.right != null) res.addAll(binaryTreePaths(root.right)); for(int i = 0 ; i < res.size(); i++) res.set(i,root.val + “->” + res.get(i)); } return res; […]

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

Older Posts

书脊

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

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