ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

๋‹จ์–ด s์˜ ๊ฐ€์šด๋ฐ ๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”. ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด ๊ฐ€์šด๋ฐ ๋‘๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์žฌํ•œ์‚ฌํ•ญ

  • s๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ, 100์ดํ•˜์ธ ์ŠคํŠธ๋ง์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

s return 
abcde c
qwer we

ํ’€์ด

class Solution {
  public String solution(String s) {
      String answer = "";
      
      int index;
		
		int sLength = s.length();
		
		if(sLength%2 == 0) {
			//์ง์ˆ˜์ผ ๊ฒฝ์šฐ
			index = sLength/2-1;
			answer = Character.toString(s.charAt(index)) + Character.toString(s.charAt(index+1));
			
		} else {
			//ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ
			index = sLength/2;
			answer = Character.toString(s.charAt(index));
		}
      
      return answer;
  }
}

์ด๋ฒˆ ๋ฌธ์ œ๋„ ๊ธˆ๋ฐฉ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

๊ทธ๋Ÿฐ๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ

return word.substring((word.length()-1) / 2, word.length()/2 + 1);

substring์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ ์ค„๋กœ ํ•ด๊ฒฐํ•ด ๋ฒ„๋ ธ๋‹ค. ๋ฉ‹์ ธ,,

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€