본문 바로가기
반응형

알고리즘222

[알고리즘] Shortest Path in Binary Matrix 포인트 1. BFS 를 이용하여 2차원 데이터에서 제일 빠른 경로를 찾아주는 알고리즘을 작성합니다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: int visited[101][101]; int bfs(vector grid) { int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dy[] = {0, 0, 1, -1, 1, -1, 1, -1}; int n = grid.size(); queue q; q.push({0, 0, 1}); visited[0][0] = 1; while (!q.empty()) { int x, y, cnt; tie(x, y, cnt) = .. 2021. 2. 14.
[알고리즘] Intersection of Two Arrays 2 포인트 1. map, dictionary 를 이용해서 수를 계산을 하고 중복되는 값을 찾아보자 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: vector intersect(vector& nums1, vector& nums2) { unordered_map map; if(nums1.size() 0){ map[ele]-=1; result.push_back(ele);.. 2021. 2. 13.
[알고리즘] Excel Sheet Column Number 포인트 1. map, 딕셔너리를 사용하면 간단하게 풀 수 있습니다. 또한, 파이썬에서는 ord(), chr() C++ 에서도 변형하여 사용할 수 있으니 참고하시면 좋습니다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: int titleToNumber(string s) { string standard = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unordered_map map; for (int i=0; i0){ if(s.size()==1){ result+=(map[s[0]]*a); s=""; } else{ result += (map[s[s.size()-1]]*a); s .. 2021. 2. 12.
[알고리즘] Number of Steps to Reduce a Number to Zero 포인트 1. 홀수, 짝수 개념은 생각보다 많이 사용하니 잘 사용하면 굿!!! 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp class Solution { public: int numberOfSteps (int num) { int i=0; while (num>0){ i+=1; if(num&1){ num-=1; continue; } else{ num/=2; continue; } } return i; } }; python class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: a = 0 for i in range(len(nums)): for j in range(i+1, len(nums)): if(nums[i]==num.. 2021. 2. 12.
반응형