随笔分类 - Algorithms
LeetCode解题报告—— Word Search & Subsets II & Decode Ways
摘要:1. 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 cel
阅读全文
LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
摘要:1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Example: 思路:这题感觉有点没有说清楚,如果k大于了链表长度该怎么办呢?这个时候怎么确定旋转位置呢?
阅读全文
LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence
摘要:1. Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represen
阅读全文
LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix
摘要:1. Group Anagrams Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Note: All inputs
阅读全文
LeetCode解题报告—— Permutations & Permutations II & Rotate Image
摘要:1. Permutations Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have the following permutations: 思路;直接使用
阅读全文
LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
摘要:1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the cand
阅读全文
LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
摘要:1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at some pivot(枢轴; 中心点) unknown to you beforehand(提前; 事先
阅读全文
LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
摘要:1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the li
阅读全文
LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
摘要:1. 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 ar
阅读全文
LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
摘要:1. 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
阅读全文
LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
摘要: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. E
阅读全文
LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum
摘要: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 represe
阅读全文
笔试算法题记录2
摘要:1. 给定一组元素个数不定的字符串数组,每个字符串的长度不定;请统计出该字符串数组中的英文字母子串、数字子串和其他字符子串的总数; 输出为以","符号分隔3个数值,分别代表英文字母子串、数字子串和其他字符子串的数量; 实现时无需考虑非法输入。 输入描述:输入为:字符串数组 例子:abcd1244!!
阅读全文
笔试算法题记录1
摘要:判断给定的前序遍历序列能否构成合法的二叉树 #带表空节点,如3## 表示根节点是3,左子树和右子树都是空的。这题关键是考虑空节点的情况。将合法的左子树和右子树变为空节点再这样递归考虑,最后只剩一个#时即为合法情况,需要注意的是如果#入栈后栈中出现了连续3个#号的话,则表明 字符串非法。 测试了几组数
阅读全文
动态规划算法实现部分——0/1背包问题
摘要:代码: 测试: 0/1 背包问题可以扩展变形到其他问题,举个栗子: 两个cpu,如果其中一个分摊的时间越接近sum/2那么总的处理时间便越小,转换成0/1背包问题,n个任务,一个容量为sum/2的袋子(cpu),求这个袋子能装的最大重量。这题我在做的时候,有两种动态规划的解法: 从物品重量到sum/
阅读全文
算法课堂笔记6—近似算法
摘要:第六章 近似算法 一. 近似算法的基本概念 首先什么是NP-完全问题(这部分参考博文:http://blog.csdn.net/xueyong4712816/article/details/6509592):NP=Non-deterministic Polynomial,也就是多项式复杂程度的非确定
阅读全文
动态规划算法实现部分——求公共最长子序列以及构造回文需要删除的最小字符个数
摘要:上学期的期末考为了应付考试,只是把每种算法都理解了一遍,但是并没有具体的代码实现。现在开始抽空慢慢实现这些算法。 代码实现: 结果: 这里有个算法的衍生,就是求构造回文需要删除的最小字符个数。其实只需要将一个字符串和它的逆序字符串求公共最长子串就可以了。
阅读全文
算法课堂笔记5—摊还分析
摘要:第五章 摊还分析 1. 什么是摊还分析? 对一个数据结构(例如栈)执行n个操作,其中有的操作代价高,有的操作代价低,有些操作代价中等。而摊还分析是用来评价程序中的一个操作序列的平均代价,有时可能某个操作的代价特别高,但总体上来看也并非那么糟糕,可以形象的理解为把高代价的操作“分摊”到其他操作上去了,
阅读全文
算法课堂笔记4—贪心算法
摘要:第四章 贪心算法 这里需要注意的便是如何证明贪心算法得到的是最优解。 下面来一些例子: 1. 活动选择问题 现在感觉贪心算法真的是目前遇到过实现起来最简单的一个,但是问题在于用贪心算法得到的解是不是最优解呢?下面来证明: 第一步要证明问题具有最优子结构性质: 还是利用了这种先假设,再反证这种策略。对
阅读全文
动态规划——流水作业调度问题
摘要:问题: n个作业 N={1,2,…,n}要在2台机器M1和M2组成的流水线上完成加工。每个作业须先在M1上加工,然后在M2上加工。M1和M2加工作业 i 所需的时间分别为 ai 和bi,每台机器同一时间最多只能执行一个作业。 流水作业调度问题要求确定这n个作业的最优加工顺序,使得所有作业在两台机器上
阅读全文
浙公网安备 33010602011771号