leetcode

Leetcode简单题41~60

╄→尐↘猪︶ㄣ 提交于 2019-12-06 06:34:03
41.思路:分别在列表中存放两个单词的索引,再用两次遍历求距离# 给定一个单词列表和两个单词 word1 和 word2,返回列表中这两个单词之间的最短距离。# 示例:# 假设 words = ["practice", "makes", "perfect", "coding", "makes"]# 输入: word1 = “coding”, word2 = “practice”# 输出: 3# 输入: word1 = "makes", word2 = "coding"# 输出: 1#meclass Solution0(object): def shortestDistance(self, words, word1, word2): result = [] idwords1 = [id_ for id_,word in enumerate(words) if word == word1] idwords2 = [id_ for id_,word in enumerate(words) if word == word2] for id1 in idwords1: for id2 in idwords2: result.append(abs(id1-id2)) return min(result)#other 复杂度O(n)class Solution1(object): def

[LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增

微笑、不失礼 提交于 2019-12-06 05:58:56
A string of '0' s and '1' s is monotone increasing if it consists of some number of '0' s (possibly 0), followed by some number of '1' s (also possibly 0.) We are given a string S of '0' s and '1' s, and we may flip any '0' to a '1' or a '1' to a '0' . Return the minimum number of flips to make S monotone increasing. Example 1: Input: "00110" Output: 1 Explanation: We flip the last digit to get 00111. Example 2: Input: "010110" Output: 2 Explanation: We flip to get 011111, or alternatively 000111. Example 3: Input: "00011000" Output: 2 Explanation: We flip to get 00000000. Note: 1 <= S.length

LeetCode刷题总结-树篇(下)

安稳与你 提交于 2019-12-06 04:14:12
本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结。前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题,2道困难题。本篇总结的知识点请参考下图: 1 新概念定义问题 本部分收录习题如下: 117.填充每个节点的下一个右侧节点指针II, 难度: 中等 297.二叉树的序列化与反序列化,难度:困难 114.二叉树展开为链表,难度: 中等 998.最大二叉树II, 难度:中等 834.树中距离之和,难度:困难 2 子树问题 本部分收录的习题如下: 508.出现次数最多的子树元素和,难度:中等 652.寻找重复的子树,难度:中等 865.具有所有最深结点的最小子树,难度:中等 1110.删点成林,难度:中等 来源: https://www.cnblogs.com/liuzhen1995/p/11961322.html

LeetCode:Nim Game

落爺英雄遲暮 提交于 2019-12-06 02:41:56
1、题目名称 Nim Game(尼姆博弈) 2、题目地址 https://leetcode.com/problems/nim-game/ 3、题目内容 英文: You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap. For example, if there are 4 stones in the heap, then you will never win the game: no matter

leetcode:程序员面试技巧

空扰寡人 提交于 2019-12-06 00:54:09
起因 写在开头,脑袋铁定秀逗了,历时20多天,刷完了leetcode上面151道题目(当然很多是google的),感觉自己对算法和数据结构算是入门了,但仍然还有很多不清楚的地方,于是有了对于每道题目写分析的冲动。不过在看到leetcode上面的文章之后,决定先从翻译入手,顺带再写写自己做题的心得体会。今天是第一篇:程序员面试技巧。 如果你主修计算机科学,那么在你工作的时候会碰到很多有难度的编程问题。当你去找工作的时候,你会有很多的面试,而面试官通常很喜欢问你很多技术性的问题,以下就是三类主要的题型: 算法/数据结构 特定编程语言/通用的计算机知识 脑筋急转弯 后续我将详细的讨论上面这几点: 算法/数据结构 你必须深刻的理解以下几种数据结构:数组,链表,二叉树,哈希表等。你必须明确地知道什么时候该使用数组或者链表,譬如在实现一个列表翻转的时候。 面试官通常会问你一些算法问题用以检验你的编程解决问题能力,一些算法问题如下: 在一个句子里面反转单词。 在一个单词里面反转字母。(尝试使用迭代或者递归解决) 如何在一个列表里面找到重复的数字。 生成数字n的全排列。(小提示:递归) 找到两个排好序数组的中间值。(小提示:分治法) 如何进行拼写检查并验证单词。(小提示:编辑距离) (译者吐槽,说句实话,上面几个问题如果没做题大部分我还真答不出来。) 特定编程语言/通用的计算机知识

LeetCode做题第三四天

♀尐吖头ヾ 提交于 2019-12-06 00:51:27
LeetCode做题第四天 今天晚上就不想做题,每次都是默默下定决心,然后默默放弃,没有办法,生活就是这么“吸引人”。就只好继续做一道简单题来完成任务。 编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true 解释: 8 = 2 × 2 × 2 示例 3: 输入: 14 输出: false 解释: 14 不是丑数,因为它包含了另外一个质因数 7。 说明: 1 是丑数。 输入不会超过 32 位有符号整数的范围: [−231, 231 − 1]。 看上去就很简单的一道题,无非就是考你数学,为你如何判断一个数是否只有2,3, 5这几个因数,用取余做判断,为零就除以取余为零的数,同理编写三个就好了,最后返回num == 1就好了。 class Solution: def isUgly(self, num: int) -> bool: if num <= 0: return False while num % 5 == 0: num /= 5 while num % 3 == 0: num /= 3 while num % 2 == 0: num /= 2 return num == 1 来源:力扣(LeetCode) 链接: https:/

LeetCode做题第三天

荒凉一梦 提交于 2019-12-05 20:44:24
LeetCode做题第三天 ** 依旧是以简单的题目先开启大脑** 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 说明:不要使用任何内置的库函数,如 sqrt。 示例 1: 输入:16 输出:True 示例 2: 输入:14 输出:False 自己做这道题居然超出时间限制了。难受,不过还好,通过 别人的 代码学到了很多,二分查找等等,不过我觉得最有意思的就是第一种方法。 最后附上我最后提交的代码 最简单的一句代码:return num**0.5 == int(num**0.5) class Solution: def isPerfectSquare(self, num: int) -> bool: i = num while i * i > num: i = (i + num / i) // 2 return i * i == num 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/valid-perfect-square 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 来源: https://www.cnblogs.com/curtain-r/p/11946273.html

LeetCode:Department Top Three Salaries -各部门工资最高的三人

泄露秘密 提交于 2019-12-05 17:42:29
1、题目名称 Department Top Three Salaries(各部门工资最高的三个人) 2、题目地址 https://leetcode.com/problems/department-top-three-salaries/ 3、题目内容 表Employee保存了所有雇员的数据,每名雇员都有一个Id,和一个部门Id(DepartmentId) +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+ 表Department保存了每个部门的Id和名称 +----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+

【leetcode算法-简单】9. 回文数

依然范特西╮ 提交于 2019-12-05 16:46:32
【题目描述】 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 示例 3: 输入: 10 输出: false 解释: 从右向左读, 为 01 。因此它不是一个回文数。 进阶: 你能不将整数转为字符串来解决这个问题吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/palindrome-number 【 解答 】 解法一:将整数转化为字符串,判断字符串反转后是否与之前的相等    def isPalindrome(x)l: return str(x) == str(x)[::-1]   执行用时:60ms 解法二:计算出此整数的倒序整数n def isPalindrome(x): if x<0: return False num = x n = 0 while num: n = n*10 + num%10 num = num // 10 if n == x: return True else: return False   执行用时:101ms 判断一个整数是否是回文数。回文数是指正序(从左向右

日常 - 在 Windows 系统下使用批处理文件自动提交代码到 GitHub 上

不想你离开。 提交于 2019-12-05 16:43:48
需求 每天都上传代码到 GitHub 上,需要一种脚本文件自动完成一些重复的工作。 git add . git commit -m "message" git push 缺点是上传的 message 都为 "auto commit",违背了代码管理的初衷,以后查看这些 message 很难知道当时修改了什么内容。 经过测试可得,如果当天没有修改项目文件内容,则不会完成 add commit push 操作。因此也可以在需要记录的当天手动执行 commit,加上一些必要的 message 信息,批处理文件则会完成 push 操作。 此前准备,需要已经配置好手动完成 push 到远程仓库的操作,在这种条件下才能完成自动化脚本的正确运行。 实现 编写文件 先在 Windows 下创建一个 leetcode.txt 文件,并输入以下内容。完成后更改文件内容后缀名,使其变成 leetcode.bat 批处理文件。 @echo off @title bat execute git auto commit F: cd F:/Code/Java/algorithm/leetcode git add . git commit -m "Auto commit." git push 解释说明:再次强调,如果看了这些解释仍不清楚 bat 文件的作用则需要先手动 commit 到 GitHub 上。