第六天

30.(219) Contains Duplicate II

JAVA
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i =0;i<nums.length;i++){
if(map.containsKey(nums[i]))
if(i-map.get(nums[i])<=k)
return true;
else
map.put(nums[i],i);
else
map.put(nums[i],i);
}
return false;
}
}

31.(747) Largest Number At Least Twice of Others

JAVA
class Solution {
public int dominantIndex(int[] nums) {
int maxIndex = 0;
for(int i =0;i<nums.length;i++){
if(nums[i]>nums[maxIndex])
maxIndex = i;
} for(int i = 0 ;i<nums.length;i++){
if(i!=maxIndex&&nums[maxIndex]<nums[i]*2)
return -1;
}
return maxIndex;
}
}

32.(665) Non-decreasing Array

有点难度

JAVA
class Solution {
public boolean checkPossibility(int[] nums) {
boolean isFirst = true;
for(int i = 0 ;i < nums.length-1;i++){
if(nums[i+1] < nums[i]){
if(isFirst){
if(i == 0){//首位置为较小值
nums[i] = nums[i+1];
}else if(i == nums.length -2){//末尾置为大值
nums[i+1] = nums[i];
}else{//中间位置通过二者左右两边数的值,判断把哪位置为何值。
if(nums[i]<nums[i+2]){
nums[i+1] = nums[i];
}else if(nums[i+1]>nums[i-1]){
nums[i] = nums[i+1];
}else{
return false;
}
}
isFirst = false;
}else{
return false;
}
}
}
return true;
}
}

33.(532) K-diff Pairs in an Array

JAVA
//注意,这里的Map中存放的是 (num,num+k)or(num,null)
//之后计算Map中value有多少个非Null即可;
class Solution {
public int findPairs(int[] nums, int k) {
int pairs = 0;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
if(nums==null||nums.length == 0||k<0) return 0;
for (int i = 0 ;i<nums.length;i++){
if(map.containsKey(nums[i]-k))
map.put(nums[i]-k,nums[i]);
if(map.containsKey(nums[i]+k))
map.put(nums[i],nums[i]+k);
if(!map.containsKey(nums[i]))
map.put(nums[i],null);
} for(Integer key : map.keySet()) {
if(map.get(key)!=null)
pairs++;
}
return pairs;
}
}

34.(189) Rotate Array

JAVA
class Solution {
public void rotate(int[] nums, int k) {
k %=nums.length;
reverse(nums,0,nums.length-1);
reverse(nums,0,k-1);
reverse(nums,k,nums.length-1);
}
public void reverse(int[] nums,int start,int end){
while(start<end){
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}

35.(169) Majority Element

JAVA
class Solution {
public int majorityElement(int[] nums) {
int count = 0;
Integer candidate = null;
for(int num :nums){
if(count == 0)
candidate = num;
count += (num == candidate)?1:-1; }
return candidate;
}
}

36.(167) Two Sum II - Input array is sorted

JAVA
class Solution {
public int[] twoSum(int[] numbers, int target) {
int start = 0;
int end = numbers.length-1;
while(start<end && numbers[start]+numbers[end] != target){
if(numbers[start]+numbers[end]>target)
end--;
else
start++;
}
return new int[] {start+1,end+1};
}
}

37.(661) Image Smoother

JAVA
class Solution {
public int[][] imageSmoother(int[][] M) {
int R = M.length,C = M[0].length;
int[][] result = new int[R][C];
for(int r = 0 ;r<R;r++)
for(int c = 0;c<C;c++){
int count = 0;
result[r][c] = 0;
for(int nr=r-1;nr <= r+1;nr++)
for(int nc = c-1;nc <= c+1;nc++)
if(nr>=0&&nr<R&&nc>=0&&nc<C){
count++;
result[r][c] +=M[nr][nc];
}
result[r][c] /= count;
}
return result;
}
}

38.(53) Maximum Subarray

动态优化问题!重点,跟上楼梯那道题类似

解题思路

JAVA
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] DP = new int[n];
DP[0] = nums[0];
int maxSum = DP[0];
for(int i = 1;i<nums.length;i++){
DP[i] = nums[i]+(DP[i-1]>0?DP[i-1]:0);
maxSum = Math.max(maxSum,DP[i]);
}
return maxSum;
}
}

39.(697) Degree of an Array

JAVA
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer,Integer> left = new HashMap(),right = new HashMap(),count = new HashMap();
for(int i = 0;i<nums.length;i++){
if(left.get(nums[i]) == null) left.put(nums[i],i);
right.put(nums[i],i);
count.put(nums[i],count.getOrDefault(nums[i],0)+1);
}
int degree = Collections.max(count.values());//找出count中value的最大值
int minLength = nums.length;
for(int key : count.keySet()){
if(count.get(key)==degree){
minLength = Math.min(minLength,right.get(key)-left.get(key)+1);
}
}
return minLength;
}
}

LeetCode第六天的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. scss 初学笔记 一 变量声明 默认的样式 嵌套

    $width: 300px !default; $:          变量声明符号; width:    变量名称; 300px:   赋予变量的值; !default  代表默认样式 !defau ...

  2. Python---socketserver

    为方便以后学习和查询,特从socketserver架构.源码分析和案例三方面进行总结 1. Python之socketserver架构 2. Python之socketserver源码分析(一) 3. ...

  3. linkin大话面向对象--枚举

    枚举类(enum) 其实我们使用到枚举的地方还是很多的,其实我们可以完全人工的来实现枚举的功能.比如说我现在手里的项目我就是自己实现的枚举,说白了,枚举就是一个类的多例模式. 1,使用enum声明,默 ...

  4. word中正文分栏重新换页问题

    小论文常需要正文分栏,但是标题.摘要不分栏的编排格式. 1.在摘要后面加入分隔符来将内容分为摘要和正文两个部分.选择 插入→分隔符→分节符(连续). 2.然后进行分栏.选择 格式→分栏. 3.此时如果 ...

  5. MathUtils

    package com.yqw.java.util;/** * 数字转换工具 */public class MathUtils {    /**     * short转byte     */    ...

  6. JavaSE基础篇—MySQL基础知识点

    MySQL MySQL是一种关系数据库管理系统,是一种开源软件.可搭配PHP和Apache可以有更好的性能,也可以工作在众多的平台上.Orcale是一个数据库创建多个用户,MySQL是一个用户创建多个 ...

  7. CSS<img>与<a href>字体同行显示方法与对齐

    1.一开始使用php的volist标签conding了这样一段代码: <volist name="result['list']" id="temp"> ...

  8. auto和bool

    一.auto' 1.只要在函数内部定义变量,默认是auto int num 等价于  auto int num = 10; 2.C语言中的auto关键字就是自动分配自动释放 二.bool类型 1.头文 ...

  9. 不干胶打印机 www.bgjdyj.com

    不干胶打印机如何保养 不干胶打印机专卖网根据多年的维修经验总结了以下几种保养不干胶打印机的方法: 1.不干胶打印机打印机标签纸不能搁置太长时间,第一容易起静电.第二容易起灰尘2.不干胶打印机的打印头最 ...

  10. slick对超过22个属性的表进行映射的两种办法

    版权声明:本文为博主原创文章,未经博主允许不得转载 slick是scala的一个FRM(Functional Relational Mapper)框架,即函数式的关系数据库编程工具库.使用slick不 ...