leetcode

【LeetCode】219. Contains Duplicate II

社会主义新天地 提交于 2019-12-02 04:43:58
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/contains-duplicate-ii/ Given an array of integers and an integer k , find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolutedifference between i and j is at most k . Example 1: Input: nums = [1,2,3,1], k = 3 Output: true Example 2: Input: nums = [1,0,1,1], k = 1 Output: true Example 3: Input: nums = [1,2,3,1,2,3], k = 2 Output: false Intuition hashSet Solution public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums == null) return false;

LeetCode:Combine Two Tables

随声附和 提交于 2019-12-02 04:31:06
1、题目名称 Combine Two Tables(跨表查询) 2、题目地址 https://leetcode.com/problems/combine-two-tables/ 3、题目内容 现在有两张表Person和Address,它们的表结构如下: 表Person: +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ 表Address: +-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ 写一个SQL语句,查出每个人的下列信息:FirstName, LastName, City, State 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库

[LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器

╄→гoц情女王★ 提交于 2019-12-02 03:51:56
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations: CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root ; CBTInserter.insert(int v) will insert a TreeNode into the tree with value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode ; CBTInserter.get_root() will return the head

LeetCode初级算法--树01:二叉树的最大深度

流过昼夜 提交于 2019-12-02 03:16:44
LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法、机器学习干货 csdn: https://blog.csdn.net/baidu_31657889/ csdn: https://blog.csdn.net/abcgkj/ github: https://github.com/aimi-cn/AILearners 一、引子 这是由LeetCode官方推出的的经典面试题目清单~ 这个模块对应的是探索的初级算法~旨在帮助入门算法。我们第一遍刷的是leetcode推荐的题目。 查看完整的剑指Offer算法题解析请点击github链接: github地址 二、题目 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。 1、思路 使用递归的方法解决此题。直接上图: 我们可以看到树的深度H等于1加上左右两个左右子树深度的最大值。 所以我们直接使用递归方法处理~看代码 2、编程实现 python # Definition for a binary tree node. # class

【LeetCode】234. Palindrome Linked List

别等时光非礼了梦想. 提交于 2019-12-02 02:36:43
Difficulty:easy More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/palindrome-linked-list/ Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up: Could you do it in O(n) time and O(1) space? Intuition Mehtod 1. reverse the right half of the list Method 2. use a stack to store Solution //Method 1: reverse half of the list public boolean isPalindrome(ListNode head) { if(head==null) return true; ListNode slow = head, fast = head; while(fast!=null && fast.next!=null){ slow = slow.next;

LeetCode 53. 最大子序和

江枫思渺然 提交于 2019-12-01 23:50:21
LeetCode 53. 最大子序和 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解法: 一:暴力求解 class Solution: def maxSubArray(self, nums: List[int]) -> int: tmp = nums[0] max_ = tmp n = len(nums) for i in range(1,n): # 当当前序列加上此时的元素的值大于tmp的值,说明最大序列和可能出现在后续序列中,记录此时的最大值 if tmp + nums[i]>nums[i]: max_ = max(max_, tmp+nums[i]) tmp = tmp + nums[i] else: #当tmp(当前和)小于下一个元素时,当前最长序列到此为止。以该元素为起点继续找最大子序列,

LeetCode53 最大子序和

假如想象 提交于 2019-12-01 23:42:13
LeetCode53 最大子序和 题目 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 分析 做这题是因为当时算法课讲到动态规划,所以先做做动态规划的简单题。这题容易理解,但是我一直没太懂哪里用到了动态规划。动态规划不是先求解小问题,然后大问题利用小问题求得的解来继续运算。直到求得问题的解 代码 class Solution { public: int maxSubArray(vector<int>& nums) { int sum=0; int ans=nums[0]; for(int i=0;i<nums.size();i++){ if(sum>0){ sum+=nums[i]; }else{ sum=nums[i]; } ans=ans>sum?ans:sum; } return ans; } };

【LeetCode】19. Remove Nth Node From End of List

落花浮王杯 提交于 2019-12-01 22:53:40
Difficulty: Medium More: 【目录】LeetCode Java实现 Description https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Given a linked list, remove the n -th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Follow up: Could you do this in one pass? Intuition Using a dummyHead. Solution public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode n0 = head; ListNode n1 =

LeetCode初级算法--链表02:合并两个有序链表

空扰寡人 提交于 2019-12-01 20:26:05
LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法、机器学习干货 csdn: https://blog.csdn.net/baidu_31657889/ csdn: https://blog.csdn.net/abcgkj/ github: https://github.com/aimi-cn/AILearners 一、引子 这是由LeetCode官方推出的的经典面试题目清单~ 这个模块对应的是探索的初级算法~旨在帮助入门算法。我们第一遍刷的是leetcode推荐的题目。 查看完整的剑指Offer算法题解析请点击github链接: github地址 二、题目 将两个有序链表合并为一个新的 有序链表 并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 1、思路 首先我们看到的数一个有序的链表,所以我们可以先比较两个链表长度相等的部分,按照顺序进行排列,对于剩下一个链表的部分,直接插入到最终的链表中,详细过程见代码。 2、编程实现 python # Definition for singly-linked list. # class ListNode(object): # def __init__(self

LeetCode初级算法--链表01:反转链表

人盡茶涼 提交于 2019-12-01 20:25:10
LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法、机器学习干货 csdn: https://blog.csdn.net/baidu_31657889/ csdn: https://blog.csdn.net/abcgkj/ github: https://github.com/aimi-cn/AILearners 一、引子 这是由LeetCode官方推出的的经典面试题目清单~ 这个模块对应的是探索的初级算法~旨在帮助入门算法。我们第一遍刷的是leetcode推荐的题目。 查看完整的剑指Offer算法题解析请点击github链接: github地址 二、题目 反转一个单链表。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表。你能否用两种方法解决这道题? 1、思路 这个题对我来说还是有点难度了,其实原理不难,我们我们使用三个指针,分别指向当前遍历到的结点、它的前一个结点以及后一个结点。 在遍历的时候,做当前结点的尾结点和前一个结点的替换。 因为这个题目之前在刷LeetCode的时候已经做过详细的图解说明 大家看链接就可以: https://blog.csdn.net/baidu_31657889/article