본문 바로가기

분류 전체보기342

💯 Daily LeetCode Challenge Day_12 - Reverse Bits Account Login - 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 문제 Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 rep.. 2020. 7. 12.
💯 Daily LeetCode Challenge Day_11 - Subsets Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: nums = [.. 2020. 7. 12.
Java 중복유무 순열 조합 import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class main { static int[] arr = {1, 2, 3, 4}; public static void main(String[] args) { int n = arr.length; //순서있는 모든 경우의 수 LinkedList perArr = new LinkedList(); int[] perCheck = new int[n]; for (int r = 0; r 2020. 7. 11.
Swift - 순열(Permutation) 1,2,3,4,5,6,7,8,9 중에서 중복되지 않고 3개를 뽑는 모든 경우의 수를 만들어라 예를들어 [1,2,3] [1,2,4] ... [9,8,7] 코드 import Foundation func permutation(_ arr : [Int],_ output : [Int],_ visited : [Bool],_ depth : Int,_ n : Int,_ r : Int){ var arr = arr var output = output var visited = visited if depth == r { // stack.append(output) print(output) return } for i in 0.. 2020. 7. 11.
RxSwift: Subjects Subjects observable 을 만들고 subscribe를 하고 작업이 끝난 뒤 dispose 하는 것 까지 배워봤다. 앱을 개발할 때 runtime동안 하나씩 새로운 값을 obervable에 추가하고 subscriber에 에 emit 하는 동작은 자주 쓰인다. 그래서 이걸 쉽게 하기 위해 Observable 과 Observer 둘의 역할을 동시에 할 수 있는 Subject가 생겼다. Create Subjects Subject를 만들고 Subscribe를 한 뒤에 추가되는 값들을 받아올 수 있다. func createSubject(){ let subject = PublishSubject() subject.onNext("Is anyone listening?") let subscriptionOne =.. 2020. 7. 10.
💯 Daily LeetCode Challenge Day_10 - Flatten a Multilevel Doubly Linked List Flatten a Multilevel Doubly Linked List - 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 문제 You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These chi.. 2020. 7. 10.
💯 Daily LeetCode Challenge Day_09 - Maximum Width of Binary Tree Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the sam.. 2020. 7. 9.
Swift - BFS iteration on Binary Tree [1,3,2,5,3,null,9] func bfs(_ root: TreeNode?) -> Int { if root == nil { return 0 } var maxWidth = 0 var queue : [TreeNode] = [] queue.append(root!) while(!queue.isEmpty){ var count = queue.count maxWidth = max(maxWidth,count) while(count > 0){ var tmp = queue.removeFirst() print(tmp.val) if(tmp.left != nil){ queue.append(tmp.left!) } if(tmp.right != nil){ queue.append(tmp.right!) } count -= 1 }.. 2020. 7. 9.
RxSwift: Observable Observable 만들기 기본적으로 observable 은 subscribe 하지 않으면 동작하지 않는다. subscribe 를 하면 element 가 하나씩 emit 되며 마지막에 completed 로 마무리 된다. just: 하나의 항목을 추가한다. of: single element로다 집어 넣는다. from: individual elements from an array of typed elements func createObservable() { let one = 1 let two = 2 let three = 3 let observable = Observable.just(one) let observable2 = Observable.of(one,two,three) let observable3 = O.. 2020. 7. 9.
Swift enum - 열거형 Enum : 열거형 연관된 항목들을 묶어서 표현 딱 정해진 값만 열거형 값에 속할 수 있음 각 열거형이 고유의 타입으로 인정됨 제한된 선택지를 주고싶을 때 정해진 값 외에는 입력받고 싶지 않을 때 예상된 입력 값이 한정되어 있을 때 enum Apple { case Macbook case MacbookPro case iPad case iPadPro case iPhone11 case iPhone11Pro case appleWatch } var device : Apple = Apple.iPad var device : Apple = .iPad //change value device = .iPadPro enum 은 원시값도 가질 수 있다. enum Apple { case Macbook = "맥북" case Mac.. 2020. 7. 9.
Swift Set Set 같은 타입의 데이터를 순서없이 하나의 묶음으로 저장 선언 var nameSet : Set = [] var nameSet : Set = Set() var nameSet : Set = ["name1","name2","name3"] 값 접근 //Set 크기 nameSet.count //Set 추가 nameSet.insert("name5") //Set 삭제 nameSet.remove("name1") Set 집합연산 let setA : Set = ["a","b","c","d"] let setB : Set = ["c","d","e"] //교집합 let intersectSet : Set = setA.intersection(setB) //여집합 let symmetricDiffSet : Set = setA.sym.. 2020. 7. 9.
💯 Daily LeetCode Challenge Day_08 - 3Sum Account Login - 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 문제 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate tri.. 2020. 7. 9.
RxSwift: Hello RxSwift!🖐 What is RxSwift? library for composing asynchronous and event-based code by using observable sequences and functional style operators, allowing for parameterized execution via schedulers simplifies developing asynchronous programs by allowing your code to react to new data and process in a sequential, isolated manner 왜 RxSwift 가 필요한가 parallel 한 코드를 작성하는 건 원래 힘들지만 특히 같은 데이터를 이용하는 병렬 코드를 작성하는건 정말 .. 2020. 7. 8.
💯 Daily LeetCode Challenge Day_07 - Island Perimeter Account Login - 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 문제 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by w.. 2020. 7. 7.
💯 Daily LeetCode Challenge Day_06 - Plus One Account Login - 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 문제 Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array co.. 2020. 7. 7.
Swift 알고리즘에 쓰이는 문법 정리 String 특정 기준으로 나누기 String -> [String] : components var str = "12345678" var arr = str.components(separatedBy : "5") //["1234","678"] String 빈칸 없애기 or 문자 치환 : replacingOccurrences var str = "1 2 3 4 5" let replaced = str.replacingOccurrences(of: " ", with: "") //"12345" String -> [String] : Array var str = "123 45" var arr = Array(str) //["1","2","3"," ","4","5"] [Int] -> [String] : map //digits .. 2020. 7. 7.
💯 Daily LeetCode Challenge Day_05 - Hamming Distance Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming di.. 2020. 7. 6.
💯 Daily LeetCode Challenge Day_04 - Ugly Number II Account Login - 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 문제 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Example: Input: n = 10 Output: 12 Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence.. 2020. 7. 6.
Swift 배열 처리 String 과 Int 를 넘나드는 데이터 타입 변경 배열 반대로 순회 배열을 String으로 리턴 코드 import Foundation var arr1 : [Int] = [3,2,3,1,2,8,6,4,3,5,2,3,2,1] var arr2 : [Int] = [9,7,5,4,3,4,2,1,3,5,0,5,4,3] var answer : [Int] = [] var carry = 0 for i in (0.. 2020. 7. 4.
💯 Daily LeetCode Challenge Day_03 - Prison Cells After N Days Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules.. 2020. 7. 3.
Swift String to Array (component) 예제 import Foundation var str = "1234 56789" var arr = str.components(separatedBy: "5") var arr2 = Array(str) print(arr) print(arr2) let replacedStr = str.replacingOccurrences(of: " ", with: "") var arr3 = replacedStr.components(separatedBy: "5") print(arr3) var eq = "1+2-3*4/5" print(eq) var eqArr = eq.components(separatedBy: ["+","-","*","/"]) .map{(value:String) -> Int in return Int(value)! } .. 2020. 7. 3.