[#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

【Day00】前言

【Day00】前言

Inheritance

Inheritance

安裝 Git/ Git 的基本指令

安裝 Git/ Git 的基本指令


Comments