avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

217. Existing duplicate elements C++/Python3

  2024-01-01        
字数统计: 213字   |   阅读时长: 1min

2023-01-27 (2).png
217. Existing duplicate elements

Thought:

For the first time**C++**Write a question
Two methods:

  1. The first is sorting,Detect each time the first and the latter are equal;
  2. 第二种方法是Hash table,Determine whether the number has appeared for the second time。
    existC++middle,Force buckle into the arraynumsin usevector,So it can be used directlysort()function。
    I want to usePythonmiddle的字典(Hash table)if,Give to importunordered_set<int>function新建Hash table,Re -usefind()andend()判断是否已经存exist。

Code:

SortC++
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(),nums.end());
for (int i = 0; i < nums.size() - 1; i++){
if(nums[i] == nums[i+1]) {
return true;
}
}
return false;
}
};
Hash tableC++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
std::unordered_set<int> Hash;
for(int i: nums){
if(Hash.find(i) != Hash.end()){
return true;
}
else{
Hash.insert(i);
}
}
return false;
}
};
Hash tablepython
1
2
3
4
5
6
7
8
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
dict1 = Counter(nums)
dict1 = sorted(dict1.values())
if dict1[-1] >= 2:
return True
else:
return False
  • Python
  • solved,answer
  • Hash table
  • C++

扫一扫,分享到微信

微信分享二维码
209.Small length array
219.Existing duplicate elements II Hash table graphics
目录
  1. 1. Thought:
  2. 2. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号