![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode解题
我没有开挂
这个作者很懒,什么都没留下…
展开
-
LeetCode133.Clone Graph
题目Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s undirected graph serialization: Nodes are labeled uniquely.We use # as a separator for each node,原创 2017-09-24 17:13:54 · 322 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters(用动态规划)
题目网站题目链接 题目简述:给出字符串s,找出s中最长的不存在重复字符的子串的长度给出 "abcabcbb", 答案子串是 "abc", 返回长度 3给出 "bbbbb" , 答案子串是 "b" , 返回长度 1给出 "pwwkew" , 答案子串是 "wke", 返回长度 3思路区分子串和子序列 例如原始串s : abcabcbb 子序列: abbbb 、cbcb、abc 等均原创 2017-12-03 23:30:49 · 354 阅读 · 0 评论 -
LeetCode 55. Jump Game
相关题目leetcode 45. Jump Game 2 扩展版:搜狐2017秋招题——袋鼠过河题目Jump Game题目简述:给出一个非负数数组,每个元素代表能跳的最大步数,问能否从第一个点开始连续跳跃最终到达最后一个点。 如: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false.思路时间复杂度O(n)若我们能到达的最远处原创 2017-12-06 01:42:45 · 261 阅读 · 0 评论 -
LeetCode 746. Min Cost Climbing Stairs(动态规划)
题目题目地址题目大意:跳阶梯,选择从第0或者第1级阶梯开始,每次可以向上跳一到两级,问到达阶梯顶部的最小代价。数组元素 cost[i] 表示在第 i 级阶梯向上跳需要花费的代价。注:这里到达顶部的意思不是指站在最高级阶梯处,需要跳出最高阶。例子 1: 输入:cost = [10, 15, 20] 输出: 15 解析: 开始点选择cost[1],花费15代价,跳两级,即可跳出该原创 2017-12-24 13:03:34 · 291 阅读 · 0 评论 -
LeetCode 198. House Robber (动态规划)
题目题目描述:给一个数组代表一排房子价值,要求小偷不能偷相邻的两间房子,求他能偷取的最大总值。思路每个房子都有两种状态,要么被偷,要么安全; 若前一个房子被偷了,那么现在到达的房子只能为安全状态; 若前一个房子安全,那么现在到达的房子有被偷和安全两种可能。为房子先定义两个对应的状态数组rob[i] :走到房子i处,并且偷了nums[i],所获得的最大总值 safe[i] :走到房子i处,但是不原创 2017-12-25 13:05:46 · 570 阅读 · 0 评论 -
LeetCode 714. Best Time to Buy and Sell Stock with Transaction FeeI(动态规划)
题目题目原文题目大意:给一个非负数组,每个元素代表股票当天价格,每次出售股票需要付出固定的小费,求多次购入与出售股票后最终得到的最大利益。注意:再次购买之前必须先出售股票,即不能同时拥有两支股票Example 1: Input: prices = [1, 3, 2, 8, 4, 9], fee = 2 Output: 8Explanation: The maximum profit原创 2017-12-16 16:52:40 · 836 阅读 · 0 评论 -
LeetCode 474. Ones and Zeroes(动态规划)
题目大意:给出一个全为01串的字符串数组,每个串都会消耗一定数量的0和1,分别给出0和1的总个数,要求在消耗完给出的01资源之前找出最多的字符串。Example 1: Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 Output: 4 Explanation: This are totally 4原创 2017-12-29 13:00:21 · 549 阅读 · 0 评论 -
LeetCode 670. Maximum Swap(动态规划结合贪心)
题目leetcode源题 给一个非负整数[0, 10^8],求至多交换一次后得到的最大值。 Example 1: Input: 2736 Output: 7236 Explanation: Swap the number 2 and the number 7. Example 2: Input: 9973 Output: 9973原创 2018-01-15 19:46:17 · 593 阅读 · 0 评论 -
LeetCode 376. Wiggle Subsequence(动态规划)
题目给出一个整数序列,求摆动子序列,即子序列相邻两位之间的差值是正负交替出现的,即序列中相邻两个数一大一小交替出现。Examples:Input: [1,7,4,9,2,5]Output: 6The entire sequence is a wiggle sequence.Input: [1,17,5,10,13,15,10,5,16,8]Output: 7There a原创 2018-01-23 21:16:21 · 299 阅读 · 0 评论 -
算法课习题证明
题目:吝啬SAT问题是这样的:给定一组子句(每个子句都是其中文字的析取)和整数k,求一个最多有k个变量为true的满足赋值——如果该赋值存在。证明吝啬SAT是NP-完全问题。SAT问题:假设有子句: (a⋃b⋃c)⋂(a⋃b¯)⋂(b⋃c¯)(a¯⋂c)⋂(a¯⋃b¯⋃c¯) 求a,b,c的取值(true or false)使得该表达式的结果是true。不难发现,这个表达式不存原创 2018-01-08 18:31:11 · 368 阅读 · 0 评论 -
LeetCode 868. Binary Gap (位运算)
题目思路规定了范围是正数,直接右移,按位与取出最低位,高位补0没影响代码class Solution {public: int binaryGap(int N) { //左移 int maxDis = 0, bit = 0, lastOne = -1, isOne = 0; while(N) { ...原创 2018-07-19 21:47:09 · 437 阅读 · 0 评论 -
LeetCode 120. Triangle 动态规划
题目源题Triangle一个正立三角形,上层的点只能接触下层与其相邻的点,求一条从顶层到底层的路径,使路径上所有点之和最小。例子: [ [2], [3,4], [6,5,7], [4,1,8,3] ]上面这三角形自顶向下的最小路径和为 11 ( 2 + 3 + 5 + 1 = 11)思路采用动态规划,但是有两种不同方式原创 2017-11-04 02:28:17 · 307 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray 贪心或动态规划
题目Maximum Subarray给定一个整数数组nums[n],让我们找出其相邻元素构成的子串的最大和。例如:数组 [-2, 1,- 3, 4, -1, 2, 1, -5, 4], 其子串 [4,-1,2,1] 具有最大和为 6。思路状态:f[i] 表示以 元素nums[i] 结尾的子串的最大和 状态转移:f[i+1] = max {f[i] + nums[i+1], nums[i+1]}代码原创 2017-11-03 23:39:41 · 375 阅读 · 0 评论 -
LeetCode1.Two Sum
题目Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same原创 2017-09-17 14:13:48 · 284 阅读 · 0 评论 -
LeetCode167.Two Sum II - Input array is sorted
题目Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers s原创 2017-09-17 14:16:58 · 301 阅读 · 0 评论 -
LeetCode65. Valid Number
题目Validate if a given string is numeric.Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => trueNote: It is intended for the problem statement to be ambiguous. You s原创 2017-09-09 16:22:05 · 383 阅读 · 0 评论 -
LeetCode8. String to Integer (atoi)
题目Implement atoi to convert a string to an integer.Requirements for atoi: The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then原创 2017-09-09 17:04:41 · 237 阅读 · 0 评论 -
LeetCode371. Sum of Two Integers
题目Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example: Given a = 1 and b = 2, return 3.思路运用数字电路里的加法器原理,异或得和,相与得进位两个数按位异或得到对应的各位和sum,按位与得到各位产生的进位tempC原创 2017-09-09 20:40:01 · 330 阅读 · 0 评论 -
LeetCode445. Add Two Numbers II
题目You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return原创 2017-09-10 00:34:28 · 278 阅读 · 0 评论 -
LeetCode2. 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 digit. Add the two numbers and return原创 2017-09-10 00:41:17 · 240 阅读 · 0 评论 -
LeetCode 202.Happy Number 利用存储结构或快慢指针
题目A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number eq原创 2017-10-22 11:02:58 · 398 阅读 · 0 评论 -
LeetCode15. 3Sum
题目给一个包含N个整数的数组S,求解是否存在三个元素a, b, c使得a + b + c = 0.注意:解集中不能包含重复的解[a, b, c]例如, 数组S = [-1, 0, 1, 2, -1, -4],解集是:{ [-1, 0, 1], [-1, -1, 2] }思路当遇到数a,我们需要寻找两个数的和 b + c = -a,即3Sum变成了2Sum问题如 未排序的Two Sum 和 已排序的原创 2017-10-15 20:33:25 · 289 阅读 · 0 评论 -
LeetCode 338. Counting Bits(动态规划)
题目思路每个二进制数i分两种情况,①最低位为0 若最低位为0,则dp[i] = dp[i >> 1]; ②最低位为1 若最低位为1,则dp[i] = dp[i - 1] + 1;代码class Solution {public: vector<int> countBits(int num) { //若最低位为0,...原创 2018-07-19 23:21:58 · 226 阅读 · 0 评论