首页 > 其他分享 >leetcode-1346-easy

leetcode-1346-easy

时间:2023-02-15 21:34:20浏览次数:41  
标签:arr set int contains 1346 easy leetcode

Check if N and Its Double Exist

Given an array arr of integers, check if there exist two indices i and j such that :

i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1:

Input: arr = [10,2,5,3]
Output: true
Explanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]
Example 2:

Input: arr = [3,1,7,11]
Output: false
Explanation: There is no i and j that satisfy the conditions.

Constraints:

2 <= arr.length <= 500
-103 <= arr[i] <= 103

思路一:用 set 判断,注意除法整除问题

    public static boolean checkIfExist(int[] arr) {
        Set<Integer> set = new HashSet<>();

        for (int i : arr) {
            if (set.contains(i * 2) || ((i & 1) == 0) && set.contains(i / 2)) {
                return true;
            }

            set.add(i);
        }

        return false;
    }

标签:arr,set,int,contains,1346,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/17124766.html

相关文章

  • leetcode-908-easy
    SmallestRangeIYouaregivenanintegerarraynumsandanintegerk.Inoneoperation,youcanchooseanyindexiwhere0<=i<nums.lengthandchangenums......
  • leetcode-821-easy
    ShortestDistancetoaCharacterGivenastringsandacharactercthatoccursins,returnanarrayofintegersanswerwhereanswer.length==s.lengthand......
  • leetcode-10460-easy
    LastStoneWeightYouaregivenanarrayofintegersstoneswherestones[i]istheweightoftheithstone.Weareplayingagamewiththestones.Oneachtur......
  • leetcode-783-easy
    MinimumDistancdBetweenNodesGiventherootofaBinarySearchTree(BST),returntheminimumdifferencebetweenthevaluesofanytwodifferentnodesinthe......
  • leetcode-824-easy
    GoatLatinYouaregivenastringsentencethatconsistofwordsseparatedbyspaces.Eachwordconsistsoflowercaseanduppercaselettersonly.Wewouldlik......
  • 【LeetCode栈与队列#05】滑动窗口最大值
    滑动窗口最大值力扣题目链接(opensnewwindow)给定一个数组nums,有一个大小为k的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的k个数字。......
  • [Leetcode]435. 无重叠区间
    435.无重叠区间给定一个区间的集合 intervals ,其中intervals[i]=[starti,endi] 。返回需要移除区间的最小数量,使剩余区间互不重叠 。示例1:输入:intervals......
  • 【算法训练营day44】完全背包基础 LeetCode518. 零钱兑换II LeetCode377. 组合总和IV
    LeetCode518.零钱兑换II题目链接:518.零钱兑换II独上高楼,望尽天涯路组合问题和完全背包的混合应用,感觉脑中模拟的不是很清晰,但是靠着背包问题的代码技巧和模板就能比较......
  • 【算法训练营day43】LeetCode1049. 最后一块石头的重量II LeetCode494. 目标和 LeetCo
    LeetCode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II独上高楼,望尽天涯路一开始还是没有想到怎么转化成01背包问题,所以直接看题解找思路慕然回首,灯......
  • vue3之异步组件defineAsyncComponent 使用无效?
    原文地址:我的稀土掘金介绍:defineAsyncComponent用于拆分应用为更小的块,并仅在需要时再从服务器加载相关组件官网案例<scriptsetup>import{defineAsyncComponent......