Tiven

Tiven

博观而约取,厚积而薄发

天问的个人网站(天问博客),专注于Node.js、Vue.js、React、Vite、Npm、Nginx等大前端技术。不断学习新技术,记录日常开发问题,持续分享coding,极客开源,共同进步。生命不息,奋斗不止... [ hexo blog ]

【数据结构与算法】(18):字母大小写切换


经典算法题:输入一个字符串,切换其中字母的大小写,下面将分别使用 正则匹配ASCII编码 判断的形式去实现。

数据结构与算法 · 字母大小写切换

一、问题示例

示例 1:

  • 输入:A23#$5bcF
  • 输出:a23#$5BCf

示例 2:

  • 输入:你好啊%……&001
  • 输出:你好啊%……&001

二、代码演示

  1. 正则匹配
// 切换字母大小写 正则

export function switchLetterCase1(s: string): string {
  let res = ''

  const len = s.length
  if (len ===0) return res
  const reg1 = /[a-z]/
  const reg2 = /[A-Z]/

  for (let i = 0; i < len; i++) {
    const c = s[i]
    if (reg1.test(c)) {
      res += c.toUpperCase()
    } else if (reg2.test(c)) {
      res += c.toLowerCase()
    } else {
      res += c
    }
  }

  return res
}
  1. ASCII编码
// 切换字母大小写 ASCII编码

export function switchLetterCase2(s: string): string {
  let res = ''

  const len = s.length
  if (len === 0) return res

  for (let i = 0; i < len; i++) {
    const c = s[i]
    const code = c.charCodeAt(0)

    if (code >= 65 && code <= 90) {
      res += c.toLowerCase()
    } else if (code >= 97 && code <= 122) {
      res += c.toUpperCase()
    } else {
      res += c
    }
  }

  return res
}

三、单元测试

// 切换字母大小写

import { switchLetterCase1, switchLetterCase2 } from '../switch-letter-case.ts'

describe('切换字母大小写 正则匹配', () => {
  it('正常情况', () => {
    const s = '12A54btr34Agh89'
    const res = switchLetterCase1(s)
    expect(res).toBe('12a54BTR34aGH89')
  })
  it('空字符串', () => {
    const res = switchLetterCase1('')
    expect(res).toBe('')
  })
  it('非字母', () => {
    const res = switchLetterCase1('你好啊%……&001')
    expect(res).toBe('你好啊%……&001')
  })
})

describe('切换字母大小写 charCode编码', () => {
  it('正常情况', () => {
    const s = '12A54btr34Agh89'
    const res = switchLetterCase2(s)
    expect(res).toBe('12a54BTR34aGH89')
  })
  it('空字符串', () => {
    const res = switchLetterCase2('')
    expect(res).toBe('')
  })
  it('非字母', () => {
    const res = switchLetterCase2('你好啊%……&001')
    expect(res).toBe('你好啊%……&001')
  })
})

四、性能测试

const s = '12A54$%^&9*btr34Agh8912A54$%^&9*btr34Agh8912A54$%^&9*btr34Agh8912A54$%^&9*btr34Agh89'

console.time('switchLetterCase1')
for (let i = 0; i < 10 * 10000; i++) {
  switchLetterCase1(s)
}
console.timeEnd('switchLetterCase1')

console.time('switchLetterCase2')
for (let i = 0; i < 10 * 10000; i++) {
  switchLetterCase2(s)
}
console.timeEnd('switchLetterCase2')

<div> <button style='padding: 10px 20px; color: #00b1fb;' class='rotate-btn' onclick='run()'>运行</button> <br> <b>switchLetterCase1 run time:</b> <span style='color: red;' class='box1-ms'>0</span> <hr> <b>switchLetterCase2 run time:</b> <span style='color: red;' class='box2-ms'>0</span> <hr> </div> <script> // 切换字母大小写 正则

function switchLetterCase1(s) { let res = ''

const len = s.length
if (len ===0) return res
const reg1 = /[a-z]/
const reg2 = /[A-Z]/

for (let i = 0; i < len; i++) {
  const c = s[i]
  if (reg1.test(c)) {
    res += c.toUpperCase()
  } else if (reg2.test(c)) {
    res += c.toLowerCase()
  } else {
    res += c
  }
}

return res

}

// 切换字母大小写 ASCII编码 function switchLetterCase2(s) { let res = ''

const len = s.length
if (len === 0) return res

for (let i = 0; i < len; i++) {
  const c = s[i]
  const code = c.charCodeAt(0)

  if (code >= 65 && code <= 90) {
    res += c.toLowerCase()
  } else if (code >= 97 && code <= 122) {
    res += c.toUpperCase()
  } else {
    res += c
  }
}

return res

}

function run() { const s = '12A54$%^&9btr34Agh8912A54$%^&9btr34Agh8912A54$%^&9btr34Agh8912A54$%^&9btr34Agh89'

let s1 = performance.now()
for (let i = 0; i < 10 * 10000; i++) {
  switchLetterCase1(s)
}
document.querySelector('.box1-ms').innerText = performance.now() - s1 + ' ms'

let s2 = performance.now()
for (let i = 0; i < 10 * 10000; i++) {
  switchLetterCase2(s)
}
document.querySelector('.box2-ms').innerText = performance.now() - s2 + ' ms'

} </script>

  • 使用正则表达式,性能较差
  • 使用ASCII码判断,性能较好(推荐)

欢迎访问:天问博客