๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/leetcode

LeetCode 344 - Reverse String (Easy)

by HandHand 2021. 4. 5.

๋ฌธ์ œ

LeetCode - 344๋ฒˆ

ํ’€์ด ๊ณผ์ •

in place ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด์„ ๋’ค์ง‘๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์™ผ์ชฝ ๋ ๊ณผ ์˜ค๋ฅธ์ชฝ ๋ ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋’ค์ง‘๊ธฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ

/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */
var reverseString = function (s) {
  let lo = 0;
  let hi = s.length - 1;

  while (lo < hi) {
    [s[hi], s[lo]] = [s[lo], s[hi]];

    lo += 1;
    hi -= 1;
  }

  return s;
};
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€