[Swift] ๊ตฌ๋ฆ„ - 1์ฐจ์› ๋ฟŒ์š”๋ฟŒ์š”
ยท
Algorithm/Swift
๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์ฒ˜์Œ์—๋Š” ์•ž, ๋’ค๋ฅผ ๋น„๊ตํ•ด์„œ ๊ฐ™์€ ๊ฐ’์ด ๋‚˜์˜ฌ๋•Œ start, ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜ฌ๋•Œ end๋ฅผ ์ €์žฅํ•ด ํ•ด๋‹น ๋ถ€๋ถ„์„ ๋นผ์ฃผ๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊น ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ†ต๊ณผํ–ˆ๋Š”๋ฐ ์ œ์ถœ์—์„œ ๋ช‡ ๊ฐœ ํ†ต๊ณผ๊ฐ€ ์•ˆ๋๋‹ค.์•Œ๊ณ ๋ณด๋‹ˆ ์Šคํƒ๊ณผ ๊ด€๋ จ๋œ ๋ฌธ์ œ์˜€๊ณ , ์Šคํƒ์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.์šฐ์„  ์Šคํƒ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ , block์„ ํ•˜๋‚˜์”ฉ ์ง‘์–ด๋„ฃ์œผ๋ฉด์„œstack์˜ ๋งˆ์ง€๋ง‰ block๊ณผ ํ˜„์žฌ block์ด ๊ฐ™์ง€ ์•Š์„ ๋•Œ ๋งˆ์ง€๋ง‰ block์˜ count๊ฐ€ m๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ block ์‚ญ์ œstack์˜ ๋งˆ์ง€๋ง‰ block๊ณผ ํ˜„์žฌ block์ด ๊ฐ™๋‹ค๋ฉด stack์˜ ๋งˆ์ง€๋ง‰ block์˜ Count += 1, ๊ฐ™์ง€ ์•Š์œผ๋ฉด ์ƒˆ๋กœ append ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.์ค‘๊ฐ„์— ์ด๊ฑธ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•œ๊ฑด ์•„๋‹Œ๋ฐ ์•„๋ฌดํŠผ ๋ฐ˜๋Œ€๋กœ ํ•ด๋„ ๋  ์ค„ ์•Œ์•„์„œ ์ข€ ๋ง‰..
[Swift] ๋ฐฑ์ค€ 12933 - ์˜ค๋ฆฌ
ยท
Algorithm/Swift
๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์ฒ˜์Œ์—๋Š” ์•„๋ž˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ quack / quack์ด๋ผ ๋‘๋งˆ๋ฆฌ์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ ์—ฐ์†๋œ quack์€ ํ•œ๋งˆ๋ฆฌ์˜€๋˜๊ฑฐ์ž„!!๊ทธ๋ฆฌ๊ณ  ์šธ์Œ์†Œ๋ฆฌ๋ฅผ ์ „๋ถ€ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋ถ€๋ถ„์ด ์ข€ ํ—ท๊ฐˆ๋ ธ๋‹ค.๊ทธ๋ž˜์„œ ์ •์ƒ์ ์ธ ์šธ์Œ์†Œ๋ฆฌ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ- q๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜- k๋กœ ๋๋‚˜์ง€ ์•Š๊ฑฐ๋‚˜- ์šธ์Œ์†Œ๋ฆฌ ์ „์ฒด๊ฐ€ 5๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.duckSound[0] != "q" || duckSound[soundCnt-1] != "k" || soundCnt % 5 != 0 ๋˜ํ•œ ์šธ์Œ ์†Œ๋ฆฌ์˜ ์ˆ˜๋Š” ๋งž์ง€๋งŒ ์œ— ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์ด์ƒํ•œ ์šธ์Œ์†Œ๋ฆฌ๋ฅผ ๊ฑฐ๋ฅด๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด ์ถ”๊ฐ€ํ–ˆ๋‹ค.if duckSound[i] == "q" && visited[i] == 0 { getDuckCnt(i)}getDuckCntfunc get..
[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํŠน์ • ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ฐพ๊ธฐ
ยท
Algorithm/Swift
๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‚ด ๊ณ„ํš์€ pat์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ, ์ฒ˜์Œ๋ถ€ํ„ฐ pat์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ์˜€๋‹ค.๊ทธ๋ž˜์„œ suffix๋กœ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ์ฐพ์•˜๊ณ , ์ฒ˜์Œ๋ถ€ํ„ฐ pat๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š”๋ฐ...์ด๊ฑฐ ํ’€๋•Œ๋Š” Swift ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ์ „ํ˜€ ๋ชจ๋ฅด๋Š” ์ƒํƒœ๋ผ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ ๋„์ „ํ•ด๋ดค๋‹ค.์ฐธ๊ณ ๋กœ ๋ธ”๋กœ๊ทธ ์˜ฎ๊ธฐ๋Š” ์ค‘์ด๋‹ค.. let end = pat.suffix(1) let lastIndex = myString.lastIndex(of: end) return myString[0...lastIndex]error: cannot convert value of type 'String.SubSequence' (aka 'Substring') to expected argument type '..