Leetcode算法
文章平均质量分 55
讲解leetcode算法解题思路,总结最优方案
hanruikai
aaa
展开
-
LeetCode总结数学转换类题目
罗马字符转换为数字题目:https://leetcode.com/problems/roman-to-integer/武器:抓住关键点,L,X,C三种减法的情况,其他都为加法结果:一次通过Excel列转换为数字题目:https://leetcode.com/problems/excel-sheet-column-number/武器:找出规律,比如BZ,('B'-'A'+1)...原创 2019-01-17 14:15:01 · 544 阅读 · 0 评论 -
一片文章说明白分布式系统NRW,Paxos,Raft
引言今天看到一篇文章提高分布式系统中service宕机的问题,其中作者提到了分布式系统中的一个基本问题。假如有两个service A和B,每个service当然有很多instance。如果A调用B进行数据更新操作,B在接受后未处理前宕机了,怎么办?数据丢失了怎么办?我们从系统架构的演进来梳理一下各个架构时代这个问题的处理方法单节点时期最早的架构很简单,都是单节点。所有请求都由一个节...原创 2019-01-17 11:39:42 · 710 阅读 · 0 评论 -
LeetCode总结单链表相关题目
反转单链表题目:https://leetcode.com/problems/reverse-linked-list/武器:反转时候记录pre,nextTmp处理结果:一次通过合并两个有序链表题目:https://leetcode.com/problems/merge-two-sorted-lists/武器:遍历单链表结果:完美通过,有个小bug,比较两个元素大小,添加到...原创 2019-01-16 17:47:50 · 394 阅读 · 0 评论 -
LeetCode总结整型数组类题目
寻找唯一的单个数题目:https://leetcode.com/problems/single-number/武器:利用map计算次数,线性时间。但是需要O(n)存储空间结果:未通过新武器:XOR操作异或的运算方法是一个二进制运算:1^1=00^0=01^0=10^1=1两者相等为0,不等为1.(1) a ^ b = b ^ a;(2) (a ^ b)...原创 2019-01-16 16:52:47 · 468 阅读 · 0 评论 -
LeetCode总结二叉树类题目
二叉树最大深度题目:https://leetcode.com/problems/maximum-depth-of-binary-tree/武器:二叉树,不是BST。思路一样。既然是深度,利用BFS,因为广度优先搜索是每层扫描的。结果:一次通过有序数组构建平衡二叉树题目:https://leetcode.com/problems/convert-sorted-array-to-b...原创 2019-01-16 16:29:11 · 653 阅读 · 0 评论 -
LeetCode总结字符串操作类题目
反转字符串题目:https://leetcode.com/problems/reverse-string/武器:两个指针二次结果:一次pass判断是否互为变位词题目:https://leetcode.com/problems/valid-anagram/武器:hashmap统计第一词语每个字符次数,遍历第二个,每个次数减去1,不需要判断是否存在。最后map有不为0,则fal...原创 2019-01-16 16:21:15 · 1493 阅读 · 0 评论 -
LeetCodeMedium篇Decode Ways
题目A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given anon-emptystring containing only digits, determi...原创 2019-01-16 11:57:28 · 225 阅读 · 0 评论 -
LeetCodeMEDIM篇3Sum
题目Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not ...原创 2019-01-15 11:44:59 · 247 阅读 · 0 评论 -
LeetCodeMEDIM篇Validate Binary Search Tree
题目Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key. ...原创 2019-01-14 16:17:43 · 252 阅读 · 0 评论 -
LeetCodeMedium篇Maximum Product Subarray
题目Given an integer arraynums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation:[2,...原创 2019-01-11 18:52:19 · 254 阅读 · 0 评论 -
LeetCodeMEDIM篇 Find First and Last Position of Element in Sorted Array
题目Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).If...原创 2019-01-08 15:20:25 · 265 阅读 · 0 评论 -
LeetCodeMEDIM篇Course Schedule II
题目There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a ...原创 2019-01-08 12:16:38 · 297 阅读 · 0 评论 -
LeetCodeMEDIM篇 Sort List
题目Sort a linked list inO(nlogn) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output:...原创 2019-01-08 11:18:39 · 249 阅读 · 0 评论 -
LeetCodeMEDIM篇Evaluate Reverse Polish Notation
题目Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Note:Division between two integ...原创 2019-01-09 20:01:26 · 245 阅读 · 0 评论 -
LeetCodeMEDIM篇Basic Calculator II
题目Implement a basic calculator to evaluate a simple expression string.The expression string contains onlynon-negativeintegers,+,-,*,/operators and empty spaces. The integer division should...原创 2019-01-09 19:38:19 · 243 阅读 · 0 评论 -
LeetCodeMEDIM篇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 fo...原创 2019-01-09 18:48:33 · 202 阅读 · 0 评论 -
LeetCodeMedium篇Coin Change
题目You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of mo...原创 2019-01-11 17:33:22 · 227 阅读 · 0 评论 -
LeetCodeMEDIM篇word search
题目Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertical...原创 2019-01-11 15:49:45 · 288 阅读 · 0 评论 -
LeetCodeMEDIM篇Word Break
题目Given anon-emptystringsand a dictionarywordDictcontaining a list ofnon-emptywords, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.Note:...原创 2019-01-07 20:01:39 · 310 阅读 · 1 评论 -
LeetCodeMedium篇 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 en...原创 2019-01-04 15:35:48 · 176 阅读 · 0 评论 -
LeetCodeMedium篇Lowest Common Ancestor of a Binary Tree
题目Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two node...原创 2019-01-04 10:14:01 · 153 阅读 · 0 评论 -
LeetCodeMedium篇Merge Intervals
题目Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] over...原创 2019-01-03 19:41:30 · 147 阅读 · 0 评论 -
LeetCodeMEDIM篇Implement Trie (Prefix Tree)
题目Implement a trie withinsert,search, andstartsWithmethods.Example:Trie trie = new Trie();trie.insert("apple");trie.search("apple"); // returns truetrie.search("app"); // returns ...原创 2019-01-03 16:55:04 · 226 阅读 · 0 评论 -
LeetCodeMedium篇Course Schedule
题目There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a ...原创 2019-01-03 11:40:14 · 249 阅读 · 0 评论 -
LeetCodeMedium篇Construct Binary Tree from Preorder and Inorder Traversal
题目Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, givenpreorder =[3,9,20,15,7]inorder = ...原创 2019-01-02 15:35:10 · 138 阅读 · 0 评论 -
LeetCodeMedium篇 Set Matrix Zeroes
题目Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do itin-place.Example 1:Input: [ [1,1,1], [1,0,1], [1,1,1]]Output: [ [1,0,1], [0,0,0], [1,0,1]]...原创 2019-01-02 14:39:43 · 199 阅读 · 0 评论 -
LeetCodeMedium篇Palindrome Partitioning
题目Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.Example:Input:"aab"Output:[ ["aa","b"], ["a"...原创 2019-01-02 10:43:14 · 207 阅读 · 0 评论 -
LeetCodeMedium篇Number of Islands
题目Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may as...原创 2018-12-29 17:14:22 · 216 阅读 · 0 评论 -
LeetCodeMedium篇Increasing Triplet Subsequence
题目Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there existsi, j, ksuch thatarr[i]<...原创 2018-12-29 15:47:37 · 179 阅读 · 0 评论 -
LeetCodeMedium篇Letter Combinations of a Phone Number
题目Given a string containing digits from2-9inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is...原创 2018-12-28 17:50:23 · 261 阅读 · 0 评论 -
LeetCodeMedium篇Binary Tree Zigzag Level Order Traversal
题目Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary...原创 2018-12-28 16:43:17 · 163 阅读 · 0 评论 -
LeetCodeMedium篇 Perfect Squares
题目Given a positive integern, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum ton.Example 1:Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exa...原创 2018-12-28 15:53:02 · 265 阅读 · 0 评论 -
LeetCodeMedium篇Kth Smallest Element in a Sorted Matrix
题目Given anxnmatrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, n...原创 2018-12-28 14:17:31 · 153 阅读 · 0 评论 -
LeetCodeMedium篇Longest Increasing Subsequence
题目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,...原创 2018-12-27 18:41:14 · 194 阅读 · 0 评论 -
LeetCodeMedium篇Find Peak Element
题目A peak element is an element that is greater than its neighbors.Given an input arraynums, wherenums[i] ≠ nums[i+1], find a peak element and return its index.The array may contain multiple pe...原创 2018-12-27 16:49:32 · 245 阅读 · 0 评论 -
LeetCode刷题Medium篇Container With Most Water
题目Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find...原创 2018-12-27 15:18:35 · 228 阅读 · 0 评论 -
LeetCode刷题Medium篇Group Anagrams
题目Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat&q原创 2018-12-26 14:03:04 · 264 阅读 · 0 评论 -
java算法篇排序之快速排序
思路1、确定轴元素,一般轴元素选择第一个或者最后一个,所以轴元素不是low就是high指针2、左侧指针和右侧指针,循环移动,左侧寻找比轴大的元素,右侧寻找比轴小的元素3、如果pl<pr,循环执行,反之停止循环4、交换轴元素(其实就是low指针位置的元素)和r[pr]5、递归处理左侧和右侧序列图片摘自https://blog.csdn.net/IT_ZJYANG/a...原创 2012-04-16 21:12:02 · 638 阅读 · 0 评论 -
LeetCodeMEDIM篇Kth Largest Element in an Array
题目Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6,4] and k = 2Output: 5...原创 2018-12-25 18:07:40 · 227 阅读 · 0 评论 -
LeetCodeMedium篇Unique Paths---动态规划
题目A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach th...原创 2018-12-25 16:42:24 · 207 阅读 · 0 评论