본문 바로가기
반응형

전체 글83

makefile https://wh00300.tistory.com/193?category=962385 Linux gcc를 이용한 컴파일 / Makefile study(2) 이번에는 makefile을 실제로 작성 해보겠습니다. 먼저 linux환경이 필요한데, linux환경 없이 window만 사용하신 분은 본 블로그의 wsl 설치 포스팅을 참조하시기 바랍니다. pwd에는 다음과 같이 구 wh00300.tistory.com CC:=g++ CFLAGS=-Wall -g TARGET=bin/main SRC_DIRS=src SRCS=$(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.cpp)) OBJS=$(SRCS:.cpp=.o) run: all ./$(TARGET) all: clean $(OBJS).. 2022. 2. 9.
2차원 vector row만 초기화하기 서론 알고리즘을 풀다보면, vector에서 row만 초기화하고 col은 push_back을 사용해야 하는 경우가 생긴다. 코드 아래와 같이 첫번째 인자만 할당해주거나 두번째 인자로 vector만 생성해주면 된다. vector graph(row); vector graph2(row, vector()); 아래와 같은 경우에서 사용할 수 있다. #include using namespace std; int main(){ int numCourses = 2; vector prerequisites = {{1,0},{0,1}}; vector graph(numCourses); for(auto i : prerequisites){ graph[i[0]].push_back(i[1]); } } 물론 2차원 vector의 row, .. 2022. 2. 9.
Alacritty 터미널 설치하기 ubuntu 21.10 버전을 설치하고, gnome에서 기본으로 제공되는 terminal이 별로 마음에 안들어서 다른 terminal을 찾아보다가 alacritty를 발견했다. opengl 을 사용한 그래픽 가속으로 가볍고 속도가 빠른게 특징이라고 한다. sudo apt install alacritty 처음에는 위와 같이 apt 패키지 관리자로 설치했지만, 무슨 이유에서 인지 터미널에서 오류를 뿜어내며 실행이 되지 않았다. 그래서 어쩔 수 없이 직접 빌드하는 길을 택했다. Ubuntu 21.10 버전을 기준으로 한다. 1. cargo 설치하기 https://doc.rust-lang.org/cargo/getting-started/installation.html rust를 설치하면 cargo도 자동으로 설치.. 2022. 2. 7.
다음 순열 서론 ​ ❔ 문제 ​ Leetcode 31, Next Permutation 문제이다. 아래는 leetcode의 링크이다. https://leetcode.com/problems/next-permutation/ A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1]. The next permutation of an array of integers is the next lex.. 2022. 2. 5.
괄호를 추가하는 서로 다른 방법 서론 ❔ 문제 Leetcode 241, Different Ways to Add Parentheses 문제이다. 아래는 leetcode의 링크이다. https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order. 숫자와 연산자로 이루어진 스트링 형식의 수식이 주어지면, 숫자와 연산자를 묶을 수 있는 모든 .. 2022. 1. 29.
최단거리계산, 모든 최단거리경로 구하기 서론 이틀 전, 가만히 멍때리다가 BFS 알고리즘으로 최단거리를 구하는 법에 대해서 다시 고민해보았다. BFS의 queue로 같은 depth 순으로 위치 배열을 탐색하며 배열의 값에 1씩 더해주는 방식으로 최단거리를 계산할 수 있다. 그러던 중, 어떻게 하면 최단거리인 모든 경로를 출력할 수 있을까? 하는 궁금증이 생겼고 BFS을 사용해서 코딩을 해봤다. 🤔고민 최단거리 계산하는 코드는 어렵지 않게 작성할 수 있었지만 두번째는 시간이 조금 걸렸다. 무엇보다 경로를 저장하기 위해서는 다차원 배열을 사용해야하는데, vector를 이용한 다차원배열은 내게 익숙하지 않아서 검색하는 시간이 오래걸렸다. vector* route[row][col]; for(int i = 0; iend(); j++){ printf(".. 2022. 1. 20.
복호화 방법 서론 ❔문제 Leetcode 91번, Decode Ways 문제이다. 아래는 leetcode의 링크이다. https://leetcode.com/problems/decode-ways/ A-Z 를 포함하는 메시지는 다음과 같이 대응되어 암호화 될 수 있다. 'A' -> "1" 'B' -> "2" ... 'Z' -> "26" 암호화 된 메시지는 위와 같은 매핑의 반대로 다시 복호화 될 수 있다.(여러 방법이 있을 수 있다.) 예를 들어 "11106"의 경우 아래와 같이 복호화 될 수 있다. "AAJF" with the grouping (1 1 10 6) "KJF" with the grouping (11 10 6) (11 1 06) 의 경우 06은 6과 다르기 때문에 .. 2022. 1. 20.
모든 가능한 괄호의 조합 찾기 서론 ❔문제 Leetcode 22번, Generate Parentheses 문제이다. 아래는 leetcode의 링크이다. https://leetcode.com/problems/generate-parentheses/ n 쌍의 괄호가 주어졌을때 잘 만들어진 모든 괄호들의 조합을 생성하는 함수를 작성하시오. 테스트 케이스는 다음과 같다. Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Input: n = 1 Output: ["()"] n의 범위는 아래와 같다. 1 2022. 1. 20.
배열 초기화 0으로 배열을 초기화 하는 방법은 간단하다. 아래와 같이 배열을 생성할때 0으로 초기화해주면 된다. #include #include using namespace std; int main(){ int arr_1d[10] = {0,}; int arr_2d[10][10] = {0,}; //{{0,}} 도 가능하다. return 0; } 하지만 문제는 특정 값으로 한번에 초기화 할 경우이다. 예를 들어 배열을 5로 초기화 하고 싶을 때 위의 방법에서 0대신 5를 넣는다고 한번에 초기화 되질 않는다. 그래서 다음과 같은 방법이 있다. 1. 반복문으로 배열 초기화 가장 많이 사용하는 방법이다. 하지만 타이핑하는데 시간이 조금 걸린다. 1차원 배열 초기화 #include #include using namespace .. 2022. 1. 20.
코드 조각모음 height-balanced-BT Binary Tree가 height-balanced 인지 확인하는 코드이다. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr), right(nullptr) {} TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} }; class Solution { public: int search(TreeNode* t, bool& check){ if(!t) ret.. 2022. 1. 20.
반응형