package y2019.Algorithm.array;

import java.util.Arrays;
import java.util.Stack; /**
* @ClassName Rotate
* @Description TODO 189. Rotate Array
*
* Given an array, rotate the array to the right by k steps, where k is non-negative.
*
* Input: [1,2,3,4,5,6,7] and k = 3
* Output: [5,6,7,1,2,3,4]
* Explanation:
* rotate 1 steps to the right: [7,1,2,3,4,5,6]
* rotate 2 steps to the right: [6,7,1,2,3,4,5]
* rotate 3 steps to the right: [5,6,7,1,2,3,4]
*
* @Author xiaof
* @Date 2019/7/6 18:44
* @Version 1.0
**/
public class Rotate { public void solution(int[] nums, int k) { //这里移动的意思就是把末尾循环遍历到前面,那么只要倒着遍历,然后跳转到开始就可以了
int index = nums.length - k % nums.length; //开始读取的位置
int[] nums2 = new int[nums.length];
int i = 0;
while(i < nums.length) {
if(index >= nums.length) {
index = 0;
}
nums2[i++] = nums[index++];
}
//复制到nums中
System.arraycopy(nums2, 0, nums, 0, nums.length); } public static void main(String args[]) {
int A1[] = {-1};
int k = 2;
Rotate fuc = new Rotate();
fuc.solution(A1, k);
System.out.println();
} }
package y2019.Algorithm.array;

import java.util.HashSet;
import java.util.Set; /**
* @ClassName ContainsDuplicate
* @Description TODO 217. Contains Duplicate
*
* Given an array of integers, find if the array contains any duplicates.
* Your function should return true if any value appears at least twice in the array,
* and it should return false if every element is distinct.
*
* Input: [1,2,3,1]
* Output: true
* Example 2:
*
* @Author xiaof
* @Date 2019/7/6 20:31
* @Version 1.0
**/
public class ContainsDuplicate { public boolean solution(int[] nums) { //判断是否有重复,很简单用set
Set set = new HashSet();
for(int a : nums) {
if(set.contains(a)) {
return true;
} else {
set.add(a);
}
} return false;
} }
package y2019.Algorithm.array;

import java.util.HashSet;
import java.util.Set; /**
* @ClassName ContainsNearbyDuplicate
* @Description TODO 219. Contains Duplicate II
* Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such
* that nums[i] = nums[j] and the absolute difference between i and j is at most k.
*
* Input: nums = [1,2,3,1], k = 3
* Output: true
*
* Input: nums = [1,0,1,1], k = 1
* Output: true
*
* Input: nums = [1,2,3,1,2,3], k = 2
* Output: false
*
* @Author xiaof
* @Date 2019/7/6 20:36
* @Version 1.0
**/
public class ContainsNearbyDuplicate { public boolean solution(int[] nums, int k) {
//1.因为范围内是k
//那么可以设置一个set,每次存放一个k范围内的值,超出部分丢掉即可
Set set = new HashSet();
for(int i = 0; i < nums.length; ++i) {
if(i > k) {
//如果超出范围,那么把范围外去掉
set.remove(nums[i - k - 1]);
}
//判断是否有重复
if(set.contains(nums[i])) {
return true;
} else {
set.add(nums[i]);
}
} return false;
} public static void main(String args[]) {
int A1[] = {1,2,3,1,2,3};
int k = 2;
ContainsNearbyDuplicate fuc = new ContainsNearbyDuplicate();
fuc.solution(A1, k);
System.out.println();
} }
package y2019.Algorithm.array;

/**
* @ClassName MissingNumber
* @Description TODO 268. Missing Number
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
* Input: [3,0,1]
* Output: 2
*
* Input: [9,6,4,2,3,5,7,0,1]
* Output: 8
* @Author xiaof
* @Date 2019/7/6 21:09
* @Version 1.0
**/
public class MissingNumber { //仔细看题,都是从0开始的,那么要计算从0开始到nums.length之间少的那个,我们只要算出总值减去所有哦值即可
public int solution(int[] nums) {
int sum = (nums.length + 1) * nums.length / 2;
for(int i = 0; i < nums.length; ++i) {
sum -= nums[i];
}
return sum;
} public static void main(String args[]) {
int A1[] = {3,0,1};
int k = 2;
MissingNumber fuc = new MissingNumber();
fuc.solution(A1);
System.out.println();
} }
package y2019.Algorithm.array;

/**
* @ClassName MoveZeroes
* @Description TODO 283. Move Zeroes283. Move Zeroes
* Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
* Input: [0,1,0,3,12]
* Output: [1,3,12,0,0]
* @Author xiaof
* @Date 2019/7/6 21:42
* @Version 1.0
**/
public class MoveZeroes { public void solution(int[] nums) {
//哎,有的时候不能想太多,这个时候就得用2个数组
int[] newNums = new int[nums.length];
int indexNewNums = 0;
for(int n : nums) {
if(n != 0) {
newNums[indexNewNums++] = n;
}
}
//最后补上0
for(; indexNewNums < nums.length; ++indexNewNums) {
newNums[indexNewNums] = 0;
} //拷贝回去
System.arraycopy(newNums, 0, nums, 0, nums.length); } public static void main(String args[]) {
int A1[] = {1,0};
int k = 2;
MoveZeroes fuc = new MoveZeroes();
fuc.solution(A1);
System.out.println();
} }
package y2019.Algorithm.array;

