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

模組化與 library

模組化與 library

Web開發學習筆記08 — prototype、__proto__、constructor、Prototype Chain(原型鏈)

Web開發學習筆記08 — prototype、__proto__、constructor、Prototype Chain(原型鏈)

一些不太好記卻很好用的 CSS 屬性

一些不太好記卻很好用的 CSS 屬性


Comments