算法
文章平均质量分 66
普通网友
这个作者很懒,什么都没留下…
展开
-
ip最长匹配原则的一种实现
路由转发时需要根据最长原则进行转发,但是RFC并没有给出算法具体是如何实现的,为此自己胡乱想了个算法。首先定义两个数据结构class IP: ipArr,mask = [0,0,0,0],0 def __init__(self,ipStr): self.ipArr,self.mask = tranStrIP(ipStr) def ipToNumbe...原创 2019-12-03 22:47:20 · 922 阅读 · 0 评论 -
LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.这个题开始想了想,以为直接loop就行了,但是又一想好歹是个中等难度,有这么简单?管他呢就简单loop。果然==Time Limit Exceeded。那有什么骚操作呢?就参考了别人的解法。有这么一个思路。说结果原创 2017-07-02 10:17:22 · 186 阅读 · 0 评论 -
LeetCode 23 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.合并k个有序链表。最直接的想法先合并两条链表,然后再与第三条链表进行合并,那么时间复杂度是o(2n+3n……kn)=o(nk²)另一种想法是借鉴归并排序的思想,两两合并。T(n)=2T(n原创 2017-07-01 12:19:05 · 204 阅读 · 0 评论 -
LeetCode 401 Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit o原创 2017-07-01 12:15:09 · 262 阅读 · 0 评论 -
LeetCode 532 K-diff Pairs in an Array
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原创 2018-01-23 14:38:58 · 183 阅读 · 0 评论 -
LeetCode 122 Best Time to Buy and Sell Stock II
这个题和Best Time to Buy and Sell Stock 是有关的。先看下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.If you were only permitt原创 2018-01-23 14:56:35 · 137 阅读 · 0 评论 -
LeetCode 718 Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1:Input:A: [1,2,3,2,1]B: [3,2,1,4,7]Output: 3Explanation: The repeated sub原创 2018-01-28 23:32:54 · 221 阅读 · 0 评论 -
LeetCode 667 Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3,原创 2018-01-21 00:05:38 · 194 阅读 · 0 评论 -
LeetCode 287 Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi...原创 2018-02-15 09:58:47 · 173 阅读 · 0 评论 -
LeetCode 621 Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could b...原创 2018-02-15 10:08:10 · 177 阅读 · 0 评论 -
LeetCode 689 Maximum Sum of 3 Non-Overlapping Subarrays
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.Return the result ...原创 2018-02-15 10:24:21 · 311 阅读 · 0 评论 -
LeetCode 153 Find Minimum 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).Find the minimum element.You may assume no duplicate exis...原创 2018-02-15 11:23:17 · 159 阅读 · 0 评论 -
LeetCode 496 Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. T...原创 2018-02-18 18:31:18 · 185 阅读 · 0 评论 -
前缀表达式,中缀表达式,后缀表达式的一点微小理解
文章目录中缀表达式前缀表达式前缀求值中缀变前缀前缀变中缀后缀表达式中缀变后缀后缀变中缀后缀求值中缀表达式 中缀即我们平时用的数学表达式,其递归定义为中缀表达式 运算符 中缀表达式。举例:1+2,(1+2)*(3+4).这种表达式便于直观理解但是不方便计算机计算。后来有波兰人发明了前缀,后缀表达式。前缀表达式也叫波兰式,后缀表达式也叫逆波兰式。前后缀表达式经常作为栈在优先级的应用的例题。...原创 2019-05-11 18:53:13 · 1941 阅读 · 2 评论 -
LeetCode 541 Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of th原创 2018-01-14 17:08:10 · 216 阅读 · 0 评论 -
LeetCode 448 Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.原创 2017-12-23 22:45:14 · 143 阅读 · 0 评论 -
LeetCode 137 Single Number II
1.题目Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtime complexity原创 2017-07-01 11:47:53 · 215 阅读 · 0 评论 -
LeetCode 136 Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2017-07-01 11:52:13 · 163 阅读 · 0 评论 -
LeetCode 382 Linked List Random Node
Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up:What if the linked list is extremely large and i原创 2017-07-01 11:52:59 · 236 阅读 · 0 评论 -
LeetCode 392 Is Subsequence
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string,原创 2017-07-01 11:53:57 · 176 阅读 · 0 评论 -
LeetCode 64 Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at a原创 2017-07-01 11:55:06 · 170 阅读 · 0 评论 -
LeetCode 377 Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.Example:nums = [1, 2, 3]target = 4The poss原创 2017-07-01 11:58:30 · 156 阅读 · 0 评论 -
LeetCode 46 Permutations
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]全排原创 2017-07-01 12:02:52 · 183 阅读 · 0 评论 -
LeetCode 22 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[“((()))”,“(()())”,“(())()”,“()(())”,原创 2017-07-01 12:04:30 · 171 阅读 · 0 评论 -
LeetCode 70 Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a posi原创 2017-07-01 12:08:37 · 177 阅读 · 0 评论 -
LeetCode 526 Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i原创 2017-07-01 12:12:57 · 210 阅读 · 0 评论 -
两个版本的快速排序
算法导论版和严蔚敏版原创 2017-12-11 10:45:30 · 343 阅读 · 0 评论 -
堆排序(Python)
堆排序的Python实现原创 2017-12-11 11:50:39 · 352 阅读 · 0 评论 -
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原创 2018-01-21 16:36:30 · 182 阅读 · 0 评论