双指针操做数组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;
}
}
复制代码