LeetCode 2441. Largest Positive Integer That Exists With Its Negative【哈希集合】简单

这篇具有很好参考价值的文章主要介绍了LeetCode 2441. Largest Positive Integer That Exists With Its Negative【哈希集合】简单。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

给你一个 不包含 任何零的整数数组 nums ,找出自身与对应的负数都在数组中存在的最大正整数 k 。

返回正整数 k ,如果不存在这样的整数,返回 -1 。

示例 1:

输入:nums = [-1,2,-3,3]
输出:3
解释:3 是数组中唯一一个满足题目要求的 k 。

示例 2:

输入:nums = [-1,10,6,7,-7,1]
输出:7
解释:数组中存在 17 对应的负数,7 的值更大。

示例 3:

输入:nums = [-10,8,6,7,-2,-3]
输出:-1
解释:不存在满足题目要求的 k ,返回 -1

提示:

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0

解法 哈希集合+一次遍历

用一个哈希表记录出现过的数字。一边遍历,一边看 − nums [ i ] -\textit{nums}[i] nums[i] 是否在哈希表中,如果在,就更新答案的最大值 。

class Solution {
    public int findMaxK(int[] nums) {
        var rec = new HashSet<Integer>();
        int ans = -1;
        for (int i : nums) {
            if (rec.contains(-i)) ans = Math.max(ans, Math.abs(i));
            rec.add(i);
        }
        return ans;
    }
}

复杂度分析:文章来源地址https://www.toymoban.com/news/detail-448509.html

  • 时间复杂度: O ( n ) O(n) O(n)
  • 空间复杂度: O ( n ) O(n) O(n)

到了这里,关于LeetCode 2441. Largest Positive Integer That Exists With Its Negative【哈希集合】简单的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Leetcode 13. Roman to Integer

    Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usu

    2024年02月08日
    浏览(27)
  • LeetCode //13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol       Value     I               1     V              5     X              10     L               50     C               100     D               500     M               1000 For example, 2 is written as I

    2024年02月16日
    浏览(35)
  • HuggingGPT Solving AI Tasks with ChatGPT and its Friends in Hugging Face

    HuggingGPT 让LLM发挥向路由器一样的作用,让LLM来选择调用那个专业的模型来执行任务。 HuggingGPT搭建LLM和专业AI模型的桥梁。 Language is a generic interface for LLMs to connect AI models Task Planning: 将复杂的任务分解。但是这里是将任务分解为一系列的structured tasks。还可以通过之前的 ch

    2024年02月15日
    浏览(32)
  • 《2023 HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in Hugging Face》阅读笔记

    借助大语言模型(LLMS)在语言理解生成推理等方面表现出的出色能力,考虑将其作为控制器来管理现有的各种AI模型, 把语言作为通用接口 。基于这一理念,提出了HuggingGPT框架,利用LLMS(ChatGPT)来连接机器学习社区(Hug face)中的各种AI模型,具体来说就是在接收用户请求

    2024年02月02日
    浏览(52)
  • LeetCode每日一题(2457. Minimum Addition to Make Integer Beautiful)

    You are given two positive integers n and target. An integer is considered beautiful if the sum of its digits is less than or equal to target. Return the minimum non-negative integer x such that n + x is beautiful. The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Init

    2023年04月16日
    浏览(84)
  • LeetCode --- 1971. Find if Path Exists in Graph 解题报告

    There is a  bi-directional  graph with  n  vertices, where each vertex is labeled from  0  to  n - 1  ( inclusive ). The edges in the graph are represented as a 2D integer array  edges , where each  edges[i] = [ui, vi]  denotes a bi-directional edge between vertex  ui  and vertex  vi . Every vertex pair is connected by  at most one  edge, and

    2024年02月07日
    浏览(33)
  • docker 启动容器异常Error response from daemon: OCI runtime create failed: container with id exists

    问题描述 docker服务异常停止,重启docker后,容器启动失败 错误信息 Error response from daemon: OCI runtime create failed: container with id exists: xxx unknown 错误原因 docker启动的时候,会在运行目录(/var/run/docker/runtime-runc/moby)(不同环境,可能目录不一样,可以通过 find / -name \\\'容器ID\\\' 查找

    2024年02月16日
    浏览(27)
  • 【已解决】:pip is configured with locations that require TLS/SSL

    在使用pip进行软件包安装的时候出现问题: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 解决: 然后输入内容: 再次使用pip安装即可。 参考:https://blog.csdn.net/witton/article/details/109352577?spm=1001.2101.3001.6650.15utm_medium=distribute.pc_relevant.

    2024年04月15日
    浏览(49)
  • Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

    报错:         Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.1.16. 解决方案: 非常简单:Build---Rebuild project,再运行就没问题了。 如果不行可以尝试:         在项目的构建文件(如 pom.xm

    2024年02月07日
    浏览(41)
  • Module was compiled with an incompatible version of Kotlin.The binary version of its metadata is....

    解决Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1.的问题。 出现此问题是因为kotlin的版本不一致,下载的版本可以从提示的错误信息中定位到下载kotlin的位置,打开后可以找到很多版本,结合错误信息说:元数据是1.7.1,

    2024年02月12日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包