[算法学习]开始leetcode之旅
在此记录一下用javascript刷leetcode的过程,每天都要坚持!
1.Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
Please note that your returned answers (both index1 and index2) are not zero-based.You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9Output: index1=1, index2=2
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var len = nums.length,
need,
map = {},
numbers = [];
for(var i = 0; i < len; i++){
need = target - nums[i];
if(need in map){
numbers.push(map[need] + 1);
numbers.push(i + 1);
break;
}else{
map[nums[i]] = i;
}
}
return numbers;
};
这道题目注意下复杂度别傻傻的用双循环...
20.Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var len = s.length,
cur,
stack = [],
map = {'(':')','[':']','{':'}'};
for(var i = 0; i < len; i++) {
cur = s[i];
if(map.hasOwnProperty(cur)){
stack.push(cur);
}else{
if(stack.length === 0){
return false;
}else{
stackTop = stack.pop();
if(map[stackTop] !== cur){
return false;
}
}
}
}
if(stack.length === 0){
return true;
}else{
return false;
}
};
这道题目是利用堆栈的,比较巧妙,数据结构很多记不得了,最近抽时间多翻翻。
189.Rotate Array
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function(nums, k) {
var len = nums.length;
if(len === 0 || len === 1 || k===0){
return;
}
var move = nums.splice(len - k,len);
for(var i = move.length - 1;i >= 0;i--){
nums.unshift(move[i]);
}
};
这题用js写的话就是考察下数组操作方法了,注意下特殊情况即可。
[算法学习]开始leetcode之旅的更多相关文章
- Manacher算法学习笔记 | LeetCode#5
Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...
- DSP算法学习-过采样技术
DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...
- 算法学习之C语言基础
算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...
- Python之路,Day21 - 常用算法学习
Python之路,Day21 - 常用算法学习 本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...
- C / C++算法学习笔记(8)-SHELL排序
原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- 算法学习之BFS、DFS入门
算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...
- 二次剩余Cipolla算法学习笔记
对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...
随机推荐
- NODE升级到V12.X.X
Node.js 是一个基于Chrome JavaScript运行时的平台,可轻松构建快速,可扩展的网络应用程序.最新版本 node.js yum存储库 由其官方网站维护.使用本教程添加yum存储库,并 ...
- Python3-unittest测试框架之DDT数据驱动
unittest测试框架之DDT数据驱动 ddt的使用 DDT数据驱动 DDT:Data Driver Test(数据驱动测试) 数据驱动思想:数据和用例进行分离,通过外部数据去生成测试用例 安装 p ...
- 【leetcode】1041. Robot Bounded In Circle
题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can recei ...
- java.nio.channels.IllegalBlockingModeException
报错信息如下: Exception in thread "main" java.nio.channels.IllegalBlockingModeException at java. ...
- win server2008关闭危险端口445,135,137,138,139的方法
在Windows server 2008系统上,有两种途经可以禁用本地端口: 1.通过Windows防火墙(比较简单,设置方便) 2.通过IP安全策略(比较复杂,功能强大,不依赖防火墙) 一.通过Wi ...
- 文件的上传与下载实现(react、express,create-react-app脚手架)
项目结构为前后端分离,中间布了一层node. 文件上传 要求:将文件信息等发送到后台. html代码 <input type="file" name="file&q ...
- 多行文本溢出隐藏处理,兼容ie,火狐
问题 多行文本溢出隐藏,webkit内核浏览器如谷歌支持如下写法: overflow: hidden; text-overflow: ellipsis; display: -webkit-box; - ...
- 麦子lavarel---16、日志
麦子lavarel---16.日志 一.总结 一句话总结: 一定要养成打印日志查看日志的好习惯,非常节约时间和便于查错 1.console.Log(“获取类别数据:"):console.Lo ...
- matlab: undocumented sprintfc
今天本想找一个类似于 R 中 paste 的 matlab 函数, 结果在 stackoverflow 上找到一个叫 sprintfc 的函数 (http://stackoverflow.com/qu ...
- Webhook到底是个啥?
参照: https://segmentfault.com/a/1190000015437514