随笔分类 -  算法与数据结构

基本的算法代码
41. First Missing Positive
摘要:Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 阅读全文

posted @ 2020-09-13 16:31 wsw_seu 阅读(73) 评论(0) 推荐(0)

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 阅读全文

posted @ 2020-09-13 14:47 wsw_seu 阅读(128) 评论(0) 推荐(0)

154. Find Minimum in Rotated Sorted Array II(循环数组查找)
摘要: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]). F 阅读全文

posted @ 2020-09-13 14:01 wsw_seu 阅读(101) 评论(0) 推荐(0)

局部最小值(二分)
摘要:局部最小存在的几种情况,1. 长度为1,arr[0]就是局部最小;2. 数组的开头,如果arr[0] < arr[1] ,arr[0]被定义为局部最小。 3. 数组的结尾,如果arr[N-1] < arr[N-2] ,arr[N-1]被定义为局部最小。 所以剩下就是数组下标1~N-2之间的了。再按a 阅读全文

posted @ 2020-09-13 12:13 wsw_seu 阅读(1142) 评论(0) 推荐(0)

92. Reverse Linked List II 翻转链表II
摘要:Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Out 阅读全文

posted @ 2020-09-13 11:00 wsw_seu 阅读(82) 评论(0) 推荐(0)

leetcode Reverse Nodes in k-Group翻转链表K个一组
摘要:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to 阅读全文

posted @ 2020-09-13 10:44 wsw_seu 阅读(163) 评论(0) 推荐(0)

字符串循环移位(2次翻转的思路)
摘要:给定一个字符串S[0…N-1],要求把S的前k个字符移动到S的尾部,如把字符串“abcdef”前面的2个字符‘a’、‘b’移动到字符串的尾部,得到新字符串“cdefab”:即字符串循环左移k。 算法要求: 时间复杂度为 O(n),空间复杂度为 O(1)。 利用翻转两次的算法思路: 假设字符串长度为n 阅读全文

posted @ 2020-09-07 22:44 wsw_seu 阅读(391) 评论(0) 推荐(0)

翻转字符串(坑比较多)
摘要: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! 阅读全文

posted @ 2020-09-06 18:23 wsw_seu 阅读(168) 评论(0) 推荐(0)

(字符串)子串变位词
摘要:题目: 给定两个串a和b,问b是否是a的子串的变位词,例如输入a=hello,b=lel,lle,ello都是true,但b=elo是false。(字串是连续的) 思路: 滑动窗口思想:动态维护一个“窗口”,比如b的长度是3,考察a[0..2],a[1..3],a[2..4]是否是b的变位词,关键在 阅读全文

posted @ 2020-09-06 17:16 wsw_seu 阅读(137) 评论(0) 推荐(0)

299. Bulls and Cows
摘要:You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time 阅读全文

posted @ 2020-07-26 21:11 wsw_seu 阅读(114) 评论(0) 推荐(0)

面试题 02.04. 分割链表
摘要:编写程序以 x 为基准分割链表,使得所有小于 x 的节点排在大于或等于 x 的节点之前。如果链表中包含 x,x 只需出现在小于 x 的元素之后(如下所示)。分割元素 x 只需处于“右半部分”即可,其不需要被置于左右两部分之间。 示例: 输入: head = 3->5->8->5->10->2->1, 阅读全文

posted @ 2020-07-26 19:48 wsw_seu 阅读(183) 评论(0) 推荐(0)

295. Find Median from Data Stream
摘要:Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two 阅读全文

posted @ 2020-07-26 19:23 wsw_seu 阅读(198) 评论(0) 推荐(0)

动态规划题解(转)
摘要:动态规划算法似乎是一种很高深莫测的算法,你会在一些面试或算法书籍的高级技巧部分看到相关内容,什么状态转移方程,重叠子问题,最优子结构等高大上的词汇也可能让你望而却步。 而且,当你去看用动态规划解决某个问题的代码时,你会觉得这样解决问题竟然如此巧妙,但却难以理解,你可能惊讶于人家是怎么想到这种解法的。 阅读全文

posted @ 2020-07-26 18:16 wsw_seu 阅读(334) 评论(0) 推荐(0)

利用c++ std::getline实现split
摘要:getline reads characters from an input stream and places them into a string: getline从输入流中读取字符, 并把它们转换成字符串. getline(input, str, delim), 默认的分隔符是’\n’字符. 阅读全文

posted @ 2020-07-25 16:29 wsw_seu 阅读(1211) 评论(0) 推荐(0)

leetcode 30day--2
摘要:202. Happy Number Write an algorithm to determine if a number n is "happy". A happy number is a number defined by the following process: Starting with 阅读全文

posted @ 2020-07-22 21:17 wsw_seu 阅读(120) 评论(0) 推荐(0)

leetcode 30day--1
摘要:Single Number Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have 阅读全文

posted @ 2020-07-22 21:00 wsw_seu 阅读(150) 评论(0) 推荐(0)

525. Contiguous Array
摘要:Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0 阅读全文

posted @ 2020-07-22 20:50 wsw_seu 阅读(162) 评论(0) 推荐(0)

字符串匹配算法之Sunday算法(转)
摘要:字符串匹配算法之Sunday算法 背景 我们第一次接触字符串匹配,想到的肯定是直接用2个循环来遍历,这样代码虽然简单,但时间复杂度却是Ω(m*n),也就是达到了字符串匹配效率的下限。于是后来人经过研究,构造出了著名的KMP算法(Knuth-Morris-Pratt算法),让我们的时间复杂度降低到了O 阅读全文

posted @ 2020-03-08 18:21 wsw_seu 阅读(508) 评论(0) 推荐(0)

负载均衡之加权轮询算法(转)
摘要:一:轮询算法(Round-Robin) 轮询算法是最简单的一种负载均衡算法。它的原理是把来自用户的请求轮流分配给内部的服务器:从服务器1开始,直到服务器N,然后重新开始循环。 算法的优点是其简洁性,它无需记录当前所有连接的状态,所以它是一种无状态调度。 假设有N台服务器:S = {S1, S2, … 阅读全文

posted @ 2019-08-11 20:46 wsw_seu 阅读(6195) 评论(0) 推荐(0)

如何理解汉诺塔的递归?
摘要:作者:魏闪链接:https://www.zhihu.com/question/24385418/answer/107705695来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 一个环:Step1.将最大的环从A移动到CA -> C 两个环:Step1.把除了最大的环之 阅读全文

posted @ 2018-04-12 14:35 wsw_seu 阅读(185) 评论(0) 推荐(0)

导航