在此记录一下用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之旅的更多相关文章

  1. Manacher算法学习笔记 | LeetCode#5

    Manacher算法学习笔记 DECLARATION 引用来源:https://www.cnblogs.com/grandyang/p/4475985.html CONTENT 用途:寻找一个字符串的 ...

  2. DSP算法学习-过采样技术

    DSP算法学习-过采样技术 彭会锋 2015-04-27 23:23:47 参考论文: 1 http://wr.lib.tsinghua.edu.cn/sites/default/files/1207 ...

  3. 算法学习之C语言基础

    算法学习,先熟悉一下C语言哈!!! #include <conio.h> #include<stdio.h> int main(){ printf(+); getch(); ; ...

  4. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  5. C / C++算法学习笔记(8)-SHELL排序

    原始地址:C / C++算法学习笔记(8)-SHELL排序 基本思想 先取一个小于n的整数d1作为第一个增量(gap),把文件的全部记录分成d1个组.所有距离为dl的倍数的记录放在同一个组中.先在各组 ...

  6. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  7. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  8. 算法学习之BFS、DFS入门

    算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...

  9. 二次剩余Cipolla算法学习笔记

    对于同余式 \[x^2 \equiv n \pmod p\] 若对于给定的\(n, P\),存在\(x\)满足上面的式子,则乘\(n\)在模\(p\)意义下是二次剩余,否则为非二次剩余 我们需要计算的 ...

随机推荐

  1. LeetCode--046--全排列(java)

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1 ...

  2. maven编译问题之 -The POM for XXX is invalid, transitive dependencies (if any) will not be available

    问题一: 把父工程tao-parent install 到maven本地仓后,接着install tao-common工程,然后报错 报错信息如下: [WARNING] The POM for com ...

  3. win7 开机,或重启自动启动 该文件下的

    win7 开机,或重启自动启动 该文件下的: 把桌面上快捷键放入文件内就行 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start ...

  4. 树状数组(一维&二维函数)

    //一维 void add(int x,int p) { while(x<=n)t[x]+=p,x+=(x&(-x)); } int query(int x) { int ans=0; ...

  5. [CSP-S模拟测试96]题解

    以后不能再借没改完题的理由不写题解了…… A.求和 求$\sum \sum i+j-1$ 柿子就不化了吧……这年头pj都不考这么弱智的公式化简了…… 坑点1:模数不定,可能没有2的逆元,那么只要先把乘 ...

  6. String 与StringBuffer习题

    1: 画出如下几行代码的结构 // 画出如下几行代码的结构 String s1 = "hello"; // value存储在常量池内 String s2 = "hello ...

  7. 慎用create table as select,一定要注意默认值的问题

    再做一些数据迁移时候,很多人会使用create table  as select * from table where id=-1的方式来年建立一摸一样的表,但是这样做有个很大的弊端,不能将原表中的d ...

  8. DELPHI之全局变量和局部变量

    http://www.cnblogs.com/Stwo/archive/2011/07/11/2102816.html DELPHI之全局变量和局部变量 全局变量: 如果我们在应用程序一个单元中的in ...

  9. hash-散列笔记

    散列基础与整数散列 散列(hash哈希)的基本思想--"将元素通过一个函数转换为整数,使该整数可以尽量唯一地代表这个元素".其中把这个转换函数称为散列函数H,元素在转换前为key, ...

  10. EasyUI的datagrid列属性添加超链接

    $("#dg").datagrid({        url: "../Ajax/PurchaseAjax.ashx",        queryParams: ...