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. 使用terraform 生成自签名证书

    terraform 是一个很不错的基础设施工具,我们可以用来做关于基础设施部署的事情,可以实现基础设施即代码 以下演示一个简单的自签名证书的生成(使用tls provider) main.tf 文件 ...

  2. PowerDesigner 创建表的时候 没有自增长Id的设置项

    今天早上同事创建表的时候,在那个界面没有自增长Id的选项,当时我也纳闷,软件肯定都是一样的,设置的步骤都一样(有些配置好的 我就没改过 然后就忘了还改过些什么步骤了),结果还是没有那个选项 百度了一下 ...

  3. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  4. 53、Spark Streaming:输入DStream之Kafka数据源实战

    一.基于Receiver的方式 1.概述 基于Receiver的方式: Receiver是使用Kafka的高层次Consumer API来实现的.receiver从Kafka中获取的数据都是存储在Sp ...

  5. sublime text 3 安装、添加命令行启动、汉化、注册码

    1. 安装sublime: 下载:http://www.sublimetext.com/3 添加命令行启动:设置环境变量->计算机->右键属性->高级系统设置->环境变量-&g ...

  6. D3.js的v5版本入门教程(第四章)—— 理解Update、Enter、Exit

    D3.js的v5版本入门教程(第四章) Update.Enter.Exit是D3.js中很重要的概念,下面来讲一下它们到底是什么?(当你看完后.你就会知道如果数据集个数和选择集个数不匹配的情况下使用d ...

  7. linux桌面系统开启windows远程访问

    本文基于centos系统,且默认你的桌面系统已经安装完成 1.在linux中安装如下软件 sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epe ...

  8. SpringBoot定时任务@Scheduled

    SpringBoot定时任务主要由两个注解完成. @Scheduled加在方法上面. @EnableScheduling加在类上面.可以是Application类,也可以是@Component类,还可 ...

  9. MySQL各种类型实验

    实验一:整数 -- 测试一 create database test;-- 新建数据库,如果已经有了就不需要再创建了 USE test;-- 打开数据库 drop table if exists te ...

  10. Mysql问题1862

    使用Navicat连接MySQL数据库出现1862错误(1862:Your password has expired.To log in you must change it change it us ...