只用于个人的学习和总结. 178. Rank Scores 一.表信息 二.题目信息 对上表中的成绩由高到低排序,并列出排名.当两个人获得相同分数时,取并列名次,且名词中无断档. Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should…
602. Friend Requests II: Who Has the Most Friends[M] 一.表信息 In social network like Facebook or Twitter, people send friend requests and accept others' requests as well. table:request_accepted 二.题目信息 找出拥有好友数最多的用户编号及其拥有的好友数.所有的请求默认都被处理了. 注意: 只有一个用户拥有最多的…
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = faster.next.next; slower = slower.next; 2 应用 2.1. 用来判断链表是否有环以及寻找环入口 Linked List Cycle Linked List Cycle II 是否有环:快慢指针思想,注意循环条件:(fast != null) && (fas…
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node w…
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3] 解法一 要解该题,只需要在上一题(leetcode解题报告(1):Remove Dupli…
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等) 题目链接 思路 想切割,要有首尾两个指针,确定了结尾指针,就能确定下一个切割的开始指针. 遍历字符串,如果已扫描部分的所有字符,都只出现在已扫描的范围内,即可做切割. 注意 : 贪心的思想为,只要是扫描过的字符,都出现在我扫描的范围之类,我就切割,不去考虑其他的条件,这样能保证切割的数量最多 代…
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                              本文地址 LeetCode:Reverse Words in a String LeetCode:Evaluate Reverse Polish Notation LeetCode:Max Points on a Line LeetCode:Sort List LeetCode:Ins…
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the…
位运算是我最近才开始重视的东西,因为在LeetCode上面刷题的时候发现很多题目使用位运算会快很多.位运算的使用包含着许多技巧(详细可以参考http://blog.csdn.net/zmazon/article/details/8262185),但我仅仅在大一学C语言入门的时候接触过,很多东西都不了解,因此我在这篇文章里面稍微总结一下我在LeetCode遇到的关于位运算的题目,当然仅仅只是一部分,因此这篇文章可能会在我每次遇到位运算的题目时更新一下. 首先要回忆一下有哪些位运算的操作: 按位与…
终于刷完了leetcode的前250道题的easy篇.好吧,其实也就60多道题,但是其中的套路还是值得被记录的. 至于全部code,请移步github,题目大部分采用python3,小部分使用C,如有问题和建议,欢迎指正. String 有一个string库,可以返回各种string的汇总,很值得用. 当题目中需要实现字符串替代的时候,python中有一个自带的translate()函数可以实现这个功能,具体可见Python3字符串替换replace(),translate(),re.sub()…
刷完题后,看一下其他人的solution,受益匪浅. 可以按不同的topic刷题,比如数组.字符串.集合.链表等等.先做十道数组的题,接着再做十道链表的题. 刷题,最主要的是,学习思路. 多刷几遍.挑面试常考的重点刷. 可以采用兔系刷题,就是直接看答案,因为很多算法如果事先不知道是很难靠自己想出来的,知道了思路,刷第二遍的时候就不用看答案了. 龟系刷题,就是自己慢慢磨,花费时间多些,难得也更难忘. 个人习惯用java.当然,练习新的语言时,比如学python时也可以刷题练手. 基础 Char 1…
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" Example…
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at some pivot(枢轴; 中心点) unknown to you beforehand(提前; 事先). (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If foun…
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Ex…
1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Now given a string represented by several bits. Return whether the last…
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sort()可实现,而本文则着重探讨关于KSum问题. leetcode求和问题描写叙述(K sum problem): K sum的求和问题通常是这样子描写叙述的:给你一组N个数字(比方 vector num), 然后给你一个常数(比方 int target) ,我们的goal是在这一堆数里面找到K个数…
描述 Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.…
前言 LeetCode题目个人答案(Golang版) 本篇预期记录 1-10 题, 持续更新 正文 1.两数之和(简单) https://leetcode-cn.com/problems/two-sum/ 题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使用两遍. 示例 给定 nums = [2, 7, 11, 15], target = 9 因为 n…
122. Best Time to Buy and Sell Stock II Say you have an array 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 complete as many transactions as you like (ie, buy one and sell on…
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor…
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 这种问题 其实大多数人都做烂了 但是在实际应用中这个问题应用很广泛 有用二进制数基本就要处理加减乘除 一种最容易理解的方法但是不高效 那就是转为十进制处理后再转…
18. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note For example, given array S = [1, 0, -1, 0, -2, 2], and targ…
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &…
16.3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given…
Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. Example 1: Input: s = "aaabb", k = 3 Output: 3 The longest substring is "aaa"…
1 题目描述 Write an algorithm to determine if a number 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 process un…
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 解题:参考网上大神做法,解题如下: class Solution: def get_palindromic(self, s, k, l): s…
题目:there are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)) 题解: 1.自己想得思路:构建一个list,然后比较各数的大小,将其插入到合适的位置 class Solution: # @param {integer[…
Insertion Sort List Sort a linked list using insertion sort. leetcode subject思路:标准的插入排序.考察一下链表的操作. 对链表进行插入排序的正确方法是:新建一个头节点,遍历原来的链表,对原链表的每个节点找到新链表中适合插入位置的前指针,然后执行插入操作.这种操作链表的题的技巧是:新建一个dummy作为head node,然后把数据插入到dummy的链表中,最后返回dummy.next. 链表的插入排序图示: 注意头结点…
尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasing Subsequence 问题:https://leetcode.com/problems/longest-increasing-subsequence/ 分析:http://zering.me/2016/09/02/Longest-Increasing-Subsequence/ 源码:http…