leetcode

Leetcode#12 integer-to-roman

[亡魂溺海] 提交于 2020-01-19 04:40:21
https://leetcode.com/problems/integer-to-roman/submissions/ * 12.php <?php class Solution { // a[0] > a[1] > a[2] ... private static function indexLessThan($a, $ans, $start=0) { $n = count($a); for ($i = $start; $i < $n; $i++) { if ($a[$i] <= $ans) { return $i; } } return -1; } /** * @param Integer $num * @return String */ public function intToRoman($num) { $a = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; $b = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L','XL','X','IX','V', 'IV', 'I']; $r = []; while (($i = self::indexLessThan($a, $num, 0)) >= 0) { $num -= $a[$i]; array_push($r, $b[$i]); }

算法:回溯七 Permutation Sequence数组全排列定位

↘锁芯ラ 提交于 2020-01-18 08:17:04
题目 地址: https://leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3: “123” “132” “213” “231” “312” “321” Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 inclusive. Given k will be between 1 and n! inclusive. Example 1: Input : n = 3 , k = 3 Output : "213" Example 2: Input : n = 4 , k = 9 Output : "2314" 回溯DFS实现 思路: 获取少一位的全排列,取商为相应的位置,把值取出来添加到结果 StringBuilder 对象。剩下余数继续上面的逻辑即可。 package backtracking ;

LeetCode刷题技巧

感情迁移 提交于 2020-01-18 05:03:06
虽然大多数经验你们大概也在互联网的这里或那里看过,不过我还是摆在这里吧。更系统一些,也许能帮到一些朋友: 最重要的是行动,现在立刻马上就去开始刷题。 一看二抄三改四写。 前期博客、文档、ppt比算法原理书重要。如果要学会用算法,书不够看也没必要,应该刷题。刷题时,首先还是要有方向、有脉络地刷题,切忌乱打拳,也就是要刷专题,刷专题就是说,找同类的题(一次只针对一种题型进行训练,如数组、链表、二叉树、回溯、动态规划),对题解打,逐步脱离题解,直到能自己做同类的题。 切忌眼高手低。不要想着自己知道思路解法了就是会了,一定要亲自Coding,手撸出来。我在刷的过程中就经常在Debug的时候才发现自己忘记考虑了某些条件。不把代码写出来,只看别人的答案对自己是没有多大的提高的,只有亲自AC了题目,才能算做过一道题。 可能刚开始会对手撸代码有压力有恐惧,总结就是心态一定要硬刚,总共就那几种题型,我刚开始也很抵触的,觉得太难了,后面迎难而上也就那么回事。第一遍不行就第二遍,不然就第三遍,直到现在***里面也有一些我不懂的题目,但我也不会去深究,大局为重,不抠细节。 关于书:敲一个算法是编程能力,是简单的;但算法能力在于总结归纳一个算法(这个太难了,《算导》更加像是讲这个)和把问题与算法联系起来(这个应该培养)。如果要看书的话,按照书里面的算法一个一个学,每学一个算法之前找好几道使用这个算法的题

VScode中配置使用LeetCode(Ubuntu)

女生的网名这么多〃 提交于 2020-01-17 21:30:20
目录 安装配置Node.js 安装LeetCode插件并更改设置 即将步入为准备找工作而刷题阶段,听说可以不用打开浏览器而直接在 VScode 中刷题,这么酷的事情当银要试试啦! 安装配置Node.js 在 VScode 中安装 LeetCode 插件前,要先安装 Node.js (其实我也不懂这是干嘛的哈哈哈哈)。具体安装方法参考: Node.js 安装配置 这个教程中我采用的方法是 Linux 上安装 Node.js 这块中的 直接使用已编译好的包 ,需要注意的是后面制作软连接的时候第一个路径是你下载的编译好的文件夹路径,所以下载的文件夹要先放好在一个地方后在制作软链接,而且注意不能删掉它。 安装LeetCode插件并更改设置 在 VScode的 扩展那里直接搜索 LeetCode ,然后点击 安装 即可。接着就可以使用 F1 或者 Ctrl+shift+P 键打开命令输入口,输入 LeetCode:Sign in 进行登录,但是你发现会一直报错登录不上,提示是密码不对,可命名用这个账号和密码在浏览器上登录就可以呀,什么原因呢?是因为这么默认登录的是 LeetCode 的英文版,而我们一般使用的是 LeetCode 的中文版,这两个版本是不互通的,所以出现了错误。这是需要我们切换到LeetCode的中文版,怎么切换呢?如果你安装好 LeetCode 后, VScode

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

