- 博客(37)
- 收藏
- 关注
原创 算法分析与设计第十九周: 567. Permutation in String
class Solution(object): def checkInclusion(self, s1, s2): l1 = [0 for _ in range(26)] l2 = [0 for _ in range(26)] size1 = len(s1) size2 = len(s2) if size2 <
2017-07-05 13:28:11
264
转载 Lua学习笔记之function type
Here we list all functions and types from the C API in alphabetical order. Each function has an indicator like this:[-o, +p, x]The first field, o, is how many elements the function pops from
2017-07-05 10:33:52
446
转载 Lua学习笔记之Coroutine
2.11 – CoroutinesLua supports coroutines, also called collaborative multithreading. A coroutine in Lua represents an independent thread of execution. Unlike threads in multithread systems, however
2017-07-04 20:59:26
288
转载 Lua学习笔记之Expressions
Both function calls and vararg expressions can result in multiple values. If an expression is used as a statement (only possible for function calls (see §2.4.6)), then its return list is adjusted to
2017-07-04 19:33:18
263
原创 课程调度问题:LeetCode 630. Course Schedule III
题目描述: There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be fini
2017-06-26 14:19:47
1426
原创 算法分析与设计第十八周:592. Fraction Addition and Subtraction
class Solution(object): def fractionAddition(self, expression): def gcd(a, b): a = abs(a) b = abs(b) if a == 0 or b == 0: return 1
2017-06-26 10:24:45
330
原创 8.8 精确4SAT问题证明
思路: 若一个问题为NP问题,要证明该问题是NPC问题,只需要将一个已知的NPC问题规约为该问题即可。 规约过程分两步:将已知的NPC问题的实例转化为该问题的实例。将已知的NPC问题的解转化为该问题的解。已知3SAT问题是NPC问题,所以 利用3SAT问题进行规约证明。 证明过程:4精确SAT问题跟3SAT问题都是NP问题规约过程: 给定一个3SAT问题的实例:X={x1,x2,
2017-06-13 17:18:27
485
原创 算法分析与设计第十七周:611. Valid Triangle Number
class Solution {public: int triangleNumber(vector<int>& nums) { //排序数组 sort(nums.begin(), nums.end()); //删除零元素 while (1) { //若数组中边长个数少于3,则直接返回0
2017-06-12 15:51:21
284
原创 算法分析与设计第十六周:582. kill process
class Solution(object): def killProcess(self, pid, ppid, kill): """ :type pid: List[int] :type ppid: List[int] :type kill: int :rtype: List[int] """
2017-06-05 15:40:24
274
原创 算法分析与设计第十五周:593. Valid Square
import mathclass Solution(object): def validSquare(self, p1, p2, p3, p4): #计算两点之间距离的函数 def dist(p1, p2): return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
2017-05-27 15:07:43
213
原创 算法分析与设计第十四周:424. Longest Repeating Character Replacement
class Solution(object): def characterReplacement(self, s, k): if s == "": return 0 char = [0 for _ in range(26)] i, j = 0, 0 maxCnt = 1 for c in s
2017-05-22 17:21:30
223
原创 算法分析与设计第十三周:582. Kill Process
class Solution(object): def killProcess(self, pid, ppid, kill): dict1 = {} size = len(pid) res = [kill] for i in range(size): if dict1.get(ppid[i]) == Non
2017-05-15 17:55:55
289
原创 算法分析与设计第十二周:473. Matchsticks to Square
Description: Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchstic
2017-05-08 21:49:20
309
原创 【0-1 knapsack】 474. Ones and Zeroes
Description: In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.For now, suppose you are a dominator of m 0s and n 1s respectively. On
2017-05-03 16:50:51
193
原创 560. Subarray Sum Equals K
Description: Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Solution:暴力列举所有可能的子连续序列和计算前n个序列的和,并储存与map中,再根据nums[0:i] - nu
2017-05-02 16:53:34
581
原创 算法分析与设计第十一周:416. Partition Equal Subset Sum
思路: 1.对数组进行排序并取得数组的总和 2.从大到小对数组进行筛选 3.只要有一种可行方案即返回Trueclass Solution(object): def canPartition(self, nums): if len(nums) <= 1: return False sum1 = sum(nums) nu
2017-05-01 23:30:07
193
原创 491. Increasing Subsequences
class Solution(object): def findSubsequences(self, nums): #先将list转成tuple #然后将tuple保存在set中 #并利用set来判断list是否已存在 self.__res = set() def rec(nums, l, cnt, val):
2017-04-25 21:13:31
150
原创 算法分析与设计第十周:384. Shuffle an Array
class Solution(object): def __init__(self, nums): """ :type nums: List[int] """ self.__l = nums self.__size = len(nums) def reset(self): """
2017-04-24 14:20:32
289
原创 算法分析与设计第九周:45. Jump Game II
#DP,从前往后扫描,依次更新到达每个位置所需要的步数class Solution(object): def jump(self, nums): size = len(nums) if size <= 1: return 0 steps = [size + 1 for _ in range(size)]
2017-04-17 22:14:33
171
原创 算法分析与设计第八周:300. Longest Increasing Subsequence
class Solution(object): def lengthOfLIS(self, nums): if nums == []: return 0 d = {1 : nums[0]} maxLength = 1 for num in nums[1:]: added = Fal
2017-04-10 17:40:47
161
原创 算法分析与设计第七周:134. Gas Station
题目简介: There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to
2017-04-01 16:19:55
254
原创 算法分析与设计第六周:236. Lowest Common Ancestor of a Binary Tree
class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { p_father.clear(); q_father.clear(); //为两个目标节点寻找father findFather(
2017-03-28 00:52:19
168
原创 算法分析与设计第五周习题:95. Unique Binary Search Trees II
与96题的对比: 95题是96题Unique Binary Search Trees的升级版,95题增加了构建出所有树的要求,而不仅仅是计算树的数量。对于96题,我们可以轻易得出树数量的规律。即:1.长度为3的树可由根和一个长度为2的子树构成(即以3为根或者以1为根) 或者 由根和两个长度为1的子树构成( 即以2为根) 2. 长度为4的子树可由根和一个长度为3的子树构成(即以4
2017-03-21 20:04:39
326
原创 算法分析与设计第四周:513. Find Bottom Left Tree Value
class Solution(object): __maxLevel = 0 __maxLeft = 0 def findBottomLeftValue(self, root): level = 0 def rec(root, level): if root != None: level
2017-03-18 18:10:35
222
原创 算法分析与设计第三周习题:图论算法之310. Minimum Height Trees
今天刚上完算法课,课上知识内容包括拓扑排序和图论一些基本知识,所以我就去leetcode上面找了一道有关图论的题目来巩固自己所学的知识。题目简介: For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree.
2017-03-09 22:27:08
239
原创 算法分析与设计第二周习题:分治算法之P215,P169,P53
题目链接 题目名称:215. Kth Largest Element in an Array 题目描述: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Fo
2017-03-05 20:36:40
459
原创 分治算法之归并排序
归并排序分两步: 第一步:将数组递归分为两部分。 第二步:将两部分数组递归合并为一个数组。def mergeSort(l1): print('mergeSort:',l1) length = len(l1) if length == 1: return l1 mid = int(length / 2) l = [] merge(me
2017-03-01 21:31:31
221
原创 算法分析与设计第一周习题:2. Add Two Numbers
题目链接 题目名称:2. Add Two Numbers 题目描述: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single di
2017-02-24 17:56:05
315
原创 memo
除以上较为普遍的算法以外,还有多种由协同过滤算法衍生出来的算法。利用均值平方差(Cacheda,Carneiro,Fernández,&For-moso,2011)进行协同过滤的算法,还有使用模糊集合论(fuzzy set theory)为不同的相似度赋权值的算法(Lu,Shambour,Xu,Lin,andZhang,2013)。还有学者将熵(entropy)、稀疏度(sparsity)、神经网
2016-04-13 15:28:36
240
原创 最小生成树--PRIM
#define MAXVALUE 100000//以1节点作为缺省开始搜索的节点void prim(int** graph, int vertices, int startPoint = 1) { int minEdge = MAXVALUE, foundV = 1; int length = vertices; bool* visited = new bool[lengt
2015-09-02 16:00:49
392
原创 最小生成树-Kruskal
#include <iostream>using namespace std;#define MAXVALUE 100000//顶点以1为最小点//保存数组中以0为最小点void kruskal(int** graph, int vertices) { int length = vertices, foundV = 0, run = 0; //set用于记录每个顶点属于哪个集合
2015-09-02 11:26:30
350
原创 最小二叉堆
最小二叉堆#include <iostream>#include <vector>using namespace std;//模板函数的实现与声明要写在一个文件中template <class comparable>class binaryMinHeap {public: binaryMinHeap(); void insert(const comparable &value)
2015-09-01 13:31:14
546
原创 AVL平衡二叉树
#include <iostream>#include <cmath>#include <algorithm>#include <time.h>using namespace std;#define DEFAULTVALUE (-1)struct avlNode { int height; int value; avlNode* left; avlNode* r
2015-08-28 18:02:36
273
原创 二叉搜索树
二叉搜索树#include <iostream>using namespace std;#define length 10#define errorValue -1int num;struct node { node* left; node* right; int value; node(int a = errorValue, node* n1 = NULL, no
2015-08-28 15:26:29
278
原创 关于线程同步的实现机制---busy waiting
第一种线程同步机制:只利用一个全局变量来判断是否有线程正在使用critical section,这样就有可能出现,两个线程同时判断当前没有线程正在使用critical section的情况,从而同时进入了critical section。 实际结果输出:偶尔出现加减交替的情况,即偶尔出现了两个线程同时进入critical section的情况。#include <stdio.h>#include
2015-08-20 16:07:00
8081
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人