zoukankan      html  css  js  c++  java
  • [算法学习]开始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写的话就是考察下数组操作方法了,注意下特殊情况即可。

  • 相关阅读:
    iOS 新建xib文件时,最外层view的约束问题
    React native 无法弹出调试控件的问题
    从GitHub下载demo时遇到的依赖问题
    Mac 解决 Sourcetree 同步代码总需要密码的问题
    Mac 安装JRE 1.8
    正则表达式-- (.*?) 或 (.*+)
    字符串内有多个#号,每俩#号为一组,JavaScript 截取每组#号之间的字符
    Js/jQuery实时监听input输入框值变化
    Redis设置密码
    redis本机能访问 远程不能访问的问题
  • 原文地址:https://www.cnblogs.com/lijie33402/p/4824589.html
Copyright © 2011-2022 走看看