Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

ํ•œ๊ตญ์ค‘ํ•™๊ต์— ๋‹ค๋‹ˆ๋Š” ํ•™์ƒ๋“ค์€ ๊ฐ์ž ์ •์ˆ˜ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•™๊ต ํ•™์ƒ 3๋ช…์˜ ์ •์ˆ˜ ๋ฒˆํ˜ธ๋ฅผ ๋”ํ–ˆ์„ ๋•Œ 0์ด ๋˜๋ฉด 3๋ช…์˜ ํ•™์ƒ์€ ์‚ผ์ด์‚ฌ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 5๋ช…์˜ ํ•™์ƒ์ด ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ์ •์ˆ˜ ๋ฒˆํ˜ธ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ -2, 3, 0, 2, -5์ผ ๋•Œ, ์ฒซ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ ํ•™์ƒ์˜ ์ •์ˆ˜ ๋ฒˆํ˜ธ๋ฅผ ๋”ํ•˜๋ฉด 0์ด๋ฏ€๋กœ ์„ธ ํ•™์ƒ์€ ์‚ผ์ด์‚ฌ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋‘ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ, ๋‹ค์„ฏ ๋ฒˆ์งธ ํ•™์ƒ์˜ ์ •์ˆ˜ ๋ฒˆํ˜ธ๋ฅผ ๋”ํ•ด๋„ 0์ด๋ฏ€๋กœ ์„ธ ํ•™์ƒ๋„ ์‚ผ์ด์‚ฌ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ๊ฒฝ์šฐ ํ•œ๊ตญ์ค‘ํ•™๊ต์—์„œ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ผ์ด์‚ฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•œ๊ตญ์ค‘ํ•™๊ต ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด number๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ํ•™์ƒ๋“ค ์ค‘ ์‚ผ์ด์‚ฌ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

 
 
์ œํ•œ์‚ฌํ•ญ
  • 3 ≤ number์˜ ๊ธธ์ด ≤ 13
  • -1,000 ≤ number์˜ ๊ฐ ์›์†Œ ≤ 1,000
  • ์„œ๋กœ ๋‹ค๋ฅธ ํ•™์ƒ์˜ ์ •์ˆ˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ™์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ž…์ถœ๋ ฅ ์˜ˆnumberresult
[-2, 3, 0, 2, -5] 2
[-3, -2, -1, 0, 1, 2, 3] 5
[-1, 1, -1, 1] 0

 

 


์ „์ฒด ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ๊ฒน์น˜์ง€์•Š๊ฒŒ ํ•œ๊ฐœ์”ฉ ๋ชจ๋‘ ๋”ํ•ด๋ณด๋ฉด ๋˜๋Š”๊ฑฐ๋‹ˆ

ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ƒ๊ฐํ•ด์„œ ์‰ฝ๊ฒŒ ํ’€์—ˆ๋‹ค.

 

public class Solution {
    public int solution(int[] number) {
        int answer = 0;
        for(int lt=0; lt<number.length-2; lt++){
            for(int mt=lt+1; mt<number.length-1; mt++){
                for(int rt=mt+1; rt<number.length; rt++){
                    if(number[lt]+number[mt]+number[rt]==0){
                        answer++;
                    }
                }
            }
        }
        return answer;
    }
}