leetcode

Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

强颜欢笑 提交于 2019-11-30 06:33:11
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ 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. 分析: leetcode上的这个不带精度要求,且输出一个整数即可(其实可以当成精度要求小于等于1)。 方法一:(二分法) 对于本题,最直观的方法就是二分法。使用二分法时,需要注意有三个指针,分别指向前中后(pre、medium、last,在书写、习惯

LeetCode:Palindrome Number

荒凉一梦 提交于 2019-11-30 05:59:05
1、题目名称 Palindrome Number(回文数) 2、题目地址 https://leetcode.com/problems/palindrome-number 3、题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4、解题方法1 将数字翻转后判断与原数字是否相等,可以参考 LeetCode第7题(Reverse Integer) 的解题思路。Java代码如下: /** * 功能说明:LeetCode 9 - Palindrome Number * 开发人员:Tsybius * 开发时间:2015年9月24日 */ public class Solution { /** * 判断某数是否为回文数 * @param x 数字 * @return true:是,false:否 */ public boolean isPalindrome(int x) { //负数不能为回文数 if (x < 0) { return false; } //反转数字观察是否相等 if (x == reverse(x)) { return true; } else { return false; } } /** * 反转数字 * @param x

LeetCode-链表

限于喜欢 提交于 2019-11-30 05:55:22
138. Copy List with Random Pointer 复制带有随机指针的链表 https://leetcode.com/problems/copy-list-with-random-pointer/ 题目:给出一个链表,使得每个节点包含一个额外的随机指针,该指针可以指向列表中的任何节点或NULL。 思路:需要复制的链表带有随机指针,每一个节点都随机指向任意一个节点或指向null,第一次遍历生成所有新节点,同时建立一个原节点和新节点的 HashMap,第二次遍历给随机指针赋值。 141. Linked List Cycle 有环的链表 https://leetcode.com/problems/linked-list-cycle/ 题目:给定一个链表,确定其中是否有一个循环。为了表示给定链表中的一个循环,我们使用一个整数pos,它表示尾连接到的链表中的位置(0索引)。如果pos是 -1,则链接列表中没有循环。 思路:采用快慢指针,快指针每次移动两步,慢指针每次移动一步,如果快慢指针汇合,则说明环存在,否则不存在。 142. Linked List Cycle II 有环的链表 II https://leetcode.com/problems/linked-list-cycle-ii/ 题目:给定一个链表,返回环开始的节点。如果没有环,则返回null

ARTS 20190921 algorithm 395. Longest Substring with At Least K Repeating Characters

北慕城南 提交于 2019-11-30 05:53:02
今天做了一道算法题 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 我的实现是暴力算法,思路比较简单,O(n2)算法复杂度。我看leetcode里面的讨论,这个思路不错,是O(n)算法复杂度。 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 他的代码很清晰,采用递归:找到第一个出现次数不到k次的字符,将字符串分成两半,然后对两半分别递归得到结果。 来源: https://blog.csdn.net/tassardge/article/details/101119102

LeetCode:Duplicate Emails

落爺英雄遲暮 提交于 2019-11-30 04:33:48
1、题目名称 Duplicate Emails(重复出现的Email) 2、题目地址 https://leetcode.com/problems/duplicate-emails/ 3、题目内容 有一个数据表包括Id和Email两列,找出数据表内Email列内容重复出现的Email数据。 例如,现有一个表Person内容如下: +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+ 那么查询得到的结果集为: +---------+ | Email | +---------+ | a@b.com | +---------+ 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本: -- 执行脚本前必须建立名为LEETCODE的DATABASE USE LEETCODE; DROP TABLE IF EXISTS Person; CREATE TABLE Person ( Id INT NOT NULL PRIMARY KEY, Email VARCHAR(50) ); INSERT INTO Person (Id, Email)

LeetCode DFS Course Schedule 课程表 Find Eventual Safe States找到最终的安全状态

你说的曾经没有我的故事 提交于 2019-11-30 03:33:48
题目链接: 1、Course Schedule https://leetcode.com/problems/course-schedule/ 2、Find Eventual Safe States https://leetcode.com/problems/find-eventual-safe-states/ 这两题有相似性很高,区别在于第一题是判断这个图中有没有环,第二题是找出连接图中环的节点以及环上的节点 这是DFS专栏,所以暂时不考虑BFS解法 第一题其实很像拓扑排序,但是向无环图(DAG)是拓扑排序的充要条件。题目的本意是要判断是不是DAG,所以和拓扑排序还是 有点差别。(拓扑排序也有DFS和两种解法) 解法1: 使用visit数组来保存图中每一个节点的访问情况,visit[i] = 1表示 i 节点被访问了。 (本解法不适用第二题,会出现TLE) // class Solution { public: // if loop, return true bool dfs(int node,vector<int>& visit, vector<vector<int>>& graph) { if(visit[node] == 1) { return true; } visit[node] = 1; vector<int> temp = graph[node]; for(int i

LeetCode

不想你离开。 提交于 2019-11-30 00:01:26
LeetCode 1. Two Sum LeetCode 2.Add Two Numbers LeetCode 3. Longest Substring Without Repeating Characters LeetCode 4. Median of Two Sorted Arrays LeetCode 5. Longest Palindromic Substring LeetCode 6. ZigZag Conversion LeetCode 8. String to Integer(aoti) LeetCode 10 Regular Expression Matching LeetCode 11.Container With Most Water LeetCode 12. Integer to Roman LeetCode 15.3Sum LeetCode 16. 3Sum Closest LeetCode 17. Letter Combinations of a Phone Number LeetCode 18. 4Sum LeetCode 19. Remove Nth Node From End of List LeetCode 22.Generate Parentheses LeetCode 29. Divide Two Integers 来源: https:/

Leetcode 300. Longest Increasing Subsequence

江枫思渺然 提交于 2019-11-29 21:56:42
https://leetcode.com/problems/longest-increasing-subsequence/ Medium Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more than one LIS combination, it is only necessary for you to return the length. Your algorithm should run in O( n2 ) complexity. Follow up: Could you improve it to O( n log n ) time complexity? 经典DP。 第一种解法DP,定义f[ i ]为i位置时最长增长序列长度,f[ i ] = max( f[ j ] + 1 ) if nums[ i ] > nums[ j ]

【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses

倖福魔咒の 提交于 2019-11-29 20:57:56
题目如下: Given a string s that consists of lower case English letters and brackets. Reverse the strings in each pair of matching parentheses, starting from the innermost one. Your result should not contain any bracket. Example 1: Input: s = "(abcd)" Output: "dcba" Example 2: Input: s = "(u(love)i)" Output: "iloveu" Example 3: Input: s = "(ed(et(oc))el)" Output: "leetcode" Example 4: Input: s = "a(bcdefghijkl(mno)p)q" Output: "apmnolkjihgfedcbq" Constraints: 0 <= s.length <= 2000 s only contains lower case English characters and parentheses. It's guaranteed that all parentheses are balanced. 解题思路:

【leetcode】1189. Maximum Number of Balloons

雨燕双飞 提交于 2019-11-29 20:56:53
题目如下: Given a string text , you want to use the characters of text to form as many instances of the word "balloon" as possible. You can use each character in text at most once. Return the maximum number of instances that can be formed. Example 1: Input: text = "nlaebolko" Output: 1 Example 2: Input: text = "loonbalxballpoon" Output: 2 Example 3: Input: text = "leetcode" Output: 0 Constraints: 1 <= text.length <= 10^4 text consists of lower case English letters only. 解题思路: 题目很简单,求出text中b,a,l,o,n这五个字符的出现次数的最小值即可。但有两点需要注意,一是text必须同时包含这五个字符,而是l和o是要算双份。 代码如下: class Solution(object): def