两数之和
// 方法1:暴力法
var twoSum = function(nums, target){
var arr = [];
for(var i=0;i<nums.length;i++){
for(var j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
arr=[i,j];
return arr;
}
}
}
}
//方法2:利用map,边存边取
var twoSum = function(nums, target){
const map = new Map();
for(let i = 0; i< nums.length; i++){
const j = map.get(target-nums[i]);//取
if(j!==undefined){
console.log(j,i);
return [j,i];
}
map.set(nums[i],i);//存 map.set(key,value)
}
}
twoSum([2,7,11,15],9) //0,1
两数相加
var addTwoNumbers = function(l1,l2){
let result = new ListNode(null);
let nextRst = result;
//进位
let params = 0;//传入下一个层级的值
let val = 0;//传给当前层级的值
while(l1 != null || l2 != null){
//TODO 遍历子链表
let x = (l1 != null) ? l1.val : 0; //l1.val返回l1链表的第一个值
let y = (l2 != null) ? l2.val : 0;//l2.val返回l1链表的第一个值
val = (x+y+params) % 10;
params = Math.floor((x+y+params)/10);
nextRst.next = new ListNode(val); //创建新子链赋值,因为倒序,所以赋值从后往前
nextRst = nextRst.next; //指针指向下一个,进行下一次遍历
if(l1 != null) l1 = l1.next;
if(l2 != null) l2 = l2.next;
}
if(params){
nextRst.next = new ListNode(params);//最后一次子链相加,如果有进位,创建新子链赋值
}
console.log(result);
return result.next;
}
addTwoNumbers([1,2,3],[2,3,4]);
无重复字符的最长子串
var lengthOfLongestSubstring = function(s){
let num =0; let res = 0; let m='';
for(n of s){
if(m.indexOf(n)==-1){
m+=n;
num++;
res = res<num?num:res;
}else{
m+=n;
m=m.slice(m.indexOf(n)+1);//得到长度,方法可以多种
num=m.length;
}}
return res;
}
寻找两个有序数组的中位数
var findMedianSortedArrays = function(nums1,nums2){
const arr = [...nums1,...nums2].sort((a,b)=>a-b);
const { length } = arr; //获取arr长度
return length%2?arr[Math.floor(length/2)]:(arr[(length/2)]+arr[length/2-1])/2;
}
最长回文子串
var longestPalindrome = function(s){
let n = s.length;
let res = '';
let dp = Array.from(new Array(n),()=>new Array(n).fill(0));
for(let i = n-1;i>=0;i--){
for(let j=i;j<n;j++){
dp[i][j]=s[i]==s[j]&&(j-i<2||dp[i+1][j-1]);
if(dp[i][j]&&j-i+1>res.length){
res = s.substring(i,j+1);
}
}
}
return res;
}
整数反转
var reverse = function(x){
let fh = "",re;
if(x<0){
fh="-";
x=0-x;
}
re = (x+"").split("").reverse().join("");//转换成字符串
if(re.length>10||re.length==10&&re>(x<0?"2147483647":"2147483647")){
return 0;
}else{
return fh + re;
}
}
字符串转换整数
var myAtoi = function(str){
str = str.trim();
if(!/^[+|-]?\d+/.test(str)) return 0; //正则判断,不是整数返回0
let val = parsetInt(str.match(/^[+|-]?\d+/));
let base = Math.pow(2,31);
let min = -base;
let max = base-1;
return Math.max(Math.min(val,max),min) //比较得出不超出范围的整数,若超出范围,则取范围最大值
}
回文数
var isPalindrom = function(x){
if(x<0) return false;
let flag = true;
x=x.toString();
for(let i=0, len = x.length; i<len/2;i++){
if(x[i]!==x[len-1-i]){
flag = false;
break;
}
}
console.log(flag)
return flag;
}
isPalindrom(121)
数组中重复的数字
var findRepeatNumber = function(nums) {
let s = new Set();
for(var i in nums){
var Len = s.size;
s.add(nums[i]);
if(s.size==Len)
return nums[i]
}
}
二维数组的查找
var findNumberIn2DArray = function(matrix, target) {
let flag = false;
for(let i = matrix.length;i>0;i--){
if(matrix[i-1][0]<=target){
if(matrix[i-1].includes(target)){
flag = true;
i = -1;
}
}
}
return flag;
};
代替空格
var replaceSpace = function(s) {
return s.replace(/ /g, "%20");
};
从头到尾打印链表
var reversePrint = function(head) {
if(head===null) return []
const arr = []
while(head){
arr.push(head.val);
head = head.next;
}
return arr.reverse()
};
重建二叉树
var buildTree = function(preorder, inorder) {
if(!preorder.length || !inorder.length) {
return null;
}
let root = preorder[0];
let node = new TreeNode(root);
let i = 0;
for(;i<inorder.length;i++){
if(inorder[i] === root) {
break;
}
}
node.left = buildTree(preorder.slice(1,i+1), inorder.slice(0,i));
node.right = buildTree(preorder.slice(i+1), inorder.slice(i+1));
return node;
};
子串模糊查询
正则
var match= function(str,key){
let str = readline();
let key = readline();
let re = key.split("?");
re = "^"+re.join(".{1,3}?");
re = RegExp(re);
const res = re.exec(str);
console.log(res?res[0].length:-1)
}
集合便利(选球)
var N = 3
var num = [2,2,3]
var ans = newArray(15).fill(0)
function xuanQiu(i, n){
if(n === 0){
console.log(ans.slice(0,K))
return
}else if( n < 0 || K === i){
return
}
for(var j = 0; j <= num[i]; j++){
ans[i] = j
xuanQiu(i+1, n-j)
}
ans[i] = 0
}
xuanQiu(K, N)
网友评论