LeetCode(一)
数据流的中位数
class MedianFinder {
private Queue<Integer> maxHeap = new PriorityQueue(new Comparator<Integer>(){
@Override
public int compare(Integer i1, Integer i2){
return Integer.compare(i2, i1);
}
});
private Queue<Integer> minHeap = new PriorityQueue(new Comparator<Integer>(){
@Override
public int compare(Integer i1, Integer i2){
return Integer.compare(i1, i2);
}
});
// Adds a number into the data structure.
public void addNum(int num) {
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
//if(maxHeap.size() > minHeap.size())
if(maxHeap.size() - minHeap.size() == 1){
minHeap.offer(maxHeap.poll());
}
}
// Returns the median of current data stream
public double findMedian() {
return minHeap.size() > maxHeap.size()
? (double)minHeap.peek()
: (minHeap.peek() + maxHeap.peek())/2.0;
}
};
两个已排序数组的中位数
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
int mid1 = (m+n+1)/2;
int mid2 = (m+n+2)/2;
return (findKthNum(nums1,0,nums2,0,mid1) + findKthNum(nums1,0,nums2,0,mid2))/2.0;
}
public int findKthNum(int[] nums1,int start1,int[] nums2,int start2,int k){
if(start1>nums1.length-1){return nums2[start2+k-1];}
if(start2>nums2.length-1){return nums1[start1+k-1];}
if(k==1) return Math.min(nums1[start1],nums2[start2]);
int mid1 = Integer.MAX_VALUE,mid2=Integer.MAX_VALUE;
if(nums1.length-start1+1>k/2){mid1 = nums1[start1+k/2 - 1];}
if(nums2.length-start2+1>k/2){mid2 = nums2[start2+k/2 - 1];}
if(mid1<mid2){
return findKthNum(nums1,start1+k/2,nums2,start2,k-k/2);
}else{
return findKthNum(nums1,start1,nums2,start2+k/2,k-k/2);
}
}
}
旋转数组中找到最小的数字
public class Solution {
public int findMin(int[] nums) {
int len = nums.length;
int l=0;
int r=len-1;
while(l<r){
if(nums[l]<nums[r]){return nums[l];}
int mid = l + (r-l)/2;
if(nums[mid]>nums[r]){
l = mid + 1;
}else if(nums[mid]<nums[r]){
r = mid;
}else{
r--;
}
}
return nums[l];
}
}
旋转数组中搜索一个数字
public class Solution {
public boolean search(int[] nums, int target) {
int start = 0, end = nums.length - 1, mid = -1;
while(start <= end) {
mid = (start + end) / 2;
if (nums[mid] == target) {
return true;
}
//If we know for sure right side is sorted or left side is unsorted
if (nums[mid] < nums[end] || nums[mid] < nums[start]) {
if (target > nums[mid] && target <= nums[end]) {
start = mid + 1;
} else {
end = mid - 1;
}
//If we know for sure left side is sorted or right side is unsorted
} else if (nums[mid] > nums[start] || nums[mid] > nums[end]) {
if (target < nums[mid] && target >= nums[start]) {
end = mid - 1;
} else {
start = mid + 1;
}
//If we get here, that means nums[start] == nums[mid] == nums[end], then shifting out
//any of the two sides won't change the result but can help remove duplicate from
//consideration, here we just use end-- but left++ works too
} else {
end--;
}
}
return false;
}
}
归并两个排序链表
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null||l2==null) return l1==null?l2:l1;
ListNode head = null;
if(l1.val<=l2.val) {
head=l1;
l1=l1.next;
}
else {
head=l2;
l2=l2.next;
}
ListNode temp = head;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val) {
temp.next=l1;
l1=l1.next;
}
else {
temp.next=l2;
l2=l2.next;
};
temp=temp.next;
}
temp.next=l1==null?l2:l1;
return head;
}
}
归并K个排序链表
public class Solution {
public static ListNode mergeKLists(ListNode[] lists){
return partion(lists,0,lists.length-1);
}
public static ListNode partion(ListNode[] lists,int s,int e){
if(s==e) return lists[s];
if(s<e){
int q=(s+e)/2;
ListNode l1=partion(lists,s,q);
ListNode l2=partion(lists,q+1,e);
return merge(l1,l2);
}else
return null;
}
//This function is from Merge Two Sorted Lists.
public static ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge(l1.next,l2);
return l1;
}else{
l2.next=merge(l1,l2.next);
return l2;
}
}
}
寻找重复数字,O(N),O(1)
public class Solution {
public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
do{
slow = nums[slow];
fast = nums[nums[fast]];
}while(slow!=fast);
int find = 0;
do{
slow = nums[slow];
find = nums[find];
}while(slow!=find);
return find;
}
}
第一个不存在的正数
public class Solution {
public int firstMissingPositive(int[] nums) {
int n= nums.length;
for(int i=0;i<n;){
if(nums[i]>0 && nums[i]<=n && nums[i]!=nums[nums[i]-1]){
swap(nums,i,nums[i]-1);
}else{
i++;
}
}
int j=0;
for(j=0;j<n;j++){
if(j+1 != nums[j]){
return j+1;
}
}
return n+1;
}
public void swap(int[] nums,int i,int j){
int tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
}
}
KMP算法
class Solution {
public:
void getNext(vector<int> &next,string &needle){
int i=0,j=-1;
next[i] = j;
while(i<needle.size()-1){
while(j != -1 && needle[i]!=needle[j]){
j = next[j];
}
i++;
j++;
if(needle[i] == needle[j]) next[i]=next[j];
else next[i] = j;
}
}
int strStr(string haystack, string needle) {
if (haystack.empty()) return needle.empty() ? 0 : -1;
if (needle.empty()) return 0;
vector<int> next(needle.size()+1);
getNext(next,needle);
int i=0,j=0;
while(i != haystack.size()){
while(j!=-1 && haystack[i] != needle[j]){j=next[j];}
i++;
j++;
if(j == needle.size()) return i-j;
}
return -1;
}
};
寻找数列的峰点(比两边数字都大的点)
public class Solution {
public int findPeakElement(int[] num) {
return helper(num,0,num.length-1);
}
public int helper(int[] num,int start,int end){
if(start == end){
return start;
}else if(start+1 == end){
if(num[start] > num[end]) return start;
return end;
}else{
int m = (start+end)/2;
if(num[m] > num[m-1] && num[m] > num[m+1]){
return m;
}else if(num[m-1] > num[m] && num[m] > num[m+1]){
return helper(num,start,m-1);
}else{
return helper(num,m+1,end);
}
}
}
}
LeetCode(一)的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- WebService---Android中访问WebService接口的方法
最近公司有个项目需要从Android平台访问WebService接口,实现向发布的函数传递对象.在网上找了一些资料,发现使用ksoap2可以调用WebService传递对象. 需要引入ksoap ...
- metaspace之三--Metaspace解密
概述 metaspace,顾名思义,元数据空间,专门用来存元数据的,它是jdk8里特有的数据结构用来替代perm,这块空间很有自己的特点,前段时间公司这块的问题太多了,主要是因为升级了中间件所致,看到 ...
- Linux 下三种方式设置环境变量
1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错误. 2.那么什么是环境变 ...
- 一张图教你搞定Mac App Store 应用安装包存储路径
还在为找不到App Store 更新应用的安装文件发愁吗?是否有过多个人同时需要更新Xcode,都自己下载一次的痛苦经历? 大家都知道通过苹果服务器下载东西,确实难耐!AppStore 甚至都经常提示 ...
- Sonar升级遇到的那些事儿
目录 背景 如何升级 如何回滚 问题解决 参考 背景 目前我们用SonarQube版本是4.0,这次准备升级到最新版本5.1, 以便支持以后的JavaScript的项目. 如何升级 我们可以直接跨越版 ...
- 黑马程序员_JAVA之交通灯管理系统
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 1.一.需求:模拟实现十字路口的交通灯管理系统逻辑,具体需求如下: 1.异步随机生成按照各个路 ...
- Oracle 收缩表大小 Oracle Shrink Table --转载
从10g开始,oracle开始提供Shrink的命令,假如我们的表空间中支持自动段空间管理 (ASSM),就可以使用这个特性缩小段,即降低HWM.这里需要强调一点,10g的这个新特性,仅对ASSM表空 ...
- Django model中常见Field types , Field options
AutoField :自增,数据库 BooleanField:布尔型 CharField:字符型 DateField:日期,datetime.date类的实例.有两个常用的option,auto_no ...
- ASP.NET Page对象各事件执行顺序(转)
很久没写 asp.net 的东西了,search 了一下 page 的事件执行顺序,找到如下的东西,仅仅做记录用 Page.PreInit 在页初始化开始时发生 Page.Init 当服务器控件初始化 ...
- js实现缓冲运动,和匀速运动有点不相同
缓冲运动和匀速运动有点不同,看图可以知道缓冲运动速度是越来越慢的. <style> *{ padding:0; margin:10px 0; } #div1{ height:100px; ...