雙指針操做數組java
桶排序,把每一個數字放在對應的位置上算法
遍歷數組,找到在正確位置的,把其餘的存在新建的數組裏,再次遍歷數組數組
若是remain不爲負,則必定有一個位置出發能走完整圈,若是i處remain爲負數則從i+1處開始bash
先sort,當citations[i]>=length-i的時候return len-i。其餘狀況return 0由於沒有such indexspa
HashSet的元素不能重複,若是hashset add失敗則出現重複元素指針
貪心算法,一直記錄最遠位置看是否涵蓋當前位置code
隨時記錄和替換min和profitcdn
貪心算法,找localmin和localMaxblog
只容許買賣兩次,動態規劃 每個位置有四種狀況,能夠是排序
class Solution {
public int maxProfit(int[] prices) {
int buy1 = Integer.MIN_VALUE;
int buy2 = Integer.MIN_VALUE;
int sell1 = 0;
int sell2 = 0;
for(int price: prices){
buy1 = Math.max(buy1, -price);
sell1 = Math.max(sell1, buy1+price);
buy2 = Math.max(buy2, sell1-price);
sell2 = Math.max(sell2, buy2+price);
}
return sell2;
}
}
複製代碼
雙指針,一塊兒向中間靠近,取代短的一邊
雙指針,每個元素若是大於peak則取代peak,若是小於peak則水量增長
public int trap(int[] height) {
if(height==null || height.length<3) return 0;
int left = 0;
int right = height.length-1;
int res = 0;
int peakLeft = height[0];
int peakRight = height[height.length - 1];
while(left<=right){
if(peakLeft<=peakRight){
if(height[left] > peakLeft){
peakLeft = height[left];
left++;
}else{
res+= peakLeft - height[left];
left++;
}
}else{
if(height[right] > peakRight){
peakRight = height[right];
right--;
}else{
res+= peakRight - height[right];
right--;
}
}
}
return res;
}
複製代碼
動態規劃,記錄下min1和min2,不停替換,若是大於min2則返回true
利用HashSet的contains和add方法爲O(1)的特性
public int longestConsecutive(int[] nums) {
if(nums.length<2 || nums==null) return nums.length;
int res = 0;
int cur = 0;
HashSet<Integer> set = new HashSet<>();
for(int num: nums){
set.add(num);
}
for(int i=0; i<nums.length; i++){
int low = nums[i] - 1;
if(!set.contains(low)){
int temp = nums[i];
while(set.contains(temp)){
cur++;
temp++;
}
res = Math.max(res,cur);
cur = 0;
}
}
return res;
}
複製代碼
兩個指針分別指向兩個array的開頭,每次較小的前進一位
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int len = nums1.length+nums2.length;
int count = 1;
int odd = 0;
int index1 = 0;
int index2 = 0;
if(len%2!=0){
odd = 1;
}
if(odd==1){//when length is odd
int res = 0;
while(count<=(len/2+1)){
if(index1>=nums1.length){
res = nums2[index2];
index2++;
count++;
}else if(index2>=nums2.length){
res = nums1[index1];
index1++;
count++;
}else{
if(nums1[index1]>=nums2[index2]){
res = nums2[index2];
index2++;
count++;
}else{
res = nums1[index1];
index1++;
count++;
}
}
}
return res;
}else{
int res1 = 0;
int res2 = 0;
while(count<=(len/2+1)){
if(index1>=nums1.length){
res2 = res1;
res1 = nums2[index2];
index2++;
count++;
}else if(index2>=nums2.length){
res2 = res1;
res1 = nums1[index1];
index1++;
count++;
}else{
if(nums1[index1]>=nums2[index2]){
res2 = res1;
res1 = nums2[index2];
index2++;
count++;
}else{
res2 = res1;
res1 = nums1[index1];
index1++;
count++;
}
}
}
double res = (double)(res1+res2)/2;
return res;
}
}
複製代碼
Math.max(beforeSum+currentElement,currentElement);
複製代碼
滑窗算法,雙指針一個控制窗子左邊一個控制窗子右邊
用兩個array儲存從前日後的到i的乘積和從i開始的乘積 i的值等於到i-1的乘積乘上從i+1開始的乘積
和最大子序和的原理類似,可是要考慮到乘積有正負,因此要有beforeMax和beforeMin
雙指針,遇到不連續的就add進res而後重置指針
相似桶排序思想,遇到非零的數就與current ptr換位置
left和right指針表明窗戶兩端,currMax表明當前窗內最大值,若是滑動後移除的是最大值則從新搜索新窗,若是不是最大值,則currMax = Math.max(nums[left-1],nums[right])
若是當前值大於前一項的值且前一項爲valley則當前項爲新peak,反之亦然
class Solution {
public int wiggleMaxLength(int[] nums) {
if(nums.length<2) return nums.length;
int count = 1;
Boolean peak = null;
for(int i=1; i<nums.length; i++){
if(nums[i]>nums[i-1] && (peak==null || peak==false)){
peak = true;
count++;
}else if(nums[i]<nums[i-1] && (peak==null || peak==true)){
peak = false;
count++;
}
}
return count;
}
}
複製代碼
給定一個非負整數 numRows,生成楊輝三角的前 numRows 行。
輸入: 5
輸出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
複製代碼
重點:經過儲存前一列來省去兩個get() function提高效率
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(numRows == 0) return list;
List<Integer> pre = new ArrayList<>();
pre.add(1);
list.add(pre);
for(int i=1; i<numRows; i++){
List<Integer> line = new ArrayList<>();
line.add(1);
for(int j=1;j<i;j++){
line.add(pre.get(j-1) + pre.get(j));
}
line.add(1);
pre = line;
list.add(line);
}
return list;
}
}
複製代碼