๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 150368] ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธํ–‰์‚ฌ swift

๋ฌธ์ œ ์„ค๋ช… ๋ฌธ์ œ ํ’€์ด ํ• ์ธ์œจ์ด ๋†’๋‹ค๊ณ  ์ข‹์€ ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ  ๊ธฐ์ค€์•ก์ด ๋‚ฎ๋‹ค๊ณ  ์ข‹์€ ๊ฒƒ๋„ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•˜๋‹ค. ๋”ฐ๋ผ์„œ ํ• ์ธํ•˜๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ง€๋ฉฐ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— DFS๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. DFS(ํ• ์ธ๊ฐœ์ˆ˜, [ํ• ์ธํผ์„ผํŠธ๋“ค]) ๋กœ ๋Œ๋ฆฌ๋ฉฐ ํ• ์ธ๊ฐœ์ˆ˜๊ฐ€ ์ด๋ชจํ‹ฐ์ฝ˜ ๊ฐœ์ˆ˜๋ž‘ ๊ฐ™์„ ๋•Œ ๋ฉˆ์ถ”๊ณ  ์ด๋ชจํ‹ฐ์ฝ˜ ์‚ฌ์šฉ์ž์˜ ์ˆ˜์™€ ์ด์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์ฝ”๋“œ import Foundation func solution(_ users:[[Int]], _ emoticons:[Int]) -> [Int] { // ๊ฐ€์ž…์ž ์ˆ˜, ํŒ๋งค์•ก ๊ณ„์‚ฐํ•ด์„œ ๋ฆฌํ„ด ex) [1, 5100] func calcResult(_ percents: [Int]) -> [Int] { var answer = [0, 0] for i in 0..

article thumbnail
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 86971] ์ „๋ ฅ๋ง์„ ๋‘˜๋กœ ๋‚˜๋ˆ„๊ธฐ swift

๋ฌธ์ œ ํ’€์ด DFS๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค. 1. ์ด์ค‘๋ฐฐ์—ด์— ์—ฐ๊ฒฐ์ ์„ ๋„ฃ๋Š”๋‹ค. (vertex๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘์ด๊ธฐ ๋•Œ๋ฌธ์— 0์€ ๋น„์›Œ๋‘ ) ex) [[], [3], [3], [1,2,4]] 2. wires๋ฅผ ํฌ๋ฌธ์„ ๋Œ๋ฆฌ๋ฉฐ ์—ฐ๊ฒฐ์ ์„ ์ œ๊ฑฐํ•ด๋ณธ๋‹ค. 3. ํ•˜๋‚˜ ์ œ๊ฑฐํ•œ ์ƒํƒœ์—์„œ DFS๋ฅผ ๋Œ๋ ค๋ณธ๋‹ค. ์ด๋•Œ DFS๋Š” DFS(๊ทธ๋ž˜ํ”„, ํ˜„์žฌ์ •์ )์ด๊ณ  DFS๊ฐ€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋๊นŒ์ง€ ๊ฐ”์„ ๋•Œ ๋ฐฉ๋ฌธํ•œ ์ •์  ๊ฐœ์ˆ˜ count๋ฅผ ๊ตฌํ•œ๋‹ค. (ํ•œ ์ชฝ์˜ depth๋งŒ ๊ตฌํ•ด๋„ ๋‹ค๋ฅธ ํ•œ์ชฝ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ 1๋ถ€ํ„ฐ ๋Œ๋ฆฌ๊ณ  ๋๊นŒ์ง€ ๊ฐ€๋ณธ๋‹ค.) 4. wires ํ•˜๋‚˜ ์ œ๊ฑฐํ• ๋•Œ๋งˆ๋‹ค ์†ก์ „ํƒ‘์˜ ๊ฐœ์ˆ˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•œ๋‹ค. (๋‹ค์Œ wires๋บ„๋•Œ visited, count ์ดˆ๊ธฐํ™”) ์ฝ”๋“œ func solution(_ n:Int, _ wires:[[Int]]) -> Int..

article thumbnail
[Swift Language Guide] ARC
Swift Language Guide 2023. 9. 7. 22:21

ARC ์ž๋™ ์ฐธ์กฐ ์นด์šดํŒ… Automatic Reference Counting swift๋Š” ARC๋กœ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์„ ๋•Œ ์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์ค€๋‹ค. ๋‹น์—ฐํ•˜๊ฒŒ๋„ struct์™€ enum์€ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” valueํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น๋˜์ง€ ์•Š๊ณ  ์ฐธ์กฐ ํƒ€์ž…์ธ class์˜ ์ธ์Šคํ„ด์Šค์—๋งŒ ์ฐธ์กฐ ์นด์šดํŒ…์ด ์ ์šฉ๋œ๋‹ค. ์ž๋ฐ”์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰์…˜(GC)์™€ ๋‹ค๋ฅธ์ ์€ GC๋Š” ๋Ÿฐํƒ€์ž„๋•Œ ๊ณ„์† ์ฐธ์กฐ๋ฅผ ์ถ”์ ํ•˜๊ณ  RC๋Š” ์ปดํŒŒ์ผ๋•Œ ๋ชจ๋‘ ๊ฒฐ์ •์ด ๋‚œ๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค. ARC์˜ ์ž‘๋™ ์›๋ฆฌ ํด๋ž˜์Šค์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ๋งˆ๋‹ค ARC๋Š” ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์คŒ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋”์ด์ƒ ํ•„์š”์น˜ ์•Š์„ ๋•Œ ARC๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์„ ํ•ด์ œ ํ•ด์คŒ ์‚ฌ์šฉ์ค‘์ธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œํ•˜๋ฉด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธ์Šคํ„ด์Šค์— ๊ฐ•ํ•œ ์ฐธ์กฐ (strong reference)..

article thumbnail
[Swift Language Guide] ํ”„๋กœํ† ์ฝœ (Protocols)
Swift Language Guide 2023. 8. 1. 21:25

ํ”„๋กœํ† ์ฝœ (Protocols) ๋”ฐ๋ผ์•ผํ•˜๋Š” ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ •์˜ํ•œ๋‹ค. class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol { // class definition goes here } ์ž‘์„ฑ ์ˆœ์„œ : ๋ถ€๋ชจํด๋ž˜์Šค ๋จผ์ € ์ž‘์„ฑํ•˜๊ณ  ๊ทธ ๋‹ค์Œ์— ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•œ๋‹ค. protocol SomeProtocol { var mustBeSettable: Int { get set } var doesNotNeedToBeSettable: Int { get } } ๊ฐ ํ”„๋กœํผํ‹ฐ๊ฐ€ gettable ์ธ์ง€ gettable๊ณผ settable ์ธ์ง€ ์ง€์ •ํ•ด์ค˜์•ผ ํ•จ mutating ๋ฉ”์†Œ๋“œ ์š”๊ตฌ์‚ฌํ•ญ protocol Togglable { mutating func toggle() } enum On..

profile on loading

Loading...