![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 87
srping123
这个作者很懒,什么都没留下…
展开
-
LeetCode 458.Poor Pigs
这一题感觉更像是数学题 题目要求如下 There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 m转载 2017-08-21 10:09:22 · 244 阅读 · 0 评论 -
Leetcode DP198.House Robber&53. Maximum Subarray
198. House Robber题目要求如下 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each转载 2017-08-22 16:57:33 · 239 阅读 · 0 评论 -
LeetCode 205. Isomorphic Strings
题目要求如下 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be rep转载 2017-08-28 15:06:29 · 159 阅读 · 0 评论 -
LeetCode Combination Sum系列
Combination Sum系列 第一题是基础,给定一个数组和一个target,求有多少种组合可以加成target。运用回溯法,注意,每次add进结果里的数组需要newclass Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { ...原创 2018-06-08 10:26:57 · 212 阅读 · 0 评论 -
算法课1-oj输入输出那点事
第一种,简单输入 #include&amp;amp;amp;amp;lt;iostream&amp;amp;amp;amp;gt; using namespace std; int a,b; cin&amp;amp;amp;amp;gt;&amp;amp;amp;amp;gt;a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;gt;b;原创 2018-09-23 11:42:25 · 202 阅读 · 0 评论 -
算法课2-算法课第一次作业总结
A:单词翻转总时间限制: 1000ms 内存限制: 65536kB描述:输入一个句子(一行),将句子中的每一个单词翻转后输出。输入:只有一行,为一个字符串,不超过500个字符。单词之间以空格隔开。输出:翻转每一个单词后的字符串,单词之间的空格需与原文一致。样例输入hello world样例输出olleh dlrow这道题算是首次用c++在oj上刷题,一开始调用了r...原创 2018-09-23 15:39:43 · 1774 阅读 · 0 评论 -
算法课3-堆和优先队列
堆的性质堆是一个完全二叉树(最后一层的节点都在左边)一般都用数组来表示堆,i结点的父结点下标就为(i – 1) / 2。它的左右子结点下标分别为2 * i + 1和2 * i + 2建堆 O(N) 合并两个堆的时间复杂度和建堆相同把元素插入堆和把某元素删除,时间复杂度O(logn)每次插入都是将先将新数据放在数组最后,由于从这个新数据的父结点到根结点必然为一个有序的序列,将新数据与其...原创 2018-10-14 11:14:38 · 152 阅读 · 0 评论 -
算法课6-并查集Union Find
为了提高查找速度,三种方法:Link by sizeLink by rank(height)Path Compression并查集的典型应用:Percolation总时间限制: 1000ms 内存限制: 32768kB描述定义一个N行N列的矩阵,矩阵中的每个元素是个方格,每个方格有两种可能的状态:开通的或关闭的。初始时,所有方格都是关闭的。输入数据的每一步会指...原创 2018-11-01 20:25:59 · 642 阅读 · 0 评论 -
算法课7-Divide and Conquer
分治的思想,最经典的两个例子首先是排序的两个例子一个是快排 一个是归并排序【请看到这里的你,去复习一下排序算法】【这里填充快排和归并排序代码】...原创 2018-11-09 09:40:41 · 512 阅读 · 0 评论 -
算法课8-Dynamic Programming⭐️
动态规划是常考算法,将指数级的问题降到多项式级别。动态规划和分治法都是将问题划分成子问题进行求解,它们的区别主要是:分治法的子问题无重叠动态规划的子问题有重叠,并且重叠的个数是指数级别的动态规划和贪心法的相同之处是原问题包含子问题的最优解,而它们的区别在于:贪心法只看局部最优,最终达到全局最优对于动态规划局部最优不一定是全局最优1. Weighted interval ...原创 2018-11-18 10:54:16 · 346 阅读 · 0 评论 -
算法课9-dynamic programming II
背包问题串讲背包问题没有办法用贪心法得到最优解,都是通过动态规划来实现最优解。只有gold dust模型可以用贪心法得到最优解(性价比最高实现)。0-1背包问题【每个物品只可以拿一次】OPT(i, w) = max profit subset of items 1, …, i with weight limit w.它是伪多项式时间的解法,NPC问题解释看这里 0-1背包为什么有多...原创 2018-11-27 21:14:45 · 222 阅读 · 0 评论 -
LeetCode(六) DP 70.Climbing Stairs&552. Student Attendance Record II
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?转载 2017-08-18 16:18:35 · 224 阅读 · 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 num转载 2017-09-22 15:35:40 · 155 阅读 · 0 评论 -
LeetCode 67. Add Binary
题目要求如下 Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”.一开始想的是转化成十进制一加,再转化回来就行,写了这个class Solution { public String addBinary(Stri转载 2017-09-05 20:48:35 · 267 阅读 · 0 评论 -
LeetCode 643. Maximum Average Subarray I 滑动窗口
题目要求如下 Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximum average value. Example转载 2017-08-22 11:11:12 · 210 阅读 · 0 评论 -
LeetCode 栈和队列的互换
225. Implement Stack using Queues栈是先进后出,队列是先进先出,要使用队列实现栈,应该在每次添加元素的时候,将队列整个颠倒,使得删除元素时满足栈的结构。class MyStack { private Queue<Integer>q; /** Initialize your data structure here. */ public MySta转载 2017-09-07 10:45:53 · 332 阅读 · 0 评论 -
LeetCode(一)--461.Hamming Distance&476.Number Complement 位运算
Hamming Distance 题目要求如下 The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hammi转载 2017-08-11 17:02:25 · 201 阅读 · 0 评论 -
LeetCode(二)563. Binary Tree Tilt&404.Sum of Left Leaves&108.Convert Sorted Array to BST
Binary Tree Tilt题目要求如下 Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values an转载 2017-08-11 17:31:42 · 168 阅读 · 0 评论 -
LeetCode(三)HashMap169. Majority Element&350. Intersection of Two Arrays II
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转载 2017-08-13 11:29:47 · 186 阅读 · 0 评论 -
LeetCode(四)链表206. Reverse Linked List&2. Add Two Numbers
206. Reverse Linked List翻转一个链表 解法如下/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class转载 2017-08-13 11:43:39 · 160 阅读 · 0 评论 -
LeetCode(五)387. First Unique Character in a String&409. Longest Palindrome
387. First Unique Character in a String题目要求如下 Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: s = “leetcode”转载 2017-08-13 12:37:58 · 186 阅读 · 0 评论 -
LeetCode(七)HashSet 202. Happy Number
202. Happy Number题目要求如下 Write an algorithm to determine if a number is “happy”. A happy number is a number defined by the following process: Starting with any positive integer, replace the nu转载 2017-08-18 16:32:43 · 209 阅读 · 0 评论 -
LeetCode(八)415. Add Strings
题目要求如下 Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 cont转载 2017-08-18 16:39:31 · 174 阅读 · 0 评论 -
LeetCode(九)231. Power of Two&405. Convert a Number to Hexadecimal
231.Power of Two题目要求判断一个数是否是2的乘方。 可以使用循环求解,也可以用位运算如下public class Solution { public boolean isPowerOfTwo(int n) { return n>0 && ((n & (n-1)) == 0); }}使用如下代码可以求解一个数的二进制中1的个数 int ans = 0转载 2017-08-18 17:20:41 · 184 阅读 · 0 评论 -
算法课10-dynamic programming III
String similarity如何衡量两个字符串的相似度?引入edit distance的概念 δ+αp,q\delta + \alpha_{p,q}δ+αp,q gap+mismatch 即,填充一个空格或者把pq对应位置的字母换成一致,最终使得两个字符串达到一致需要最小化这个距离Hirschberg算法可将空间压缩到θ(m+n)\theta(m+n)θ(m+n)单汇最短路径...原创 2018-12-05 20:25:37 · 208 阅读 · 0 评论