[#006] 344. Reverse String


Posted by s1101192101 on 2021-05-14

  • link:
    leetcode

  • 解題思路:

    1. 對第一個字元與最後一個字元做 swap,對第二個字元與倒數第二個字元做 swap ... (依此類推)

  • 程式碼:

/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */
var reverseString = function(s) {
  let t = ''
  for (var i = 0; i < s.length / 2; i++) {
    t = s[i]
    s[i] = s[s.length - 1 - i]
    s[s.length - 1 - i] = t
  }
}


  • 結果:



#Leetcode #string #easy







Related Posts

Props  觀念與使用

Props 觀念與使用

W16_JavaScript 核心與物件導向_學習筆記整理

W16_JavaScript 核心與物件導向_學習筆記整理

大公司 vs 小公司

大公司 vs 小公司


Comments