전체 글
-
[Swift] Stack 구현하기Swift 2023. 1. 4. 00:05
Swift 라는 언어에서 자료구조인 Stack 을 지원 하지 않습니다. Stack 이란? 👇👇 탄창의 구조와 같습니다. Last In First Out 의 구조로써 처음들어간 총알을 제일 마지막에 나오게 되어있습니다. 반대로 마지막에 들어간 총알은 제일 처음 나오게 되어있습니다. import Foundation struct Stack { private var stack: [T] = [] public var count: Int { return stack.count } public var isEmpty: Bool { return stack.isEmpty } public mutating func push(_ element: T) { stack.append(element) } public mutating fun..
-
[Swift] struct 의 mutating funcSwift 2023. 1. 3. 22:44
struct SomeType { var number = 0.0 func add(_ value: Double) { self.number += value ✔️ 컴파일 에러 발생 } } var instance = SomeType() instance.add(1.0) print(instance.number) 기술 인터뷰하면서 너무 어이없는 실수로 질문에 대답을 못했다. 여기서 컴파일 에러가 나는 이유는 무엇일까? 그것은 add 의 실행문 내부에서 property 의 값을 변화 시키고자 하기 때문이다 mutating 키워드를 붙여야하는데.. 이걸 아는 건데 대답하지 못했다 바보인가 ㅠㅠ 아래와 같이 작성해야한다. struct SomeType { var number = 0.0 mutating func add(_ va..
-
[Swift] defer 란?Swift 2023. 1. 3. 22:39
함수가 종료되기 전에 실행하는 녀석 예를 들면 👇👇 func someFunc() { print("1") defer { print("defer") } print("2") } someFunc() // Output 1 2 defer 그리고 defer 를 여러개 사용할수도 있다 👇👇 그런데 stack 이라서 먼저 읽혀진 defer 의 실행문은 제일 늦게 출력된다. (Last In First Out) func someFunc() { print("1") defer { print("defer 1") } defer { print("defer 2") } defer { print("defer 3") } print("2") } someFunc() // Output 1 2 defer 3 defer 2 defer 1 그리고 r..
-
[Programmers] 나머지 한 점 - Swift (통과)Algorithm 2022. 12. 31. 20:00
프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 정사각형이 되려면 x가 총 4개 인데 그중 두개가 동일해야하고, y 4개중에 두개 두개가 동일해야 한다. 그래서 x 끼리 모으고 y 끼리 모은 다음에 1개씩만 남은 좌표를 구하고 x, y 를 모은 다음에 리턴해주었다. func solution(_ v: [[Int]]) -> [Int] { var ans: [Int] = [] var x: [Int] = [] var y: [Int] = [] for location in v { x.append(location[0]) y.append(location[1]) } let ..
-
[Swift] NSCountedSet - 리스트에서 중복된 요소의 갯수를 구해보자Swift 2022. 12. 31. 19:49
이놈으로 말할것 같으면 Array 안에 동일한 요소가 몇개 있는지 알려주는 아주 친절한 녀석이다 알고리즘 풀때 아주 요긴하게 사용할 수 있다 대충 사용은 아래와 같이 하면 됩니다 👇👇 import Foundation let array = [1,1,2,3,3,4] let countedArray = NSCountedSet(array: array) print(countedArray.count(for: 1)) // 2 print(countedArray.count(for: 2)) // 1 print(countedArray.count(for: 3)) // 2 print(countedArray.count(for: 4)) // 1
-
[Leetcode] 1578. Minimum Time to Make Rope Colorful - Swift 1차 시도 (탈락)Algorithm 2022. 12. 30. 20:51
https://leetcode.com/problems/minimum-time-to-make-rope-colorful/ Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon. Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are..
-
[Leetcode] 1375. Number of Times Binary String Is Prefix-Aligned - Swift (통과)Algorithm 2022. 12. 30. 17:15
https://leetcode.com/problems/number-of-times-binary-string-is-prefix-aligned/description/ flip 이라는 배열이 들어온다. 그리고 전구의 수는 배열의 길이 만큼있다. 각 전구에는 스위치가 연결되어 있다. 하지만 각 전구의 스위치는 이전 전구의 스위치와 연결이 되어있어 이전 전구가 켜져있어야 전구의 스위치를 올렸을 때 전구가 켜진다. 0번 전구와 1번전구가 연결되어 있고 1번 전구와 2번 전구가 연결되어있다. 0번이 안켜져있는 상태에서 2번은 켜도 소용이 없고 0번 1번이 켜져있어야 2번 스위치를 올렸을 때 불이 들어온다. 그리고 flips 배열에는 스위치를 켜는 순서가 담겨있다 예) flips = [3,2,4,1,5] 라는 배열이 ..