当前位置:网站首页>HashTable哈希表与索引1、599、219
HashTable哈希表与索引1、599、219
2022-04-22 14:12:00 【借点头发吧】
219. 存在重复元素 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值至多为 k
示例 1:
输入: nums = [1,2,3,1], k = 3
输出: true
示例 2:
输入: nums = [1,0,1,1], k = 1
输出: true
示例 3:
输入: nums = [1,2,3,1,2,3], k = 2
输出: false
题解:
1)hash遍历一次,出现重复数字时判断是否满足条件并更新map
sloution:
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer,Integer> map=new HashMap<>();
int n=0;
for(int j=0;j<nums.length;j++){
if(map.containsKey(nums[j]) && Math.abs(j-map.get(nums[j]))<=k) return true;
map.put(nums[j],j);
}
return false;
}
}
599. 两个列表的最小索引总和
假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。
示例 1:
输入:
[“Shogun”, “Tapioca Express”, “Burger King”, “KFC”]
[“Piatti”, “The Grill at Torrey Pines”, “Hungry Hunter Steakhouse”, “Shogun”]
输出: [“Shogun”]
解释: 他们唯一共同喜爱的餐厅是“Shogun”。
示例 2:
输入:
[“Shogun”, “Tapioca Express”, “Burger King”, “KFC”]
[“KFC”, “Shogun”, “Burger King”]
输出: [“Shogun”]
解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。
提示:
两个列表的长度范围都在 [1, 1000]内。
两个列表中的字符串的长度将在[1,30]的范围内。
下标从0开始,到列表的长度减1。
两个列表都没有重复的元素。
题解:
1)用map保存list1中的餐厅名称和序号,用list2中的餐厅名称去匹配list1中的hashmap,如果匹配成功则计算序列和; 1.若序号之和小于当前的最小值sum,清空结果数组,存入新的餐厅名称; 2.若序号之和等于当前最小值,就将当前餐厅名称存入结果数组; 最后返回结果数组
sloution:
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
Map<String,Integer> map=new HashMap<>();
for(int i=0;i<list1.length;i++){
map.put(list1[i],i);
}
List<String> list=new ArrayList();
int sum=Integer.MAX_VALUE;
for(int j=0;j<list2.length;j++){
int n=Integer.MAX_VALUE;
if(map.containsKey(list2[j])) n=j+map.get(list2[j]);
else continue;
if(n>sum) continue;
else if(n==sum) list.add(list2[j]);
else{
sum=n;
list.clear();
list.add(list2[j]);
}
}
return list.toArray(new String[list.size()]);
}
}
1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
题解:
1)暴力 双重循环
2)hash
sloution:
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i< nums.length; i++) {
if(map.containsKey(target - nums[i])) {
return new int[] {
map.get(target-nums[i]),i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
版权声明
本文为[借点头发吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47866171/article/details/108110063
边栏推荐
猜你喜欢

Timer--

idea中pom.xml文件里mysql5.7坐标报红出错

二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer

银行为什么要上堡垒机?选择哪家好?有案例吗?

【Zeekr_Tech】ROS/ROS 2介绍

Why should the bank go to the fortress machine? Which one to choose? Are there any cases?

日志脱敏是什么意思?为什么要做日志脱敏?

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
![Error reported by uniapp to wechat developer tool - [wrong content of app.json file] JSON: the sitemap.json file corresponding to [](/img/3b/2f371eab7d2f9e976dcd4612a19518.png)
Error reported by uniapp to wechat developer tool - [wrong content of app.json file] JSON: the sitemap.json file corresponding to ["sitemaplocation"] was not found

定时器--
随机推荐
How to use openfeign to call the third-party interface
Brief description of three elements of LAN characteristics
[PHP] MVCs concept (easy to understand)
QT create window flash back problem
LeetCode-3 无重复字符的最长子串
Live classroom system platform software source code available for personal testing
Fastdfs installation and configuration
ipv6相关
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Analyze the seven reasons for the slow use of computers
Compared with redis, memcached
Blocking mode and non blocking mode of socket
In the source code of Vue cache compilation results, the template is used as the cache key, and the template written by the user is so long. Is this appropriate
2D conversion (move: translate, rotate: rotate, scale: scale, 2D conversion synthesis)
Mysql database transferring SQL file
BCC-funccount
Eight strange facts about semiconductors
Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
2022茶艺师(中级)考试模拟100题及模拟考试
CPT 104_ Lab 09