
算法学习笔记
wuqingdeqing
生如逐放 心有焰藏
展开
-
LeetCode 205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the order of characters. No two原创 2020-10-24 21:55:08 · 350 阅读 · 0 评论 -
LeetCode 182. Duplicate Emails
SQL SchemaWrite a SQL query to find all duplicate emails in a table named Person.±—±--------+| Id | Email |±—±--------+| 1 | a@b.com || 2 | c@d.com || 3 | a@b.com |±—±--------+For example, your query should return the following for the above原创 2020-10-01 10:07:37 · 322 阅读 · 0 评论 -
LeetCode 172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Follow up: Could you write a solution that works in logarithmic time complexity?Example 1:Input: n = 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: n = 5Output: 1Expl原创 2020-09-25 21:48:12 · 319 阅读 · 1 评论 -
LeetCode 169. Majority Element
Given 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 and the majority element always exist in the array.Example 1:Input: [3,2,3]Output:原创 2020-09-23 21:20:45 · 158 阅读 · 0 评论 -
LeetCode 167. Two Sum II - Input array is sorted
Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers ...原创 2019-12-19 09:46:52 · 234 阅读 · 0 评论 -
LeetCode 153. Find Minimum in Rotated Sorted Array
153.Find Minimum in Rotated Sorted ArrayMedium1413192FavoriteShareSuppose 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 b...原创 2019-12-18 22:06:51 · 125 阅读 · 0 评论 -
LeetCode 151. Reverse Words in a String
Given an input string, reverse the string word by word.Example 1:Input: "the sky is blue"Output:"blue is sky the"Example 2:Input: " hello world! "Output:"world! hello"Explanation:...原创 2019-12-18 10:41:21 · 122 阅读 · 0 评论 -
LeetCode 144. Binary Tree Preorder Traversal
Given a binary tree, return thepreordertraversal of its nodes' values.Example:Input:[1,null,2,3] 1 \ 2 / 3Output:[1,2,3]Follow up:Recursive solution is trivial, could...原创 2019-12-10 18:54:54 · 120 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integerposwhich represents the position (0-indexed)in the linked list where tail co...原创 2019-12-07 08:35:48 · 216 阅读 · 0 评论 -
LeetCode 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5...原创 2019-11-15 10:59:43 · 114 阅读 · 0 评论 -
LeetCode 9. Palindrome Number
Determine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExpl...原创 2019-07-24 09:46:59 · 128 阅读 · 0 评论 -
LeetCode 700. Search in a Binary Search Tree
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node...原创 2019-03-22 17:02:52 · 124 阅读 · 0 评论 -
LeetCode 657. Robot Return to Origin
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robotends up at (0, 0)after it completes its moves.The move sequence is represe...原创 2019-03-11 09:44:33 · 167 阅读 · 0 评论 -
LeetCode 905. Sort Array By Parity
Given an arrayAof non-negative integers, return an array consisting of all the even elements ofA, followed by all the odd elements ofA.You may return any answer array that satisfies this conditi...原创 2019-03-07 10:07:17 · 147 阅读 · 0 评论 -
LeetCode 48. Rotate Image
You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the imagein-place, which means you have to modify the input 2D matrix d...原创 2019-09-02 10:03:43 · 112 阅读 · 0 评论 -
LeetCode 709. To Lower Case
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1:Input: "Hello"Output: "hello"Example 2:Input: "here"Output: "here"...原创 2019-02-05 10:27:06 · 138 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
Given a string, find the length of thelongest substringwithout repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:...原创 2019-07-19 16:02:57 · 107 阅读 · 0 评论 -
LeetCode 1. Two Sum
Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would haveexactlyone solution, and you may not use thesame...原创 2019-07-17 12:16:02 · 134 阅读 · 0 评论 -
LeetCode 19. Remove Nth Node From End of List
Given a linked list, remove then-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, t...原创 2019-08-05 12:22:20 · 104 阅读 · 0 评论 -
LeetCode 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2019-08-19 11:09:43 · 130 阅读 · 0 评论 -
LeetCode 27. Remove Element
Given an arraynumsand a valueval, remove all instances of that valuein-placeand return the new length.Do not allocate extra space for another array, you must do this bymodifying the input arra...原创 2019-08-09 22:04:07 · 109 阅读 · 0 评论 -
LeetCode 74. Search a 2D Matrix
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each row...原创 2019-10-08 12:03:57 · 122 阅读 · 0 评论 -
LeetCode 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear onlyonce.Example 1:Input: 1->1->2Output: 1->2Example 2:Input: 1->1->2->3->3Output: 1-...原创 2019-10-09 10:43:55 · 89 阅读 · 0 评论 -
八大排序算法(六)——优先队列、堆和堆排序
6.1 API优先队列是一种抽象数据类型,它表示了一组值和对这些值的操作。优先队列最重要的操作就是删除最大元素和插入元素。6.2 初级实现6.2.1 数组实现(无序)或许实现优先队列最简单方法就是基于下压栈的代码。insert()方法的代码和栈的完全一样。要实现删除最大元素,可以添加一段类似选择排序的内循环的代码,将最大元素和边界元素交换然后删除它,和栈的pop()方法的实现一样。和栈类似,也可以...原创 2018-05-15 11:22:44 · 450 阅读 · 1 评论 -
LeetCode 929. Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign.For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.Besides lowercase l...原创 2019-02-04 13:46:25 · 383 阅读 · 0 评论 -
LeetCode 789. Escape The Ghosts
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], gh...原创 2019-01-29 09:47:57 · 158 阅读 · 0 评论 -
LeetCode 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in Sis a type of stone you have. You want to know how many of the ston...原创 2019-02-03 10:38:54 · 121 阅读 · 0 评论 -
LeetCode 202.Happy Number
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 o...原创 2018-06-14 13:27:51 · 230 阅读 · 0 评论 -
LeetCode 744.Find Smallest Letter Greater Than Target
Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target.Letters also...原创 2018-06-07 10:06:24 · 167 阅读 · 0 评论 -
LeetCode 268.Missing Number
Given 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]O...原创 2018-05-30 09:41:04 · 261 阅读 · 0 评论 -
LeetCode 674.Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).Example 1:Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing ...原创 2018-06-12 10:18:16 · 217 阅读 · 0 评论 -
LeetCode 796.Rotate String
We are given two strings, A and B.A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one...原创 2018-05-15 11:50:17 · 179 阅读 · 0 评论 -
八大排序算法(四)——归并排序
要将一个数组排序,可以先将它分成两半分别排序,然后将结果归并起来。这就是归并排序。归并排序的主要优势是它能保证将任意长度为n的数组排序所需时间与nlogn成正比,它的主要缺点则是它所需的额外空间与n成正比。4.1 原地归并原地排序先将前半部分排序,再将后半部分排序,然后在数组中移动元素而不需要使用额外的空间。public static void merge(Comparable[] a, int ...原创 2018-05-06 16:33:46 · 516 阅读 · 0 评论 -
八大排序算法(三)——希尔排序
希尔排序是一种快速的基于插入排序的排序算法。对于大规模乱序数组插入排序很慢,因为它只会交换相邻的元素,因此元素只能一点一点从数组的一端移动到另一端。希尔排序为了加快速度简单改进了插入排序,交换不相邻的元素以对数组的局部进行排序,并最终用插入排序将局部有序的数组排序。希尔排序的思想是使数组中任意间隔为h的元素都是有序的。这样的数组被称为h有序数组。即一个h有序数组就是h个互相独立的有序数组编织在一起...原创 2018-04-27 14:53:00 · 397 阅读 · 0 评论 -
八大排序算法(五)——快速排序
快速排序可能是应用最广泛的排序算法。快速排序流行的原因是因为它实现简单、适用于各种不同的输入数据且在一般应用中比其他排序算法都要快的多。快速排序的特点包括它是原地排序(只需要一个很小的辅助栈),且将长度为n的数组排序所需的时间和nlogn成正比。快速排序的内循环比大多数排序算法都要短小,这意味着无论是理论上还是实际上都要更快。它的主要缺点是非常脆弱,在实现时要非常小心才能避免低劣的性能,许多错误都...原创 2018-05-11 18:12:24 · 665 阅读 · 0 评论 -
LeetCode 766.Toeplitz Matrix
今日刷到LeetCode766题,判断给定的矩阵是否托普利兹矩阵。托普利兹矩阵,简称为T型矩阵。托普利兹矩阵的主对角线上的元素相等,平行于主对角线的线上的元素也相等;矩阵中的各元素关于次对角线对称,即T型矩阵为次对称矩阵。本人所写代码如下:class Solution { public boolean isToeplitzMatrix(int[][] matrix) { ...原创 2018-05-11 10:57:28 · 444 阅读 · 0 评论 -
八大排序算法(二)——插入排序
与选择排序一样,当前索引左边的所有元素都是有序的,但它们的最终位置还不确定,为了给更小的元素腾出空间,它们可能会被移动。当索引到达数组右端时,数组排序就完成了。和选择排序不同的是,插入排序所需的时间取决于输入元素的初始位置。例如,对一个很大且其中的元素已经有序的数组进行排序将会比对随机顺序的数组或是逆序数组进行排序要快得多。对于随机排列的长度为N且主键不重复的数组,平均情况下插入排序需要~(N^2...原创 2018-04-26 14:26:30 · 306 阅读 · 0 评论 -
LeetCode 628.Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6 Example 2:Input: [1,2,3,4]Output: 24 Note:The...原创 2018-06-01 15:47:32 · 181 阅读 · 0 评论 -
LeetCode 551.Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. A student could be re...原创 2018-06-01 17:30:35 · 182 阅读 · 0 评论 -
LeetCode 921. Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', and in any positions ) so that the resulting parentheses string is valid.Formally, a parentheses s...原创 2018-12-30 21:08:32 · 270 阅读 · 0 评论