/**
* @ClassName ThirdMax
* @Description TODO 414. Third Maximum Number
* Given a non-empty array of integers, return the third maximum number in this array.
* If it does not exist, return the maximum number. The time complexity must be in O(n).
*
* Input: [3, 2, 1]
* Output: 1
* Explanation: The third maximum is 1.
*
* @Author xiaof
* @Date 2019/7/6 22:40
* @Version 1.0
**/
public class ThirdMax { //寻找第三大的数据
public int solution(int[] nums) {
//我们定义长度为3的数组,用来存放前三大的数据,最后0位就是第三大的数据
Integer[] maxNum = new Integer[3];
//初始化数据
for(int i = 0; i < maxNum.length; ++i) {
maxNum[i] = null;
} for(int i = 0; i < nums.length; ++i) {
//依次和前三比较
int index = 0;
for(int j = 0; j < maxNum.length; ++j) {
if(maxNum[j] == null || maxNum[j] < nums[i]) {
++index;
} else if (nums[i] == maxNum[j]) {
//去除重复数据入列
index = -1;
break;
} else {
break;//如果比max里面的位置小那么直接跳出
}
} if(index > 0) {
//修改位置
for(int k = 0; k < index - 1; ++k) {
//前面几位从新排序
maxNum[k] = maxNum[k+1];
}
maxNum[index - 1] = nums[i];
}
} //如果不存在第三大的,那么就获取最大的
int max = maxNum[0] == null ? maxNum[2] : maxNum[0];
return max; } public static void main(String args[]) {
int A1[] = {1,2,-2147483648};
int k = 2;
ThirdMax fuc = new ThirdMax();
fuc.solution(A1);
System.out.println();
} }

【LEETCODE】48、数组分类,简单级别,题目:189,217,219,268,283,414的更多相关文章

  1. 【LEETCODE】54、数组分类,简单级别,题目:605、532

    数组类,简单级别完结.... 不容易啊,基本都是靠百度答案.... 希望做过之后后面可以自己复习,自己学会这个解法 package y2019.Algorithm.array; /** * @Proj ...

  2. 【LEETCODE】53、数组分类,简单级别,题目:989、674、1018、724、840、747

    真的感觉有点难... 这还是简单级别... 我也是醉了 package y2019.Algorithm.array; import java.math.BigDecimal; import java. ...

  3. 前端与算法 leetcode 48. 旋转图像

    目录 # 前端与算法 leetcode 48. 旋转图像 题目描述 概要 提示 解析 解法一:转置加翻转 解法二:在单次循环中旋转 4 个矩形 算法 传入测试用例的运行结果 执行结果 GitHub仓库 ...

  4. 【LeetCode】数组-1(643)-返回规定长度k的最大子数组的平均数

    好久没有刷LeetCode了,准备重拾并坚持下去,每天刷个两小时.今天算是开始的第一天,不过出师不利,在一道很简单的题目上墨迹半天.不过还好,现在踩过的坑,应该都不会白踩,这些可能都是以后程序员路上稳 ...

  5. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  6. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  7. LeetCode~移除元素(简单)

    移除元素(简单) 1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使 ...

  8. 面阿里P7,竟问这么简单的题目?

    关于作者:程序猿石头(ID: tangleithu),来自十八县贫困农村(查看我的逆袭之路),BAT某厂P7,是前大疆(无人机)技术主管,曾经也在创业公司待过,有着丰富的经验. 本文首发于微信公众号, ...

  9. LeetCode:数组中的第K个最大元素【215】

    LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: ...

  10. Java数据结构和算法之数组与简单排序

    一.数组于简单排序 数组 数组(array)是相同类型变量的集合,可以使用共同的名字引用它.数组可被定义为任何类型,可以是一维或多维.数组中的一个特别要素是通过下标来访问它.数组提供了一种将有联系的信 ...

随机推荐

  1. Cogs 732. [网络流24题] 试题库(二分图)

    [网络流24题] 试题库 ★★ 输入文件:testlib.in 输出文件:testlib.out 评测插件 时间限制:1 s 内存限制:128 MB «问题描述: 假设一个试题库中有n道试题.每道试题 ...

  2. C博客作业--我的第一篇博客作业

    1你对网络专业或计算机专业了解是怎样的 由于从小就与电脑打交道,对于各类软件的生产非常感兴趣,所以在高三开学查询有什么专业的时候,就打算报与计算机有关的专业.我对计算机专业感到非常神奇,毕竟只是看似简 ...

  3. Intellij IDEA 自动清除无效 import 和 清除无效 import 的快捷键

    可以settings-general-auto import-java项,勾选optimize imports on the fly,在当前项目下会自动清除无效的import,而且这个是随时自动清除的 ...

  4. 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数

    今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...

  5. 刷题记录:[LCTF]bestphp's revenge

    目录 刷题记录:[LCTF]bestphp's revenge 一.知识点 1.SoapClient触发反序列化导致ssrf 2.serialize_hander处理session方式不同导致sess ...

  6. python 播放MP3和MP4

    import pygame import time def play_music(): filepath = r"900A.mp3"; pygame.mixer.init() # ...

  7. bash 重启后台程序脚本

    kill -9 `cat pid` nohup python3 -u webserver.py & echo $! > pid

  8. POST请求BODY格式区别

    在PostMan中用Post方式,Body有form-data,x-www-form-urlencoded,raw,binary四种. Request Header示例:POST /upload.do ...

  9. 用于KV集群的一致性哈希Consistent Hashing机制

    KV集群的请求分发 假定N为后台服务节点数,当前台携带关键字key发起请求时,我们通常将key进行hash后采用模运算 hash(key)%N 来将请求分发到不同的节点上, 后台节点的增删会引起几乎所 ...

  10. Nginx location wildcard

    Module ngx_http_core_modulehttps://nginx.org/en/docs/http/ngx_http_core_module.html#location locatio ...