silver-hands / sss

0 stars 0 forks source link

【Q083】面试题58 - II. 左旋转字符串 #83

Open fly0o0 opened 4 years ago

fly0o0 commented 4 years ago

面试题58 - II. 左旋转字符串

fly0o0 commented 4 years ago
/**
 * @param {string} s
 * @param {number} n
 * @return {string}
 */
var reverseLeftWords = function(s, n) {

  let res = ''
  for (let i = n; i < s.length; i++) {
    res += s.charAt(i)
  }
  for (let i = 0; i < n; i++) {
    res += s.charAt(i)
  }

  return res
};

/**
 * @param {string} s
 * @param {number} n
 * @return {string}
 */
var reverseLeftWords = function(s, n) {

  let res = ''
  for (let i = n; i < s.length + n; i++) {
    res += s.charAt(i % s.length)
  }

  return res
};