LeetCode - 1346. Check If N and Its Double Exist(Array, Hash Table, Two Pointers, Binary Search, Sorting)
質問する
https://leetcode.com/problems/check-if-n-and-its-double-exist/
Given an array of integers, check if there exists two integers and such that is the double of ( i.e. ).
arr N M N M N = 2 * M
More formally check if there exists two indices and such that :ij i != j 0 <= i, j < arr.length arr[i] == 2 * arr[j] 翻訳
整数の配列を考慮して、2つの整数(すなわち、2つの整数)に確実に保存してください.arrNMNMN = 2 * M
2つのより正式な指数に保存してください.ij
Example 1:
Input: arr = [10,2,5,3]
Output: true
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
Example 2:
Input: arr = [7,1,14,11]
Output: true
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
Example 3:
Input: arr = [3,1,7,11]
Output: false
Explanation: In this case does not exist N and M, such that N = 2 * M.
に答える
これはJavaです.配列の値が2の倍数であることを確認します. i,jが異なる場合、2倍であることを確認し、trueを返します.
https://leetcode.com/problems/check-if-n-and-its-double-exist/
Given an array of integers, check if there exists two integers and such that is the double of ( i.e. ).
arr N M N M N = 2 * M
More formally check if there exists two indices and such that :ij
整数の配列を考慮して、2つの整数(すなわち、2つの整数)に確実に保存してください.arrNMNMN = 2 * M
2つのより正式な指数に保存してください.ij
Example 1:
Input: arr = [10,2,5,3]
Output: true
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
Example 2:
Input: arr = [7,1,14,11]
Output: true
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
Example 3:
Input: arr = [3,1,7,11]
Output: false
Explanation: In this case does not exist N and M, such that N = 2 * M.
に答える
これはJavaです.
class Solution {
public boolean checkIfExist(int[] arr) {
for(int i=0;i<arr.length;i++){
if(arr[i]%2==0){
for(int j=0;j<arr.length;j++){
if(i!=j&&arr[i]==arr[j]*2) return true;
}
}
}
return false;
}
}
ハッシュを使う方法もあります.Reference
この問題について(LeetCode - 1346. Check If N and Its Double Exist(Array, Hash Table, Two Pointers, Binary Search, Sorting)), 我々は、より多くの情報をここで見つけました https://velog.io/@yamamamo/LeetCode-1346.-Check-If-N-and-Its-Double-ExistArray-Hash-Table-Two-Pointers-Binary-Search-Sortingテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol