Leetcode经验总结
Leetcode经验总结
Ricky.Guo
人工智障/仙工小硕
展开
-
Leetcode String to Integer (atoi) Python 如何从一堆符号中挑选出数字并表示出来,正则表达式,.strip() .group()
Leetcode 第8题 String to Integer (atoi)Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes原创 2020-06-05 22:50:23 · 247 阅读 · 0 评论 -
Leetcode Longest Palindromic Substring Python 如何在遗传字符串中找到回文的子字符串
Leetcode 第5题 Longest Palindromic SubstringGiven a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Example 2:Input:原创 2020-06-04 04:49:16 · 121 阅读 · 0 评论 -
Leetcode Longest Substring Without Repeating Characters Python 找到字符串中重复次数最多的字符长度
Leetcode 第3题 Longest Substring Without Repeating CharactersGiven a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:原创 2020-06-02 22:40:27 · 206 阅读 · 0 评论 -
Leetcode Add Two Numbers Python 用链表的方式做加法,如何构造链表
Leetcode 第2题 Add Two NumbersYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.You may原创 2020-06-02 01:43:54 · 126 阅读 · 0 评论 -
Leetcode Fizz Buzz Python 如何通过条件改变数组中的值
Leetcode 412题 Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of原创 2020-05-29 22:19:50 · 180 阅读 · 0 评论 -
Leetcode First Unique Character in a String Python 列表中找到第一个只出现一次的第一个数字,如何计算字符出现次数。
Leetcode 387题 First Unique Character in a StringGiven a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: You may assume th原创 2020-05-29 01:56:10 · 184 阅读 · 0 评论 -
Leetcode Sum of Two Integers Python 使用与或非位运算实现加法 经典算法
Leetcode 371题 Sum of Two IntegersCalculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example 1:Input: a = 1, b = 2Output: 3Example 2:Input: a = -2, b = 3Output: 1题目大意: 不用加减号,实现相加的功能。这是到非常经典的计算机原理的题,如原创 2020-05-28 04:53:38 · 341 阅读 · 0 评论 -
Leetcode Intersection of Two Arrays II Python 如何求两个数组的交集 Counter()统计出现次数和.element()返回指定次数个元素 的用法
Leetcode 350题 Intersection of Two Arrays IIGiven two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2,2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [4,9]Note:E原创 2020-05-25 20:58:35 · 192 阅读 · 0 评论 -
Leetcode Reverse String Python 如何反转列表中的字符串(元素) 列表中首尾相换s[i],s[l-i-1] = s[l-i-1],s[i]
Leetcode 344题 Reverse StringWrite a function that reverses a string. The input string is given as an array of characters char[].Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.原创 2020-05-24 23:53:44 · 330 阅读 · 0 评论 -
Leetcode Power of Three Python 如何判断给定数值是否为某一个数的幂 integer类型的取值范围是(-2,147,483,648,2,147,483,647)
Leetcode 326题 Power of ThreeGiven an integer, write a function to determine if it is a power of three.Example 1:Input: 27Output: trueExample 2:Input: 0Output: falseExample 3:Input: 9Output: trueExample 4:Input: 45Output: falseFollow u原创 2020-05-23 22:17:21 · 851 阅读 · 0 评论 -
Leetcode Missing Number Python 如何找出数组中缺少的值,异或方法。
leetcode 268题 Missing NumberGiven an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input: [9,6,4,2,3,5,7,0,1]Output: 8Note:Your algorithm sh原创 2020-05-22 20:34:41 · 208 阅读 · 0 评论 -
Leetcode Valid Anagram Python 如何判断两字符串拆开所有元素是否一样?
Leetcode 242题 Valid AnagramGiven two strings s and t , write a function to determine if t is an anagram of s.Example 1:Input: s = "anagram", t = "nagaram"Output: trueExample 2:Input: s = "rat", t = "car"Output: falseNote:You may assume the str原创 2020-05-19 20:07:55 · 149 阅读 · 0 评论 -
Leetcode Delete Node in a Linked List Python 单链中删除一个节点的方法,先赋值,后跳过。
Leetcode 237题 Delete Node in a Linked ListWrite a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9], which looks like following:Example 1:Input: head = [4,5,1,9],原创 2020-05-17 21:54:45 · 126 阅读 · 0 评论 -
Leetcode Contains Duplicate Python | 如何判断列表中有重复数字? Python中用set()消重复
Leetcode 217题 Contains DuplicateGiven an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.Example 1:原创 2020-05-16 21:33:01 · 184 阅读 · 0 评论 -
Leetcode Count Primes Python 求小于给出值所有的质数(素数) | 函数内嵌函数直接调用
Leetcode 204题 Count PrimesCount the number of prime numbers less than a non-negative number, n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.题目大意: 找出小于n的所有质数的个数。这就是个数学问题。先给定义什么是质数?质数(Primenum原创 2020-05-15 23:32:34 · 531 阅读 · 0 评论 -
Leetcode Happy Number Python 计算数字每一位的平方和 | str()为了遍历每一位,再int()回来是为了数学计算
Leetcode 202题 Happy NumberWrite an algorithm to determine if a number n is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the原创 2020-05-13 21:41:54 · 1186 阅读 · 0 评论 -
Leetcode Number of 1 Bits Python 操作32位的数字要小心
Leetcode 191题 Number of 1 BitsWrite a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).Example 1:Input: 00000000000000000000000000001011Output: 3Explanation: The input binary string 0原创 2020-05-13 01:54:05 · 127 阅读 · 0 评论 -
Leetcode Reverse Bits Python python数组的高级裁切(切片)操作 | python[:]的多元操作 | 通过切片反转列表
Leetcode 190题 Reverse BitsReverse bits of a given 32 bits unsigned integer.Example 1:Input: 00000010100101000001111010011100Output: 00111001011110000010100101000000Explanation: The input binary string 00000010100101000001111010011100 represents the u原创 2020-05-11 20:43:40 · 152 阅读 · 0 评论 -
Leetcdoe Rotate Array Python 如何旋转(移动)数组,如何用取余表示循环后的元素
Leetcode 189题 Rotate ArrayGiven an array, rotate the array to the right by k steps, where k is non-negative.Follow up:Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.Could you do it in-place with O原创 2020-05-10 21:04:03 · 152 阅读 · 0 评论 -
Leetcode Factorial Trailing Zeroes Python 如何求阶乘之后的结果结尾有多少个0(零)
Leetcode 172题 Factorial Trailing ZeroesGiven an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: 5Output: 1Explanation: 5! = 120, one trailing zero.Note: You原创 2020-05-09 21:21:42 · 197 阅读 · 0 评论 -
Leetcode Excel Sheet Column Number Python 读取数据库(或excel)对应列的值,26进制转10进制,ord()函数。
Leetcode 171题 Excel Sheet Column NumberGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z ...原创 2020-05-07 22:32:00 · 208 阅读 · 0 评论 -
Leetcode Valid Palindrome Python 什么是回文 | 如何挑选出列表中的字母和数字.isalnum()方法
Leetcode 125题 Valid PalindromeGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty stri...原创 2020-05-07 04:17:17 · 137 阅读 · 0 评论 -
Leetcode Best Time to Buy and Sell Stock II Python | 多次买卖股票求收益问题
Leetcode 122题 Best Time to Buy and Sell Stock IISay you have an array prices for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may ...原创 2020-05-06 23:20:47 · 160 阅读 · 0 评论 -
Leetcode Pascal's Triangle Python 经典题目,构建杨辉三角
Leetcode 118题 Pascal’s TriangleGiven a non-negative integer numRows, generate the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above i...原创 2020-05-04 23:22:51 · 167 阅读 · 0 评论 -
Leetcode Convert Sorted Array to Binary Search Tree Python 如何把数组转化为BST(二叉搜索树),经典的二叉树操作。
Leetcode 108题 Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tr...原创 2020-05-03 23:25:26 · 259 阅读 · 0 评论 -
Leetcode Remove Element Python 用变量名删除数组中的相同元素,python的优势。
Leetcode 27题 Remove ElementGiven an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this ...原创 2020-05-03 21:50:46 · 153 阅读 · 0 评论 -
Leetcode Merge Sorted Array Phthon | 两个数组的互相比较插入,排序。
Leetcode 88题 Merge Sorted ArrayGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n res...原创 2020-04-27 00:03:50 · 102 阅读 · 0 评论 -
Leetcode Sqrt(x) 实现求某一个数的平方根算法 Python
Leetcode 69题 Sqrt(x)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...原创 2020-04-25 21:33:15 · 479 阅读 · 0 评论 -
Leetcode Implement strStr() Python 长串中寻找短串。实现python中的.fing()方法。
Leetcode 28题 Implement strStr()Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needl...原创 2020-04-18 03:49:55 · 137 阅读 · 0 评论 -
Leedcode Remove Duplicates from Sorted Array Python 不用额外空间,在当前集合内去重
Leetcode 26题 Remove Duplicates from Sorted ArrayGiven a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra spac...原创 2020-04-17 00:05:07 · 109 阅读 · 0 评论 -
Leetcode Roman to Integer Python 计算罗马数字对应的十进制数字, for in 倒叙遍历逆循环参数设置
Leetcode 13 Roman to IntegerRoman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C ...原创 2020-04-15 03:59:05 · 191 阅读 · 0 评论 -
Leetcode Diameter of Binary Tree python 计算二叉树中两点间的最长距离(直径)
Leetcode Diameter of Binary Tree python 计算二叉树中两点间的最长距离(直径)原创 2020-04-10 00:45:42 · 535 阅读 · 0 评论 -
Leetcode Find All Numbers Disappeared in an Array | python 如何找出一组数列中缺少的值
以leetcode一个题为例子 记录用python如何找出一组数列中缺少的值原创 2020-04-08 04:29:33 · 253 阅读 · 0 评论 -
Leetcode Path Sum III python 二叉树上的操作 找出路径和为制定sum的路径个数
Leetcode 437题 Path Sum IIIYou are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the r...原创 2020-04-07 00:47:48 · 149 阅读 · 0 评论 -
Leetcode Reverse Integer python 按位反转数字 | python3中替代cmp()表示正负值用法 abs()取绝对值 bit_length()查看二进制长度
Leetcode Reverse Integer python 按位反转数字 | python3中替代cmp()表示正负值用法 abs()取绝对值 bit_length()查看二进制长度原创 2020-04-14 01:59:23 · 139 阅读 · 0 评论 -
Leetcode Invert Binary Tree python 反转二叉树的问题 还是DFS和BFS,递归和迭代的操作
Leetcode 226题 Invert Binary TreeInvert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1据说是谷歌面试题。很简单了,二叉树的题...原创 2020-03-30 23:50:05 · 633 阅读 · 0 评论 -
Leetcode Majority Element python 多种思路求集合中出现最多次数的值 提升计算机思维
Leetcode 169题 Majority ElementGiven an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty...原创 2020-03-26 00:13:47 · 198 阅读 · 0 评论 -
Leetcode Count and Say Python 数字按照字符串来操作 | 计数和报数
Leetcode 38题 Count and SayThe count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as “one ...原创 2020-04-20 00:16:58 · 120 阅读 · 0 评论 -
Leetcode Longest Common Prefix Python 集合内对比字符串,找到相同元素。
Leetcode 14题 Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string “”.Example 1:Input: ["f...原创 2020-04-16 04:33:16 · 132 阅读 · 0 评论 -
Leetcode Merge Two Binary Trees python 合并两颗二叉树
Leetcode 617. Merge Two Binary TreesGiven two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need ...原创 2020-04-13 01:39:40 · 150 阅读 · 0 评论