320x100

전체 글 435

1. Two Sum

https://leetcode.com/problems/two-sum/ Two Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정수 목록이 주어졌을 때, 목록 내 두 정수의 합이 목표 값이 되는 경우 그 목록의 인덱스를 반환하는 문제다. [C++] class Solution { public: vector twoSum(vector& nums, int target) { unordered_map cache; for (int i = 0; i < nums.size..

자료형 및 연산자

수치 int, long, float, complex등 문자 str 리스트 값의 나열이다. 순서가 존재하며, 여러 종류의 값을 담을 수 있다 0부터 시작하는 인덱스가 있으며, 슬라이싱도 가능하다. // 리스트 생성 colors = ['red', 'green', 'gold'] // 리스트 항목 추가 colors.append('blue') // 중간에 항목 삽입 colors.insert(1, 'black') // 다중 삽입 colors.extend(['white', 'gray']) // 항목 추가 colors += ['red'] // 항목 위치 찾기 colors.index('red') colors.index('red',1) // 시작점 추가 // 원소 갯수 colors.count('red') // 맨 마지막 ..

기본 사항

변수 -- 변수 생성 a = 10 -- 변수 삭제 a = nil 변수 타입 : 루아는 동적 타입 지정 언어이므로 별도의 데이터 타입을 지정하는 정의가 없다. 각 값은 내부에 자신의 타입을 담고 있다. 문자열(string) 수치(number) 함수 부울형(boolean) 닐(nil) 닐(nil) 정상 값이 아니라는 뜻으로 사용 전역 변수를 지울 때 대입한다. 부울형(boolean) true와 false를 가진다. 조건에선 false와 nil만 거짓으로, 나머지는 참으로 판단된다. 수치(number) 배정도 부동소수점(double-precision floating point) 수치를 뜻한다. 기본으로 실수형을 사용하며, 정수를 표현할 때 (10^14)를 넘지 않는다면 반올림 오차 없이 표현이 가능하다. lo..

프로그래밍/lua 2021.08.07

50. Pow(x, n)

https://leetcode.com/problems/powx-n/ Pow(x, n) - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 실수d 와 제곱수n 를 받았을 때 pow(d,n)을 구하는 문제다. 단순히 d를 n번 곱셈하는게 제일 쉽겠지만, 그 안에서 반복되는 횟수를 줄여야 한다. 10번 처리할거 5번으로 줄이는 방식으로 2를 나누어 2배수로 처리한다. 더 큰 배수로 처리해도 되지만 예외처리가 그만큼 늘어날 수 있다. 제곱수n은 정수이므로 2로 나누었을 ..

49. Group Anagrams

https://leetcode.com/problems/group-anagrams/ Group Anagrams - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문자열의 목록이 주어졌을 때, Anagram(원본 문자를 한번 사용으로 재배열해 만든 문자)을 그룹화 해 반환하는 문제다. 주어진 문자열들을 각각 정렬하고 동일한 문자들끼리 모아둔다. (정렬된 문자열을 key로 unordered_map 사용) 그 후에 모아둔 문자열을 2중 벡터에 담아 반환한다. class..

53. Maximum Subarray

https://leetcode.com/problems/maximum-subarray/ Maximum Subarray - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 이지인데 난 왜 헤메나;; 정수 목록이 주어졌을 때 합이 최대가 되는 부분 목록의 합을 구하는 문제다. 누적된 합보다 인수가 클 때 다시 누적을 시작한다. 금요일인데 억지로 하고 있어서 그런가... 방법과 로직은 이해가 되는데 서로 연결이 안된다.. 왜 max 함수 내용으로 그것이 구현되는가... ..

35. Search Insert Position

https://leetcode.com/problems/search-insert-position/ Search Insert Position - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 금요일이 고단해 easy 두 문제로 간다. 정렬된 목록과 목표 숫자가 있을 때, 목록에 있다면 index를, 없다면 순서대로 삽입될 index를 리턴한다. class Solution { public: int searchInsert(vector& nums, int target) ..

16. 3Sum Closest

https://leetcode.com/problems/3sum-closest 3Sum Closest - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 임의의 정수(-10^3 ~ 10^3) 목록이 주어졌을 때, 3개의 숫자를 더해 주어진 목표 값과 가장 가까운 합을 구하는 문제다. 15. 3Sum 직전에 풀었던 문제(15. 3Sum)는 case를 구하는 문제였으므로 중복을 제거해 연산 수를 줄였지만, 이 문제는 동일한 숫자도 덧셈에 활용될 수 있어 중복 수는 제거..

15. 3Sum

https://leetcode.com/problems/3sum/ 3Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정수의 목록이 주어졌을 때, 3개의 정수를 더했을 경우 0이 되는 목록을 2차원 배열에 담아 반환하는 문제다. 단순히 생각하면 O(N^3)이지만 O(N^2)로 낮추는게 핵심인것 같다. 일단 받은 정수의 목록을 오름차순으로 정렬해놓고, 첫번째 숫자부터 끝까지 반복하며 나머지 목록을 검사한다. case를 반환하는 문제기 때문에 중복들은 전부 건..

70. Climbing Stairs

https://leetcode.com/problems/climbing-stairs/ Climbing Stairs - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정수로 주어진 n개의 계단을 1계단 또는 2계단씩 올라가려고 한다. 몇 가지 방법으로 올라갈 수 있는지 구하는 문제다. 먼저 n개의 계단을 올라갈 수 있는 방법은 2가지가 있다. 첫번째, 1개의 계단을 오르고 N-1계단을 올라가는 방법 두번째, 2개의 계단을 오르고 N-2계단을 올라가는 방법 T(n) ..

320x100