首页 > 编程语言 >暑期算法打卡----第二天

暑期算法打卡----第二天

时间:2022-10-17 21:07:28浏览次数:70  
标签:target nums int 暑期 ---- nextInt new 打卡 scanner


目录

​1、二分查找​

​2.找出数组排序后的目标下标​

​3.寻找重复数​

​ 4.Binary Deque​

​说在最后✏️​


1、二分查找

题目:​​力扣​

给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。

示例 1:

输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4

示例 2:

输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1


提示:

    你可以假设 nums 中的所有元素是不重复的。
    n 将在 [1, 10000]之间。
    nums 的每个元素都将在 [-9999, 9999]之间。

题解:

Hash表法:

暑期算法打卡----第二天_数组

package 暑期特训__社区.day2;

import java.util.HashMap;
import java.util.Scanner;

/**
* @author yx
* @date 2022-07-03 19:30
*/
public class lc_704二分查找 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
int[] nums=new int[i];
HashMap<Integer,Integer>map=new HashMap<>();
for (int j = 0; j < i; j++) {
nums[j]=scanner.nextInt();
map.put(nums[j],j);
}
int target=scanner.nextInt();
System.out.println(map.get(target));
}
}

 二分法:

暑期算法打卡----第二天_算法_02

package 暑期特训__社区.day2;

import com.sun.org.apache.xml.internal.serializer.ElemDesc;

import java.util.HashMap;
import java.util.Scanner;

/**
* @author yx
* @date 2022-07-03 19:30
*/
public class lc_704二分查找 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
int[] nums=new int[i];
for (int j = 0; j <i ; j++) {
nums[j]=scanner.nextInt();
}
int target=scanner.nextInt();
// System.out.println(hashmap_coding(nums,target));
// System.out.println(二分_coding(nums,target));
}
//hashMap
static int hashmap_coding(int[] nums,int target){
HashMap<Integer,Integer>map=new HashMap<>();
for (int j = 0; j < nums.length; j++) {
map.put(nums[j],j);
}
if(map.get(target)!=null){
return map.get(target);
}else {
return -1;
}
}

/**
*二分板子 大于等于target的最小值
*/
static int 二分_coding(int[] nums,int target) {
int left = 0;//定义左指针
int rigth = nums.length - 1;//定义右指针
while (left < rigth) {
int mid=left+rigth>>1;//移动位置
if(nums[mid]>=target)rigth=mid;
else left=mid+1;
}
return nums[rigth]==target?rigth:-1;
}
}

2.找出数组排序后的目标下标

题目:​​力扣​

给你一个下标从 0 开始的整数数组 nums 以及一个目标元素 target 。

目标下标 是一个满足 nums[i] == target 的下标 i 。

将 nums 按 非递减 顺序排序后,返回由 nums 中目标下标组成的列表。如果不存在目标下标,返回一个 空 列表。返回的列表必须按 递增 顺序排列。

示例 1:

输入:nums = [1,2,5,2,3], target = 2
输出:[1,2]
解释:排序后,nums 变为 [1,2,2,3,5] 。
满足 nums[i] == 2 的下标是 1 和 2 。

示例 2:

输入:nums = [1,2,5,2,3], target = 3
输出:[3]
解释:排序后,nums 变为 [1,2,2,3,5] 。
满足 nums[i] == 3 的下标是 3 。

示例 3:

输入:nums = [1,2,5,2,3], target = 5
输出:[4]
解释:排序后,nums 变为 [1,2,2,3,5] 。
满足 nums[i] == 5 的下标是 4 。

示例 4:

输入:nums = [1,2,5,2,3], target = 4
输出:[]
解释:nums 中不含值为 4 的元素。

提示:

    1 <= nums.length <= 100
    1 <= nums[i], target <= 100

题解:

暴力:

暑期算法打卡----第二天_i++_03

package 暑期特训__社区.day2;

import java.util.*;

/**
* @author yx
* @date 2022-07-03 21:46
*/
public class lc_2089找出数组排序后的目标下标 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=scanner.nextInt();
int[] nums=new int[i];
for (int j = 0; j < i; j++) {
nums[j]=scanner.nextInt();
}
int target=scanner.nextInt();
List<Integer>T=targetIndices(nums,target);
for (int j = 0; j < T.size(); j++) {
System.out.print(T.get(j)+" ");
}
}
static List<Integer> targetIndices(int[] nums, int target) {
List<Integer> T=new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if(nums[i]==target){
T.add(i);
}
}
return T;
}
}

3.寻找重复数

题目:​​力扣​

给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。

假设 nums 只有 一个重复的整数 ,返回 这个重复的数 。

你设计的解决方案必须 不修改 数组 nums 且只用常量级 O(1) 的额外空间。


示例 1:

输入:nums = [1,3,4,2,2]
输出:2

示例 2:

输入:nums = [3,1,3,4,2]
输出:3


提示:

    1 <= n <= 105
    nums.length == n + 1
    1 <= nums[i] <= n
    nums 中 只有一个整数 出现 两次或多次 ,其余整数均只出现 一次


进阶:

    如何证明 nums 中至少存在一个重复的数字?
    你可以设计一个线性级时间复杂度 O(n) 的解决方案吗?

题解:

暑期算法打卡----第二天_算法_04

package 暑期特训__社区.day2;

import java.util.HashMap;
import java.util.Scanner;

/**
* @author yx
* @date 2022-07-03 22:05
*/
public class lc_287寻找重复数 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
int[] nums=new int[n+1];
for (int i = 0; i < n+1; i++) {
nums[i]=scanner.nextInt();
}
System.out.println(findDuplicate(nums));
}

