Menu Sidebar
Menu

Leetcode

Find the Duplicate Number

双指针. 找环 public class FloydsCycleDetection { public static int floyd(int[] a){ int s = a[0]; int f = a[0]; do { s = a[s]; f = a[a[f]]; } while (s!=f); s = a[0]; while (s != f){ s = a[s]; f = a[f]; } return f; } public static void main(String[] args) { int[] t […]

H-Index

public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) return 0; Arrays.sort(citations); // 0 1 3 5 6 for(int i = 0; i < citations.length; i++){ int index = citations.length – i; if(citations[i] >= index) return index; } return 0; } }  

Missing Number 位运算

public int missingNumber(int[] nums) { if(nums.length == 0 || nums == null) return 0; int missing = 0; for(int i = 0; i <= nums.length; i++) missing ^= i; for(int i = 0 ; i < nums.length; i++) { missing ^= nums[i]; } return missing; }  

Sliding Window Maximum 单调队列解区间最值

经典面试问题, 终于出现在Leetcode了, 写完后看了下discuss, 基本大家写的都差不多. 可见这题多容易考, 我身边的朋友就有被问过这题的. 题目很简单, 就是给一个大小为k的窗口, 然后给一个数组, 问在k下的最值是什么. 就用Leetcode的例子吧. 单调队列解区间最值 nums 输入数组 k 队列窗口大小 Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. Window position Max ————— —– [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 […]

Newer Posts

书脊

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

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