
算法
wotu__
这个作者很懒,什么都没留下…
展开
-
使用信号量实现没有饥饿问题的锁 (假设信号量为 weak semaphores)
实现没有饥饿问题的锁。实现算法: Morris's solution。 It uses two turnstiles to create two waiting rooms before the critical section.The mechanism works in two phases. During the first phase, the first turnstile...原创 2019-12-09 15:00:13 · 532 阅读 · 0 评论 -
LeetCode-365-水壶问题 (不定次方程的整数解)
365. Water and Jug ProblemDescriptionYou are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to m...原创 2019-12-09 11:52:57 · 348 阅读 · 0 评论 -
260. Single Number III (位运算)
LeetCode-260.Single Number IIIDescriptionGiven an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that a...原创 2019-11-22 13:43:24 · 302 阅读 · 0 评论 -
LeetCode.208 实现 Trie (字典树的实现)
LeetCode.208实现一个字典树 TrieTrie: 百度百科结点类型的定义在我的实现中, 用了两种不太一样的结点类型.Node1视频教程地址: Trie Data Structure (EXPLAINED)(演示不断插入每个字符之后树的变化)两个成员变量:map, 用于接受一个字符, 并得到对应的下一个孩子结点isLeaf, 指定当前是否到达叶子结点除此之...原创 2019-11-14 13:03:54 · 276 阅读 · 0 评论 -
LeetCode.207 LeetCode.210 (有向无环图拓扑排序的实现)
210. Course Schedule II207. Course ScheduleDescriptionThere are a total of n courses you have to take, labeled from 0 to n-1.Some courses may have prerequisites, for example to take course 0 you h...原创 2019-11-14 12:18:36 · 680 阅读 · 0 评论 -
200. Number of Islands (并查集)
200. Number of IslandsDescriptionGiven a 2d grid map of '1's (land) and '0's (water), count thenumber of islands. An island is surrounded by water and is formedby connecting adjacent lands horizon...原创 2019-11-12 13:44:52 · 270 阅读 · 0 评论 -
187. Repeated DNA Sequences (位运算)
LeetCode-187.Repeated DNA SequencesDescriptionAll DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to i...原创 2019-11-11 21:51:35 · 207 阅读 · 0 评论 -
人工智能实验一: 搜索算法问题求解
一、实验目的了解4种无信息搜索策略和2种有信息搜索策略的算法思想; 能够运用计算机语言实现搜索算法; 应用搜索算法解决实际问题(如罗马尼亚问题); 学会对算法性能的分析和比较二、实验的硬件、软件平台硬件:计算机 软件:操作系统:WINDOWS/Linux 应用软件:C,Java或者MATLAB三、实验内容及步骤使用搜索算法实现罗马尼亚问题的求解 (从出发点 Ar...原创 2019-11-11 21:09:52 · 1597 阅读 · 0 评论 -
栈实现树的前序, 中序, 后序遍历
前序遍历:Code:vector<int> inorderTraversal(TreeNode* root) { // 中序遍历. vector<int> retvec; stack<TreeNode*> stk; while (root != NULL || !stk.empty()) { // 注意循环进行的条...原创 2019-10-25 17:44:48 · 774 阅读 · 0 评论 -
LeetCode-394. 字符串解码
题目链接Leetcode-394. 字符串解码题目描述 :给定一个经过编码的字符串,返回它解码后的字符串。编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。此外,你可以认为原始数据不包含数字,所有的数...原创 2019-06-04 15:35:05 · 1019 阅读 · 0 评论 -
Leetcode- 347( 桶排序 或 快排思想)
题目链接 LeetCode-347题目描述给定一个非空的整数数组,返回其中出现频率前 k 高的元素。示例 1:输入: nums = [1,1,1,2,2,3], k = 2输出: [1,2]思路一根据自己的做法,使用一个列为2的二维数组来存储 <元素, 个数>。根据二维数组的第二列**(元素个数), 利用快速排序对二维数组进行排序。取出二维数组中前 k 个**的 元素...原创 2019-05-29 17:25:58 · 1127 阅读 · 1 评论 -
LeetCode - 236 二叉树的最近公共祖先
原题url:236. Lowest Common Ancestor of a Binary Tree根据自己的思路写出来的代码性能太差。网上的解答一:class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root ==...原创 2019-05-15 17:46:42 · 185 阅读 · 0 评论 -
Leetcode-231 位运算判断一个整数是否是 2 的幂次
如果一个数是2的n次方,那么这个数对应的二进制表示中只有一位是1,其余都是0.因此判断一个数是否是2的n次方,可以转换为判断这个数对应的二进制表示是否只有一位为1.如果一个数的二进制表示只有一位是1, 如 num = 00010000, 那么 num-1 的二进制为 num-1 = 00001111由于num与num-1二进制表示中每一位都不相同,因此 num & (num-1) 的...原创 2019-05-15 15:32:50 · 417 阅读 · 0 评论 -
最长公共子序列算法思想的应用
最长公共子序列解答:public class Test1 { public static void main(String[] args) { char[] chars1 = "ABCBDAB".toCharArray(); char[] chars2 = "BDCABA".toCharArray(); LCS(chars1, cha...原创 2019-05-19 15:47:13 · 1675 阅读 · 0 评论 -
leetcode-3 滑动窗口做法
class Solution { public int lengthOfLongestSubstring(String s) { int[] dict = new int[128]; int len = s.length(); if(len <= 1) return len; int re = 0, l = 0, r ...原创 2019-04-24 16:47:18 · 454 阅读 · 0 评论 -
leetcode-309: 最佳买卖股票时机含冷冻期
参考博客:LeetCode 309: 一个很清晰的DP解题思路生动形象,图解一目了然题目传送:309. 最佳买卖股票时机含冷冻期类似于《逻辑与计算机设计基础》中的状态图,根据状态图写出转化方程:s[i] = Math.max(s[i-1], sell[i-1]);buy[i] = Math.max(buy[i-1], s[i-1]-prices[i]);sell[i] =...原创 2019-04-28 23:37:58 · 234 阅读 · 0 评论 -
优化0-1背包问题
public static int max_back() { // 0-1 背包优化,使用一维数组来实现 int[] weight = new int[]{2, 2, 6, 5, 4}; int[] value = new int[]{6, 3, 5, 4, 6}; int Max = 10; // 最大容量 i...原创 2019-04-25 19:21:04 · 365 阅读 · 0 评论 -
算法中快慢指针的应用(Java)
何为快慢指针?快慢指针就是指两个指针在移动的过程中,慢的指针每次移动一步,而快的指针每次移动两步。快慢指针的应用一 (判断一个链表是否有环):问题来源:leetcode-141题,环形链表 https://leetcode-cn.com/problems/linked-list-cycle/解法一:在这道题中,我首先想到的办法也是哈希表。遍历所有的结点,并在哈希表中存储每个结点...原创 2019-03-29 16:53:51 · 2480 阅读 · 1 评论