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. circus security 来自官方的安全建议

    转自:https://circus.readthedocs.io/en/latest/design/security/ Circus is built on the top of the ZeroMQ ...

  2. Bzoj 1927: [Sdoi2010]星际竞速(网络流)

    1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Description 10年一度的银河系赛车大赛又要开始了.作为全银河最盛大 ...

  3. P5589 【小猪佩奇玩游戏】

    这题还是比较妙妙套路的,复杂度为\(O(log^2N)\),可以卡掉\(\sqrt n\)的做法 首先我们可以把原数列分成很多个集合,集合之间肯定是两两独立的,考虑分别计算答案 我们定义\(f_i\) ...

  4. 一篇文章了解Github和Git教程

    有趣有内涵的文章第一时间送达! 喝酒I创作I分享 关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 生活中总有些东 ...

  5. 二分法python实现

    聚会游戏,一个人想一个数,其他人来猜,然后告诉你猜大了还是小了,直到猜到这个数. 二分法和猜数游戏类似,只不过猜的时候一定猜最中间的那个数,折半查找所需内容,就数组来说,数组越长,梯度下降越快,二分查 ...

  6. GitHub 远程仓库 de 第一次配置

    GitHub远程仓库, Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.首先找一台电脑充当服务器的角色, 每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上 ...

  7. HTML试题解析

    1.关于CSS为什么会出现Bug说法不正确的是(). (选择二项) A:编写CSS样式时需要考虑在不同浏览器中实现表现一致 B:各大主流浏览器由于不同厂家开发,浏览器使用的内核不同,支持CSS的程度不 ...

  8. mysql abs() 获取绝对值

    mysql> -); +----------+ | abs(-) | +----------+ | | +----------+ row in set (0.00 sec)

  9. kubernetes监控终极方案-kube-promethues

    kube-promethues简介 前面我们学习了Heapster+cAdvisor方式监控,这是Prometheus Operator出现之前的k8s监控方案.后来出现了Prometheus Ope ...

  10. 20189220 余超《Linux内核原理与分析》第七周作业

    分析Linux内核创建一个新进程的过程 基础知识概括 操作系统内核实现操作系统的三大管理功能,即进程管理功能,内存管理和文件系统.对应的三个抽象的概念是进程,虚拟内存和文件.其中,操作系统最核心的功能 ...