今日学习内容
● 344.反转字符串
var reverseString = function(s) {
// return s.reverse();
for(let i = 0 , j = s.length-1;i < Math.floor(s.length/2) ; i++,j--){
[s[i] , s[j]] = [s[j] , s[i]]
}
return s
};
● 541. 反转字符串II
var reverseStr = function(s, k) {
let count = 0 ;
s = s.split('');
for(let i = 0 ; i< s.length ; i+=2*k){
// count++;
// if(count === 2*k ){
let left = i ;
let right = i+k-1;
while(left <= right){
let temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--
}
// }
// count = 0;
// if(i == s.length-1){
// }
}
return s.join('')
};
● 卡码网:54.替换数字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function repleceNumber(s) {
s = s.split('');
// console.log(/^[a-zA-Z]*$/.test('9'))
s=s.map((item, index) => {
// return 90
// console.log(/[a-z]/.test(item))
if(/[a-z]/.test(item)){
return 'number'
}else{
return item
}
})
console.log(s)
return s.join('')
}
let result = repleceNumber('a1b2c3')
console.log(result)
</script>
</body>
</html>
● 151.翻转字符串里的单词
var reverseWords = function(s) {
let temp = [] ;
let arr = [];
let count = 0 ;
s = s.trim()
for(let i = 0 ; i <s.length;i++){
if(s[i] !== ' '){
if(!arr[count]) arr[count] = ''
arr[count] = arr[count]+s[i]
}else{
if(!arr[count]){
}else{
count++
}
}
}
console.log(arr)
return arr.reverse().join(' ')
};
● 卡码网:55.右旋转字符串
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function reserver(s,left,right){
}
function rightStr(s,n){
let left = 0 ;
let right = s.length-1;
s= Array.from(s)
while(left<=right){
[s[left],s[right]] = [s[right],s[left]];
left++;
right--
};
console.log(s)
left = 0 ;
right = n-1;
while(left<=right){
[s[left],s[right]] = [s[right],s[left]];
left++;
right--
};
left = n ;
right = s.length-1;
while(left<=right){
[s[left],s[right]] = [s[right],s[left]];
left++;
right--
};
return s.join('')
}
let s = 'abcdefg';
n = 2;
console.log(rightStr(s,n))
</script>
</body>
</html>
今日收获,记录一下自己的学习时长
标签:卡码,function,return,反转,let,字符串,left From: https://www.cnblogs.com/lijiacheng/p/17842292.html2h