不羁的心 提交于 2020-01-17 15:36:26
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2 . Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9 . Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or convert the inputs to integer directly. 这道题让我们求两个字符串的相加,之前 LeetCode 出过几道类似的题目,比如二进制数相加,还有链表相加,或是字符串加1,基本思路很类似,都是一位一位相加,然后算和算进位,最后根据进位情况看需不需要补一个高位,难度不大,参见代码如下: class Solution { public: string addStrings(string num1, string num2) { string res = ""; int m = num1.size(), n = num2.size(), i = m - 1,

从 0 学习 Python 0 - 50 大合集总结 Python技术

£可爱£侵袭症+ 提交于 2020-01-17 07:48:45
大家使用的时候找到对应的代码文件夹直接 Ctrl +B 在 IDK工具中就可以验证执行相应代码即可。以此提高学习效率。 另外学习结构体系中咱们还加入 LeetCode 面试系列好文,大家可以边学习基础知识,边了解面试及算法知识,这样后期系统学习下来对 Python 的使用会有事半功倍的效果。文章目录如下: 前 50 天基础文章目录: 第1天:Python 环境搭建 第2天:Python 基础语法 第3天:Python 变量与数据类型 第4天:Python 流程控制 第5天:Python函数 第6天:Python 模块和包 第7天:Python 数据结构--序列 第8天:Python List 第9天:Python tupple 第10天:Python 类与对象 第11天:Python 字典 第12天:Python 集合 第13天:Python 函数的参数 第14 天:Python 高阶函数 第15天:Python 输入输出 第16天:Python 错误和异常 第17天:Python 之引用 第18天:Python 之迭代器 第19天:Web 开发 Flask 介绍 第20天:Python 之装饰器 第21天:Web开发 Jinja2模板引擎 第22天:Python NameSpace & Scope 第23天:Python Standard Library 01 第24天

leetcode刷题记第17题解法(python解析)

空扰寡人 提交于 2020-01-17 02:08:01
leetcode刷题记--> 17题解法(python解析) 题目定义 解题 1. 使用reduce方法进行解决问题 2. 动态规划 3. 动态规划 4. 使用递归 实现 题目定义 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 示例: 输入:“23” 输出:[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. 说明: 尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。 来源:力扣(LeetCode) 链接: leetcode_17题 . 解题 本次使用4种方法,在leetcode上还有更多的方法,只能说真牛逼,真聪明。 1. 使用reduce方法进行解决问题 熟悉大数据的同学都知道map reduce 此处就使用reduce进行解决 2. 动态规划 使用列表生成式 3. 动态规划 同样是列表生成式,比较绕,仔细理解 4. 使用递归 对于打印"2345"这样的字符串: 第一次递归就是上图中最下面的方格,然后处理完第一个字符2之后,将输入的字符改变成"345"并调用第二个递归函数 第二次递归处理3,将字符串改变成"45"后再次递归 第三次递归处理4,将字符串改变成"5"后继续递归 第四次递归处理5,将字符串改变成"

leetcode 69. Sqrt(x) 二分法 python3

淺唱寂寞╮ 提交于 2020-01-16 21:08:01
一.问题描述 Implement int sqrt(int x) . Compute and return the square root of x , where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1: Input: 4 Output: 2 Example 2: Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned. 二.解题思路 方法一:迭代 从0~i开始迭代,知道i*i大于x的时候,返回i-1. 时间复杂度:O(N)。 方法二:二分法 这道题其实就是然我们从0~x中,找一个数满足 i*i<=x and (i+1)*(i+1)>x。 和二分法的思路差不多,注意结束条件,结束条件l>r,返回值l-1,原因参考结束条件(当l超过r的时候,说明l-1太小,但是l=r,说明l又太大)

【算法】——Leetcode 287 Find the Duplicate Number 找重复元素

送分小仙女□ 提交于 2020-01-16 13:19:05
目录 题目 随想 思路及代码 方法1 哈希表 优点: 缺点: 方法2 排序 优点: 缺点: 方法3 二分查找 优点: 缺点: 代码 方法4 利用索引交换 优点: 缺点: 代码: 方法5 链表判环 优点: 缺点: 代码: 题目 原题链接 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. 给定一个包含 n + 1 n+1 n + 1 个元素的数组,每个元素都在 1 1 1 到 n n n 之间(闭区间),证明至少一个元素重复,找出重复元素。 随想 这也是我遇到的一个面试题,当时想了很长时间,表现也不太好。 这个题和另外一个题:每个元素出现两次,有一个出现一次( Leetcode 136 Single Number )较为类似,有正好反过来的感觉。Leetcode136那道题异或位运算直接秒之。 证明一定有一个重复元素并不难,根据 抽屉原理 即可。 下面说如何找到它。 注意:并不要求 1 1 1 到

LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

本秂侑毒 提交于 2020-01-16 07:29:50
此文首发于我的个人博客: LeetCode 111. Minimum Depth of Binary Tree–Python解法–二叉树最小高度–迭代,递归 — zhang0peter的个人博客 LeetCode题解文章分类: LeetCode题解 LeetCode 所有题目总结: LeetCode 所有题目总结 题目地址: Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 这道题目是计算二叉树最小高度,可以用迭代或者递归来做。 递归解法如下。 Python解法如下: # Definition for a binary tree node. # class TreeNode: # def __init__(self,