๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm_JS

์•Œ๊ณ ๋ฆฌ์ฆ˜(7) : ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ _ Lv.1

by oranssy 2022. 11. 3.
728x90
๋ฐ˜์‘ํ˜•

โญ๏ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜(7) : ๊ฐ€์šด๋ฐ ๊ธ€์ž ๊ฐ€์ ธ์˜ค๊ธฐ _ Lv.1 โญ๏ธ


01. ๋ฌธ์ œ ์„ค๋ช…

๐Ÿงฉ ๋‹จ์–ด s์˜ ๊ฐ€์šด๋ฐ ๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.
๐Ÿงฉ ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด ๊ฐ€์šด๋ฐ ๋‘ ๊ธ€์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๐Ÿงฉ ์˜ˆ๋ฅผ ๋“ค์–ด str์ด "abcde"์ด๋ฉด "c"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , "qwer"์ด๋ฉด "we"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๐Ÿ”’ ์ œํ•œ ์กฐ๊ฑด : s๋Š” ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ, 100์ดํ•˜์ธ ์ŠคํŠธ๋ง์ž…๋‹ˆ๋‹ค.

02. " solution.js " ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ

๐Ÿ’ก ๊ฐ€์šด๋ฐ ๊ธ€์ž๋Š” ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ’์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆด์„ ๋•Œ ์œ„์น˜์ž„์„ ์ƒ๊ฐํ•˜๊ณ , ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์™€ ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ ์„œ ๋ฌธ์ž์—ด์„ ๋ถ€๋ถ„ ์ถœ๋ ฅํ•ด๋ณด์ž.

const str1 = "level";
const str2 = "updown";
function solution(s) {
let strLength = Math.floor(s.length / 2); // ๊ฐ€์šด๋ฐ ๊ธ€์ž ์ฐพ๊ธฐ : index ๊ฐ’์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด '๋‚ด๋ฆผ'
// ex) Math.floor(5 / 2) = 2 // Math.floor(6 / 2) = 3
return s.length % 2 ? s.substr(strLength, 1) : s.substr(strLength - 1, 2);
} // ํ™€์ˆ˜์ด๋ฉด strLength๋ถ€ํ„ฐ ํ•œ ๊ธ€์ž : ์ง์ˆ˜์ด๋ฉด (strLength - 1)๋ถ€ํ„ฐ ๋‘ ๊ธ€์ž
console.log(solution(str1))
console.log(solution(str2))

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   Math.floor( ) : ์ฃผ์–ด์ง„ ์ˆซ์ž์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ์ •์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. โ˜› ๋‚ด๋ฆผ
  `2   String.prototype.substr( ) : ์‹œ์ž‘ ์œ„์น˜๋ถ€ํ„ฐ ๊ธธ์ด๊ฐ’ ๋งŒํผ์˜ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. โ˜› ์›น ํ‘œ์ค€์—์„œ ์—†์–ด์กŒ์œผ๋‚˜ ์•„์ง ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

03. ๋‹ค๋ฅธ ํ’€์ด ์•Œ์•„๋ณด๊ธฐ

# 1) substr( )๊ณผ Math.ceil( ), ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ํ™œ์šฉํ•˜๊ธฐ

const str1 = "level";
const str2 = "updown";
function solution(s) {
return s.substr(Math.ceil(s.length / 2) - 1, s.length % 2 === 0 ? 2 : 1); // ์œ„์˜ ํ’€์ด๋ฅผ ํ•œ ๋ฌธ์žฅ์œผ๋กœ ํ•ฉ์ณ์„œ ๊ตฌ์„ฑํ•จ
} // ๊ฐ€์šด๋ฐ ๊ธ€์ž ์ฐพ๊ธฐ : index ๊ฐ’์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด '์˜ฌ๋ฆผ'
// ex) Math.floor(5 / 2) = 3 // Math.floor(6 / 2) = 3
console.log(solution(str1))
console.log(solution(str2))

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   Math.ceil( ) : ์ฃผ์–ด์ง„ ์ˆซ์ž๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆซ์ž ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋ฅผ integer ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. โ˜› ์˜ฌ๋ฆผ
  `2   ์กฐ๊ฑด(์‚ผํ•ญ) ์—ฐ์‚ฐ์ž : ์•ž์—์„œ๋ถ€ํ„ฐ ์กฐ๊ฑด๋ฌธ, ๋ฌผ์Œํ‘œ(?), ์กฐ๊ฑด๋ฌธ์ด ์ฐธ(truthy)์ผ ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ํ‘œํ˜„์‹, ์ฝœ๋ก (:), ์กฐ๊ฑด๋ฌธ์ด ๊ฑฐ์ง“(false)์ผ ๊ฒฝ์šฐ ์‹คํ–‰ํ•  ํ‘œํ˜„์‹์ด ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.

 

# 2) Math.floor( )์™€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•œ ๋‹ค๋ฅธ ํ’€์ด

const str1 = "level";
const str2 = "updown";
function solution(s) {
let center = Math.floor(s.length / 2);
return s.length % 2 === 1 ? s[center] : s[center-1] + s[center];
} // ํ™€์ˆ˜์ด๋ฉด s[center] ํ•œ ๊ธ€์ž : ์ง์ˆ˜์ด๋ฉด s[center-1]์™€ s[center] ๋‘ ๊ธ€์ž
console.log(solution(str1))
console.log(solution(str2))

 

# 3) ๋ฌธ์ž์—ด์˜ ๊ธธ์ด์™€ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž ํ™œ์šฉํ•˜๊ธฐ

const str1 = "level";
const str2 = "updown";
function solution(s) {
let x = ( s.length - 1 ) / 2; // ex) Math.floor(5 - 1) / 2 = 2 // Math.floor(6 - 1) / 2 = 2.5
return x % 1 ? ( s[ x - 0.5 ] + s[ x + 0.5 ]) : s[x];
} // ์ง์ˆ˜์ธ ๊ฒฝ์šฐ x % 1 = 0.5 โ˜› ๋‚˜๋จธ์ง€๊ฐ€ ์ƒ๊น€
// ๋‚˜๋จธ์ง€๊ฐ€ ์žˆ์œผ๋ฉด s[ x - 0.5 ]์™€ s[ x + 0.5 ] ๋‘ ๊ธ€์ž : ๋‚˜๋จธ์ง€๊ฐ€ ์—†์œผ๋ฉด s[x] ํ•œ ๊ธ€์ž
console.log(solution(str1))
console.log(solution(str2))

 

# 4) ํ™œ์šฉํ•˜๊ธฐ

const str1 = "level";
const str2 = "updown";
function solution(s) {
let answer = 0;
if ( s.length % 2 == 1){ // ํ™€์ˆ˜์ด๋ฉด,
answer = s.charAt( s.length / 2 ); // charAt( )์€ 0๊ณผ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด - 1 ์‚ฌ์ด์˜ ์ •์ˆ˜๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค๊ณ  ์ •์˜๋˜์–ด ์žˆ๋Š”๋ฐ, 2.5 ์˜ ๊ฐ’์„ ๋„ฃ์–ด๋„ ์ถœ๋ ฅ๋จ.
} else {
answer = s.substring( s.length / 2 -1, s.length / 2 +1 );
}
return answer;
}
console.log(solution(str1))
console.log(solution(str2))

   ___ ์ฐธ๊ณ ํ•˜๊ธฐ ___
  `1   String.prototype.charAt( ) : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ๋‹จ์ผ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  `2   String.prototype.substring( ) : string ๊ฐ์ฒด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ „๊นŒ์ง€ ๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€