### 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面試
- 前端筆試題
- 筆試題