Leetcode部分题目整理(Javascript)
3.无重复字符的最长子串
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
var ans = [], vis = [], max = 0;
for(var i = 0; i < 256; i++){
vis.push(-1);
}
for(var i = 0; i < s.length; i++){
var t = s[i].charCodeAt() - 0;
if(vis[t] == -1){
vis[t] = i;
ans.push(s[i]);
if(ans.length > max){
max = ans.length;
}
}
else{
var pos = ans.indexOf(s[i]);
for(var j = pos - 1; j >= 0; j--){
vis[ans[j].charCodeAt() - 0] = -1;
}
ans = ans.slice(pos + 1);
ans.push(s[i]);
vis[t] = i;
}
}
return max;
};
4. 寻找两个有序数组的中位数
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var findMedianSortedArrays = function(nums1, nums2) {
if(nums1.length > nums2.length){
var t = nums1;
nums1 = nums2;
nums2 = t;
}
var len = nums1.length + nums2.length;
function find(k,nums1,nums2){
var ans = null;
while(ans === null){
if(k % 2 == 1){
x = Math.floor(k / 2);
if(x == 0)
x = 1;
}else{
x = k / 2;
}
var flag = false;
var temp = [];
if(nums1.length == 0 && nums2.length != 0){
temp = nums2.slice(0,k);
k = 0;
}else if(nums2.length == 0){
temp = nums1.slice(0,k);
k = 0;
}else if(nums1.length < x){
if(nums1[nums1.length - 1] < nums2[x - 1]){
k -= nums1.length;
temp = nums1.slice();
nums1 = [];
}else{
k -= x;
temp = nums2.slice(0,x);
nums2 = nums2.slice(x);
}
}else{
k -= x;
if(nums1[x - 1] < nums2[x - 1]){
temp = nums1.slice(0,x);
nums1 = nums1.slice(x);
}else{
temp = nums2.slice(0,x);
nums2 = nums2.slice(x);
}
}
if(!k){
ans = temp[temp.length - 1];
}
}
return ans;
}
if(len % 2){
return find((len + 1) / 2,nums1,nums2);
}else{
return (function(){
var x1 = nums1.slice(),x2 = nums2.slice();
var sum = find(len / 2,nums1,nums2);
nums1 = x1;
nums2 = x2;
sum += find((len / 2 + 1),nums1,nums2);
return sum / 2;
})();
}
};
Leetcode部分题目整理(Javascript)的更多相关文章
- Noip往年题目整理
Noip往年题目整理 张炳琪 一.历年题目 按时间倒序排序 年份 T1知识点 T2知识点 T3知识点 得分 总体 2016day1 模拟 Lca,树上差分 期望dp 144 挺难的一套题目,偏思维难度 ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- LeetCode SQL题目(第一弹)
LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...
- LeetCode题库整理(自学整理)
1. Two Sum 两数之和 来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...
- NOIp初赛题目整理
NOIp初赛题目整理 这个 blog 用来整理扶苏准备第一轮 csp 时所做的与 csp 没 有 关 系 的历年 noip-J/S 初赛题目,记录了一些我从不知道的细碎知识点,还有一些憨憨题目,不定期 ...
- leetcode二叉树题目总结
leetcode二叉树题目总结 题目链接:https://leetcode-cn.com/leetbook/detail/data-structure-binary-tree/ 前序遍历(NLR) p ...
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
随机推荐
- Numpy学习之——数组创建
Numpy学习之--数组创建 过程展示 import numpy as np a = np.array([2,3,9]) a array([2, 3, 9]) a.dtype dtype('int32 ...
- Django Rest Framework 安装
1. 环境要求 Python (3.5, 3.6, 3.7): 查看 python版本:python -V Django (1.11, 2.0, 2.1, 2.2) 查看django版本:pip li ...
- Istio技术与实践01: 源码解析之Pilot多云平台服务发现机制
服务模型 首先,Istio作为一个(微)服务治理的平台,和其他的微服务模型一样也提供了Service,ServiceInstance这样抽象服务模型.如Service的定义中所表达的,一个服务有一个全 ...
- APP安全测试之安装/卸载/更新测试
在app测试中,有个不可忽视的测试方向,就是安装.卸载和更新,有很多人问到了这个问题,我就在这里做了一个总结,有补充的请留言哦 安装 1.正常安装测试,检查是否安装成功. 2.APP版本覆盖测试.例如 ...
- Promise.all的使用
我有一个需求如下: 有一个学生号的数组,根据这个数组,取出相关学生的所有信息.获取学生信息的接口用setTime模拟,假设每次请求需要耗时2s,代码如下 // 获取学生信息接口 function ge ...
- react 管理平台
https://open.vbill.cn/react-admin/ 开源中国:https://gitee.com/sxfad/react-admin.git GitHub:https://githu ...
- VS219 没有.net core 3.0模板
控制台命令 dotnet --info dotnet --version 都正常显示有安装3.0 需要升级VS 2019 16.3.3,本地版本为16.3.2
- Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26
Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/TH ...
- python滴啊用caffe时的小坑
在使用caffe的python接口时, 如下,如果标黄的部分不加上的话,两次调用该函数,后面的会将前面的返回值覆盖掉,也就是fea1与fea2相等,但是fea1_ori会保留原来的fea1 解决方法为 ...
- 4.caffe:train_val.prototxt、 solver.prototxt 、 deploy.prototxt( 创建模型与编写配置文件)
一,train_val.prototxt name: "CIFAR10_quick" layer { name: "cifar" type: "Dat ...