๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

level1. ์Œ์–‘ ๋”ํ•˜๊ธฐ Java ๋ณธ๋ฌธ

๐ŸŽ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

level1. ์Œ์–‘ ๋”ํ•˜๊ธฐ Java

jhj.sharon 2023. 9. 12. 17:19
๋ฐ˜์‘ํ˜•

โœจ ๋ฌธ์ œ

๐Ÿ’ป์ฝ”๋“œ

class Solution {
    public int solution(int[] absolutes, boolean[] signs) {
        int answer = 0;
        
        for(int i=0; i<absolutes.length; i++){
            if(signs[i]){
                answer += absolutes[i];
            }else{
                answer -= absolutes[i];
            }
        }
        return answer;
    }
}

 

๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์ด ๋ฐฉ๋ฒ•

  • ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค. ์Œ์–‘ ๋ถ€ํ˜ธ๊ฐ€ boolean ํ˜•ํƒœ๋กœ ์ฃผ์–ด์กŒ๊ธฐ ๋•Œ๋ฌธ์— if ์กฐ๊ฑด์‹์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ตฌํ–ˆ๋‹ค.

๐Ÿ”” ๋Š๋‚€์ 

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๋ฅผ ๋ณด๋ฉด์„œ if~ then ~ ์ ˆ์„ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ๋ฐฐ์› ๋‹ค. 
  •  
 answer += (signs[i]) ? absolutes[i] : -absolutes[i];
๋ฐ˜์‘ํ˜•
Comments