?LeetCode刷題實(shí)戰(zhàn)217:存在重復(fù)元素
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
題意
示例
示例 1:
輸入: [1,2,3,1]
輸出: true
示例 2:
輸入: [1,2,3,4]
輸出: false
示例 3:
輸入: [1,1,1,3,3,4,3,2,4,2]
輸出: true
解題
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> hashSet = new HashSet<>();
if (nums.length <= 1) return false;
for (int num : nums) {
if (hashSet.contains(num)) return true;
else hashSet.add(num);
}
return false;
}
}
LeetCode1-200題匯總,希望對(duì)你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)201:數(shù)字范圍按位與
LeetCode刷題實(shí)戰(zhàn)202:快樂(lè)數(shù)
LeetCode刷題實(shí)戰(zhàn)203:移除鏈表元素
LeetCode刷題實(shí)戰(zhàn)204:計(jì)數(shù)質(zhì)數(shù)
LeetCode刷題實(shí)戰(zhàn)205:同構(gòu)字符串
LeetCode刷題實(shí)戰(zhàn)206:反轉(zhuǎn)鏈表
LeetCode刷題實(shí)戰(zhàn)207:課程表
LeetCode刷題實(shí)戰(zhàn)208:實(shí)現(xiàn) Trie (前綴樹(shù))
LeetCode刷題實(shí)戰(zhàn)209:長(zhǎng)度最小的子數(shù)組
LeetCode刷題實(shí)戰(zhàn)210:課程表 II
LeetCode刷題實(shí)戰(zhàn)211:添加與搜索單詞
LeetCode刷題實(shí)戰(zhàn)212:?jiǎn)卧~搜索 II
LeetCode刷題實(shí)戰(zhàn)213:打家劫舍 II
LeetCode刷題實(shí)戰(zhàn)214:最短回文串
