Contents

qr code(with.Java)

   Sep 16, 2023     1 min read

qr code(with.Java)์— ๋Œ€ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

๋‘ ์ •์ˆ˜ q, r๊ณผ ๋ฌธ์ž์—ด code๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, code์˜ ๊ฐ ์ธ๋ฑ์Šค๋ฅผ q๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ r์ธ ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ์•ž์—์„œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ด์–ด ๋ถ™์ธ ๋ฌธ์ž์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ
qrcoderesult
31โ€œqjnwezgrpirldywtโ€โ€œjerryโ€
10โ€œprogrammersโ€โ€œprogrammersโ€

๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‚˜์˜ ํ’€์ด

class Solution {
    public String solution(int q, int r, String code) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < code.length(); i++){
            if(i % q == r) sb.append(code.charAt(i));
        }
        return sb.toString();
    }
}
ํ’€์ด ์„ค๋ช…

StringBuilder sb = new StringBuilder();: ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•œ StringBuilder ๊ฐ์ฒด sb๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

for(int i = 0; i < code.length(); i++) {: ๋ฌธ์ž์—ด code๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ๊ฐ ๋ฌธ์ž๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

if(i % q == r) sb.append(code.charAt(i));: ํ˜„์žฌ ์ธ๋ฑ์Šค i๋ฅผ q๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ r๊ณผ ๊ฐ™๋‹ค๋ฉด, ํ˜„์žฌ ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๋ฅผ sb์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

return sb.toString();: ์ตœ์ข…์ ์œผ๋กœ sb์— ์ €์žฅ๋œ ๋ฌธ์ž๋“ค์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฝ”๋“œ๋Š” ์ž…๋ ฅ ๋ฌธ์ž์—ด code์—์„œ q๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ r๊ณผ ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋Š” ๋ฌธ์ž๋“ค์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, q๊ฐ€ 3์ด๊ณ  r์ด 1์ด๋ฉด, ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๊ฐ€ 1, 4, 7, 10, โ€ฆ ๋“ฑ 3์˜ ๋ฐฐ์ˆ˜์—์„œ ๋ฌธ์ž๋ฅผ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.