leetcode

用JavaScript刷LeetCode的正确姿势

爷,独闯天下 提交于 2020-01-25 18:09:52
虽然很多人都觉得前端算法弱,但其实 JavaScript 也可以刷题啊!最近两个月断断续续刷完了 leetcode 前 200 的 middle + hard ,总结了一些刷题常用的模板代码。 走过路过发现 bug 请指出,拯救一个辣鸡(但很帅)的少年就靠您啦! 常用函数 包括打印函数和一些数学函数。 const _max = Math.max.bind(Math); const _min = Math.min.bind(Math); const _pow = Math.pow.bind(Math); const _floor = Math.floor.bind(Math); const _round = Math.round.bind(Math); const _ceil = Math.ceil.bind(Math); const log = console.log.bind(console); //const log = _ => {} log 在提交的代码中当然是用不到的,不过在调试时十分有用。但是当代码里面加了很多 log 的时候,提交时还需要一个个注释掉就相当麻烦了,只要将 log 赋值为空函数就可以了。 举一个简单的例子,下面的代码是可以直接提交的。 // 计算 1+2+...+n // const log = console.log.bind(console);

LeetCode 26. Remove Duplicates from Sorted Array

走远了吗. 提交于 2020-01-25 16:56:37
问题链接 LeetCode 26. Remove Duplicates from Sorted Array 题目解析 给定有序数组,删除其中重复元素,返回新数组长度。 解题思路 由于不是很懂题目的意思,题目要求不要分配额外的数组空间,其实是想说保证空间复杂度为 \(O(1)\) 。 简单题。了解一下, std::unique 。unique函数要求有序数组,需要注意的是unique函数并不是真正地把元素删除,而是将重复的元素移动到最后了,该函数返回最后一个非重复元素的下一位置。所以,使用unique函数后,再用erase函数删除元素。 参考代码 class Solution { public: int removeDuplicates(vector<int>& nums) { nums.erase(unique(nums.begin(), nums.end()), nums.end()); return nums.size(); } }; 快慢指针 使用快慢指针(cur & pre)来记录遍历的坐标,初始两个指针都指向第一个数字,如果两个指针指的数字相同,则快指针+1;如果不同,则两个指针都+1。当快指针到达数组尾部,慢指针当前的坐标加1就是数组中不同数字的个数。参考代码如下: class Solution { public: int removeDuplicates(vector

快速上手leetcode动态规划题

我的梦境 提交于 2020-01-25 16:18:59
快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固。 我leetcode从动态规划开始刷,语言用的java。 一.了解动态规划 我上网查了一下动态规划,了解到动态规划是“ 带有备忘录的递归 ”, 而大多数用来理解动态规划的例子都是 斐波那契数列 ,就是那个经典的递归式 f(i)=f(i-1)+f(i-2) ,f(1)=f(2)=1 那么我们就可以得到很多式子,比如求f(5): f(5)=f(4)+f(3); f(4)=f(3)+f(2); f(3)=f(2)+f(1); 然后我们就发现了重复的部分,在求f(5)和f(4)的时候都要求f(3),那么它们都要做一次f(3)的递归操作,来得到f(3)的值。 我想这是很不值得的,没必要同样的操作执行两遍。并且我们知道当f(n)的n比较大时,是很多重复的部分的,这也就意味着有很大的优化空间。 因此有了所谓的“ 备忘录 ”,也就是用一个 数组 来记录每个状态的结果,比如f(5)就是n为5时f(n)的状态。 这样的话,我们就可以在求f(n)的时候,先查看一下数组中是否记录了这一个状态的值,如果有,就直接从数组中拿,如果没有,就递归计算一下,再把这个值放到数组中去。这也是所谓的“ 以空间换时间 ”的思想。 int[] dp=new int[n+1];//dp[i]表示f(i)的值 在求f(x)时: if

LeetCode 11 - Container With Most Water

荒凉一梦 提交于 2020-01-25 10:29:21
一、问题描述 D e s c r i p t i o n : Given n non-negative integers a 1 , a 2 , . . . , a n , where each represents a point at coordinate ( i , a i ) . n vertical lines are drawn such that the two endpoints of line i is at ( i , a i ) and ( i , 0 ) . Find two lines, which together with x-axis forms a container, such that the container contains the most water. N o t e : You may not slant the container. 给定 n 个非负整数 a 1 , a 2 , . . . , a n ,构造 n 条垂直于x-轴的直线,每条直线都是从起点 ( i , 0 ) 到终点 ( i , a i ) 。 任选两条直线,都可以与x-轴一起构成一个容器,求这些容器中能装的最大的水量。 假定容器不可以倾斜。 二、解题报告 1. 解法一: O ( n 2 ) 直接遍历,枚举容器的两个边界,记录最大值: class Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

本秂侑毒 提交于 2020-01-25 05:43:56
Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2,4] . Note: Recursive solution is trivial, could you do it iteratively? 这道题让我们求N叉树的前序遍历,有之前那道 Binary Tree Preorder Traversal 的基础,知道了二叉树的前序遍历的方法,很容易就可以写出N叉树的前序遍历。先来看递归的解法,主要实现一个递归函数即可,判空之后,将当前结点值加入结果res中,然后遍历子结点数组中所有的结点,对每个结点都调用递归函数即可,参见代码如下: 解法一: class Solution { public: vector<int> preorder(Node* root) { vector<int> res; helper(root, res); return res; } void helper(Node* node, vector<int>& res) { if (!node) return; res.push_back(node->val); for

leetcode-位运算

吃可爱长大的小学妹 提交于 2020-01-25 03:41:09
位运算 文章目录 位运算 LeetCode 231. Power of Two LeetCode 762. Prime Number of Set Bits in Binary Representation LeetCode 136. Single Number LeetCode 476. Number Complement LeetCode 137. Single Number II LeetCode 260. Single Number III LeetCode 371. Sum of Two Integers LeetCode 201. Bitwise AND of Numbers Range LeetCode 477. Total Hamming Distance LeetCode 421. Maximum XOR of Two Numbers in an Array LeetCode 231. Power of Two class Solution { public : bool isPowerOfTwo ( int n ) { return n > 0 && ( 1 << 30 ) % n == 0 ; //return (n>0 && (n & -n) == n); } } ; //lowbit,取出n二进制中第一个1的位置k /* lowbit(n) = n & (

《1.9-1.15 Leetcode》

六眼飞鱼酱① 提交于 2020-01-24 10:55:56
1. 路径总和 题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。 说明: 叶子节点是指没有子节点的节点。 代码: package leetcode . week4 ; /** * @author chengzhengda * @version 1.0 * @date 2020-01-14 17:09 * @desc 路径总和 */ public class t112 { public static boolean hasPathSum ( TreeNode root , int sum ) { if ( root == null ) { return false ; } sum -= root . data ; // 判断当前节点是否为叶子节点 if ( root . left == null && root . right == null ) { if ( sum != 0 ) { return false ; } else { return true ; } } // 当前节点的子节点是否有一个满足要求 return hasPathSum ( root . left , sum ) || hasPathSum ( root . right , sum ) ; } public static void main

272. Closest Binary Search Tree Value II

浪子不回头ぞ 提交于 2020-01-24 02:34:21
题目: Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target. Note: Given target value is a floating point. You may assume k is always valid, that is: k ≤ total nodes. You are guaranteed to have only one unique set of k values in the BST that are closest to the target. Follow up: Assume that the BST is balanced, could you solve it in less than O ( n ) runtime (where n = total nodes)? Hint: Consider implement these two helper functions: getPredecessor(N) , which returns the next smaller node to N. getSuccessor(N) , which returns the next

LeetCode 324:摆动排序Ⅱ

不问归期 提交于 2020-01-23 23:09:40
LeetCode 324:摆动排序Ⅱ 题目描述 解题思路 代码实现 总结 题目描述 给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]… 的顺序。 示例 1: 输入: nums = [1, 5, 1, 1, 6, 4] 输出: 一个可能的答案是 [1, 4, 1, 5, 1, 6] 示例 2: 输入: nums = [1, 3, 2, 2, 3, 1] 输出: 一个可能的答案是 [2, 3, 1, 3, 1, 2] 说明: 你可以假设所有输入都会得到有效的结果。 进阶: 你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗? 解题思路 先将数组排序 ,然后一分为二, 从中间分为两个数组 ,暂且称为小数组和大数组吧!那么是不是将小数组和大数组元素交叉组合就满足摆动数组条件呢?有一种特例那就是当数组个数为偶数个,且第n/2和第(n/2+1)个元素相等时,上述不满足,距离[4,5,5,6]。小数组为[4,5]大数组为[5,6]交叉结合结果[4,5,5,6]不满足摆动数组定义!!!怎么办?????答案是 把大数组和小数组都逆序排列再依次对应组合 ,也就是把[5,4]和[6,5]依次插入组合为[5,6,4,5]即可!!!! 代码实现 去 博客设置 页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的

Leetcode——两数之和python

余生长醉 提交于 2020-01-23 15:23:42
Leetcode——两数之和python 方法一: 方法二: # 题目 : 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum 方法一: class Solution : def twoSum ( self , nums : List [ int ] , target : int ) - > List [ int ] : for i in range ( len ( nums ) ) : temp = target - nums [ i ] if temp in nums : j = nums . index ( temp ) if i == j : continue else : return [ i , j ] break ; else : continue 方法二: class Solution : def twoSum ( self ,