647. 回文子串
leetcode-cn.com/problems/pa…
var countSubstrings = function (s) {
let count = 0
for (let start = 0;start<s.length;start++) {
// 分类思想 转换为偶数个和奇数个 大问题转子问题
// 偶数
calcExpandCount(start,start+1)
// 奇数
calcExpandCount(start,start)
}
function calcExpandCount(start,end){
while(start>=0&&end<s.length&&s[start]===s[end]){
count++
start
end++
}
}
return count
};
/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function (s) {
const len = s.length
const dp = []
for (let i = 0
dp[i] = []
}
let count = 0
for (let start = s.length-1
for (let end = start
if (start === end) {
dp[start][end] = true
count ++
continue
}
if (s[start] === s[end]) {
if (start + 1 === end) {
dp[start][end] = true
count++
} else {
dp[start][end] = dp[start + 1][end - 1]
if(dp[start][end]){
count ++
}
}
} else {
dp[start][end] = false
}
}
}
return count
}
350. 两个数组的交集 II
leetcode-cn.com/problems/in…
var intersect = function(nums1, nums2) {
nums1.sort((a,b)=>a-b)
nums2.sort((a,b)=>a-b)
const res = []
for(let i=0,j=0;i<nums1.length&&j<nums2.length;){
if(nums1[i] === nums2[j]){
res.push(nums1[i])
i++
j++
}else{
if(nums1[i]<nums2[j]){
i++
}else{
j++
}
}
}
return res
};
9. 回文数
leetcode-cn.com/problems/pa…
- 取得一个整数的每一位的数字 这个数对10取余然后再除以10
var isPalindrome = function(x) {
if(x<0){
return false
}
if(x<10){
return true
}
const orginTraget = x
const res = []
let reverseTarget = null
while(x>=1){
res.push(x%10)
x = Math.floor(x/10)
}
for(let i=0,j=res.length-1;i<res.length;i++,j--){
reverseTarget += res[i]*Math.pow(10,j)
}
return orginTraget === reverseTarget
};
14. 最长公共前缀
leetcode-cn.com/problems/lo…
/**
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
if(!strs.length){
return ""
}
let flag = strs[0]
for(let i=1
while(flag.length &&strs[i].indexOf(flag)!==0){
flag = flag.slice(0,flag.length-1)
}
}
return flag
}