static int findDuplicate(int[] nums) {
HashMap<Integer,Integer> map=new HashMap<>();
for (int i = 0; i < nums.length; i++) {
/*
这个地方不知道为啥用containsVlue放在leetcode上会超时
可能是时间复杂度有区别,到时候再看看
*/
if(map.containsKey(nums[i])){
return nums[i];
}else {
map.put(nums[i],0);
}
}
return -1;
}
}

 4.Binary Deque

题目:​Problem - 1692E - Codeforces (Unofficial mirror by Menci)​

E. Binary Deque
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Slavic has an array of length  

consisting only of zeroes and ones. In one operation, he removes either the first or the last element of the array.

What is the minimum number of operations Slavic has to perform such that the total sum of the array is equal to  
after performing all the operations? In case the sum  

can't be obtained after any amount of operations, you should output -1.
Input

The first line contains a single integer  
(   

) — the number of test cases.

The first line of each test case contains two integers  
and   (   

) — the length of the array and the needed sum of elements.

The second line of each test case contains  
integers     (   

) — the elements of the array.

It is guaranteed that the sum of  
over all test cases doesn't exceed    

.
Output

For each test case, output a single integer — the minimum amount of operations required to have the total sum of the array equal to  
, or -1 if obtaining an array with sum  

isn't possible.
Example
Input
Copy

7
3 1
1 0 0
3 1
1 1 0
9 3
0 1 0 1 1 1 0 0 1
6 4
1 1 1 1 1 1
5 1
0 0 1 1 0
16 2
1 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1
6 3
1 0 1 0 0 0

Output
Copy

0
1
3
2
2
7
-1

Note

In the first test case, the sum of the whole array is  

from the beginning, so we don't have to make any operations.

In the second test case, the sum of the array is  
and we want it to be equal to  , so we should remove the first element. The array turns into  , which has a sum equal to 

In the third test case, the sum of the array is  
and we need it to be  . We can obtain such a sum by removing the first two elements and the last element, doing a total of three operations. The array turns into  , which has a sum equal to 

题解:

暑期算法打卡----第二天_i++_05

package 暑期特训__社区.day2;

import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;

import java.util.Scanner;

/**
* @author yx
* @date 2022-07-03 23:13
*/
public class cf_E_BinaryDeque {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t=scanner.nextInt();
while (t-->0){
int n=scanner.nextInt();
int v=scanner.nextInt();
int [] s=new int[n+1];
for (int i = 1; i <=n ; i++) {
s[i]=scanner.nextInt();
s[i]+=s[i-1];
}
if(s[n]<v){
System.out.println(-1);
continue;
}
int res=0x3f3f3f3f;
for (int i = 1; i <=n ; i++) {
int l=i,r=n;
while (l<r){
int mid=l+r+1>>1;
if(s[mid]-s[i-1]<=v)l=mid;
else {
r=mid-1;
}
}
if(s[r]-s[i-1]==v){
res=Math.min(res,n-(r-i+1));
}
}
System.out.println(res);
}
}
}

说在最后✏️

算法暑期打卡快乐每一天,如果觉得我的题解对您有帮助的话,欢迎一键三联!

暑期算法打卡----第二天_算法_06

标签:target,nums,int,暑期,----,nextInt,new,打卡,scanner
From: https://blog.51cto.com/u_15754851/5764387

相关文章

  • 暑期算法打卡----第三天
    1、区域和检索-数组不可变题目:​​力扣​​题解:package暑期特训__社区.day3;importjava.util.Scanner;/***@authoryx*@date2022-07-0410:09*//*这个题目只要求......
  • 暑期算法打卡----第四天
    1、合并两个有序数组题目:​​力扣​​给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2......
  • 暑期算法打卡---第五天
    1、分发饼干 题目:​​力扣​​假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子i,都有一个胃口值g[i],这是能让孩子们满足......
  • 暑期算法打卡---第六天
    1、学生分数的最小差值题目:给你一个下标从0开始的整数数组nums,其中nums[i]表示第i名学生的分数。另给你一个整数k。从数组中选出任意k名学生的分数,使这k个......
  • 【数据库】期末必知必会-----第一章 数据库概述
    第一章数据库概述1、数据库相关的基本概念:?DB:数据库,相互关联的数据集合DBMS:数据库管理系统,管理数据库的软件,负责数据库的访问、管理和控制DBS:数据库系统,是指在计算机系统中......
  • 【数据库】期末必知必会-----第二章 关系数据模型
    第二章关系数据模型1、关系数据结构的相关概念?1)关系模型的数据结构就是二维表,把表称为关系2)关系数据库是表的集合,或者说是关系的集合3)表示一个实体集,每一行是一个实体,又因......
  • 【数据库】期末必知必会-----第六章 实验部分
    第六章实验部分(这一部分是考试重点)1、SQL语言的组成、特点?组成:1)DDL(数据库定义语言:CREAT、DROP、ALTER)2)DML(数据库操纵语言:INSERT、UPDATE、DELETE、SELECT)3)DCL(数据库控制语......
  • 【数据库】期末必知必会-----第八章 数据库安全
    第八章数据库安全1、安全性和完整性的区别完整性:1)防止数据库中存在不符合语义的数据2)防范对象:不合语义、不正确的数据安全性:1)保护数据库,防止恶意破坏和非法存取2)防范对象:非......
  • Python学习:标准库之数据持久存储与交换
    持久存储数据以便长期使用包括两个方面:在对象的内存中表示和存储格式之间来回转换数据,以及处理转换后数据的存储区。标准库包含很多模块可以处理不同情况下的这两个方面......
  • 报告分享|2022年中国灵活用工市场研究报告
    报告链接:http://tecdat.cn/?p=29474报告回顾了2021年中国网络招聘行业市场发展变化,从多个角度解读市场以及行业的变化。通过分析以上变化,总结如今网络招聘行业存在的问题......