### 1、字符串模式匹配
>[warning] Write a function called that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return `true` if the string is valid, and `false` if it's invalid.
**Examples:**
```js
"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true
```
**Constraints:**
```js
0 <= input.length <= 100
```
>[warning] You may assume that the input string will only contain opening and closing parenthesis and will not be an empty string.
我的代码:
```js
//不好意思,又见递归
//递归将匹配到的(),替换掉,直到匹配不到,则false,为空则true
function validParentheses(parens){
parens = parens.replace(/\(\)/g,'').trim();
if(!parens){
return true;
}
let match = parens.match(/\(\)/g);
if(null != match && match.length>0){
return validParentheses(parens);
}else{
return false;
}
}
```
<div class="cline"></div>
```js
function validParentheses(parens){
let tokenizer=/[()]/g,token,count=0;
while(token = tokenizer.exec(parens),null!=token){
if(token == '('){
count++;
}else if(token==')'){
count--;
if(count<0){
return false;
}
}
}
return count == 0;
}
```
关于`exec`,记录一笔:
>[success] `RegExpObject.exec(string)`
[参考](http://www.w3school.com.cn/jsref/jsref_exec_regexp.asp)
<div class="cline"></div>
```js
function validParentheses(parens){
var indent = 0;
for (var i = 0 ; i < parens.length && indent >= 0; i++) {
indent += (parens[i] == '(') ? 1 : -1;
}
return (indent == 0);
}
```
### 2、字符串替换
>[warning] Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
**Examples:**
```js
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldWay !
```
我的解法:
```js
function pigIt(str){
return str.split(' ').map(item=>item.substring(1,item.length)+item.substr(0,1)+'ay').join(' ')
}
```
主要还是通过截取字符串,达到拼接的目的,这里有几点值得注意下:
* `substr`与`substring`的区别,前者返回一个从指定位置开始的指定长度的子字符串;后者是获取区间内的字符串。
* 字符串`item`可通过`item.charAt(0)`或者`item[0]`来获取首字母;
* 字符串的截取同样可以沿用数组的方法`slice`,跟字符串的截取方法差不多;
优化版本:
```js
function pigIt(str){
return str.split(' ').map(item=>item.slice(1)+item[0]+'ay').join(' ')
}
```
<div class="cline"></div>
之前使用的字符串模板替换,在这里同样可用,沿用并巩固下:
```js
//简洁的很漂亮
function pigIt(str){
return str.replace(/(\w)(\w+)/g,'$2$1ay')
}
```
### 3、
>[warning] Find the missing letter
Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.
You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2.
The array will always contain letters in only one case.
**Example:**
```js
['a','b','c','d','f'] -> 'e'
['O','Q','R','S'] -> 'P'
```
>[warning] (Use the English alphabet with 26 letters!)
Have fun coding it and please don't forget to vote and rank this kata! :-)
I have also created other katas. Take a look if you enjoyed this kata!
我的解法:
```js
function findMissingLetter(array)
{
let result = '';
array.reduce((r,item)=>{
let num = item.charCodeAt();
if(r === 0){
r = num;
}
if(Math.abs(num-r) >1){
result = String.fromCharCode(((num+r)/2).toString());
}
return num;
},0);
return result;
}
```
- 前端
- C1-Javascript
- H5图片分块和断点续传
- JavascriptPatterns[Stoyanstefanov]
- macotask和microtask
- 前端代码生成器
- 跨域
- 页面回到顶部滚动按钮实现
- C2-CSS
- 浏览器的一些单位
- 盒模型
- 移动端判断横竖屏
- C3-框架
- ReactNative
- 开发环境搭建(安卓篇)
- Vue
- vue+pdfjs使用
- vue+typescript使用实践
- vue+webpack3.x集成typescript
- Vue源码3
- vue源码分析1
- vue源码分析2
- vue笔记
- C4-工具
- git
- Gitlab-CICD
- mock规则
- vscode-settings
- webpack自定义命令,切换代理地址
- 正则表达式
- 深入浅出webpack
- C5-Node
- express
- express源码阅读
- nightmare使用指南
- 爬虫1.0
- C6-微信
- 微信
- C7-Canvas
- 基础API
- 前端随笔笔记
- 后端
- C1-Java
- shiro
- C2-Linux
- ffmpeg
- ITerm
- Linux
- MongoDB安装
- MySql安装
- Ngnix反向代理
- 常见错误
- 备忘
- mac
- 备忘-Work
- 备忘Link
- 服务器资源
- 教程
- Hexo个人博客搭建笔录
- 文档
- CSS编码规范
- 前端编码规范
- 随笔
- 整理
- 正则
- 链接收藏
- 面试
- CodeWars题库
- CodeWars题库(二)
- Java社招面试题
- Java面试
- Web面试
- 前端笔试题
- 笔试题