본문 바로가기

Algorithm/Leet Code

LeetCode #705 RelativeRanks. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

LeetCode #705 RelativeRanks. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

 

 

Runtime: 264 ms, faster than 11.44% of C++ online submissions for Design HashSet.

Memory Usage: 35.8 MB, less than 98.26% of C++ online submissions for Design HashSet.

 

Badest performance

 

 그냥 vector 이용해서 해시셋 개념에 맞게 대충 만들었는데 퍼포먼스가 엄청 안나옴

뭐 다른 빠른게 있는지는 나중에 찾아봄

 

LeetCode #705

Q.

 Design a HashSet without using any built-in hash table libraries.

To be specific, your design should include these functions:

  • add(value): Insert a value into the HashSet. 
  • contains(value) : Return whether the value exists in the HashSet or not.
  • remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.

 해쉬셋을 디자인하는데, hash table 라이브러리 쓰지말고 만들어봐라

구체적으로, 이러한 함수들을 디자인 해야한다.

 add(value) : 해시셋에 값을 넣는다.

 contains(value) : 해시셋에 값이 있는지 없는지 반환한다.

 remove(value) : 해시셋에서 값을 지운다. 없는거면 아무 일도 없다.

 


Example:

 

MyHashSet hashSet = new MyHashSet();

hashSet.add(1);        

hashSet.add(2);        

hashSet.contains(1);    // returns true

hashSet.contains(3);    // returns false (not found)

hashSet.add(2);          

hashSet.contains(2);    // returns true

hashSet.remove(2);          

hashSet.contains(2);    // returns false (already removed)

 


Note:

  • All values will be in the range of [0, 1000000].
  • The number of operations will be in the range of [1, 10000].
  • Please do not use the built-in HashSet library.

모든 값들은 0 ~ 1000000 사이 값이다.

명령의 수는 1 ~ 10000 이다.

HashSet library를 쓰지 말아라

 

 

 

Code.. lemme see example code!!!

코드.. 예제코드를 보자!!!

 

 

 

class MyHashSet {
private:
	vector<int> *list;
public:
	/** Initialize your data structure here. */
	MyHashSet() {
		this->list = new vector<int>();
	}

	void add(int key) {
		if (std::find(list->begin(), list->end(), key) == list->end()) {
			list->push_back(key);
		}
	}

	void remove(int key) {
		//erase-remove idiom stl technique
		list->erase(std::remove(list->begin(), list->end(), key), list->end());
	}

	/** Returns true if this set contains the specified element */
	bool contains(int key) {
		if (std::find(list->begin(), list->end(), key) != list->end()) {
			return true;
		}
		else {
			return false;
		}
	}
};

/**
 * Your MyHashSet object will be instantiated and called as such:
 * MyHashSet* obj = new MyHashSet();
 * obj->add(key);
 * obj->remove(key);
 * bool param_3 = obj->contains(key);
 */

 

 

 

Something else you might like...?

 

 

2019/04/28 - [Algorithm/Leet Code] - LeetCode #121 BestTimeToBuyAndSellStock. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/24 - [Algorithm/Leet Code] - LeetCode #119 Pascal'sTriangle2. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 파스칼..

2019/04/24 - [Algorithm/Leet Code] - LeetCode #104 MaximumDepthOfBinaryTree. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/22 - [Algorithm/Leet Code] - LeetCode #118 Pascal'sTriangle. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/17 - [Algorithm/Leet Code] - LeetCode #88 MergeSortedAray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/17 - [Algorithm/Leet Code] - LeetCode #021 MergeTwoSortedLists. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

 

 

2019/04/14 - [Programming/C++] - C++ Math - sqrt (square root, 제곱근, 루트). stl, math.h, 씨쁠쁠, example code, 예제코드

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법