Leetcode 5、14、20:最长回文子串-最长公共前缀-有效括号

自己的思路,仅供参考。
一。给你一个字符串 s,找到 s 中最长的回文子串。

/*给你一个字符串 s,找到 s 中最长的回文子串。*/

function longestPalindrome(s) {
  if (s.length === 1) {
    return s
  }

  if (isReverse(s)) {
    return s
  }

  let len = 0
  let newS = s
  let str = ''
  let sLength = s.length - 1

  let n = Math.floor(s.length / 2)  // 9/2=4.5
  let m = 0
  while (m <= n) {
    let ss = newS.slice(m, -m)
    if (isReverse(ss)) {
      if (ss.length > len) {
        len = ss.length
        str = ss
      }
    }
    m++
  }

  let i = 0
  while (i < sLength) {
    let ss = newS.slice(i)
    if (isReverse(ss)) {
      if (ss.length > len) {
        len = ss.length
        str = ss
      }
    }
    i++
  }

  let j = sLength
  while (j >= 0) {
    let ss = newS.slice(0, -j)
    if (isReverse(ss)) {
      if (ss.length > len) {
        len = ss.length
        str = ss
      }
    }
    j--
  }

  let h = 0
  let ss
  let u = sLength
  while (u >= 0) {
    for (h = 0; h < sLength; h++) {
      ss = newS.slice(h, -u)
      if (isReverse(ss)) {
        if (ss.length > len) {
          len = ss.length
          str = ss
        }
      }
    }
    u--
  }
  return str
}

function isReverse(s) {
  let i = Math.floor(s.length / 2)
  while (i >= 0) {
    if(s[i] != s[s.length - i-1]){
      return false
    }
    i--
  }
  return true
}

二。编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。

/*编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。*/
function longestCommonPrefix(strs) {
  if (strs.length === 1) {
    return strs[0]
  }
  let minL = ''
  for (let i = 0; i < strs.length; i++) {
    if (!minL) {
      minL = strs[i]
    } else {
      if (strs[i].length < minL.length) {
        minL = strs[i]
      }
    }
  }

  if(!minL) {
    return ''
  }

  let str = ''
  let n

  for (let i = 0; i < minL.length; i++) {
    for (let j = 0; j < strs.length; j++) {
      if(strs[j][i] == minL[i]) {
        n = true
      } else {
        n = false
        break
      }
    }

    if (n) {
      str+=minL[i]
    } else {
      return str
    }
  }

  return str
}

三。给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。

/*给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。*/
function isValid(s) {
  if (s.length % 2 !== 0) {
    return false
  }

  let stack = []
  let left1 = '{'
  let right1 = '}'
  let left2 = '('
  let right2 = ')'
  let left3 = '['
  let right3 = ']'

  for (let i = 0; i < s.length; i++) {
    if (!stack.length) {
      stack.push(s[i])
    } else {
      if (stack[stack.length - 1] === left1 && s[i] === right1 || stack[stack.length - 1] === left2 && s[i] === right2 || stack[stack.length - 1] === left3 && s[i] === right3) {
        stack.pop()
      } else {
        stack.push(s[i])
      }
    }
  }
  if (stack.length) {
    return false
  }
  return true
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值