![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Algorithms
文章平均质量分 58
StepForewardMonthly
STAY HUNGRY.STAY FOOLISH.
展开
-
Leetcode:169. Majority Element
Description: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-empty and the majority e原创 2017-09-14 22:31:03 · 157 阅读 · 0 评论 -
Leetcode:718. Maximum Length of Repeated Subarray
DescriptionGiven two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repea原创 2017-12-10 16:49:52 · 168 阅读 · 0 评论 -
Leetcode:121. Best Time to Buy and Sell Stock
Description:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the原创 2017-12-02 23:51:23 · 114 阅读 · 0 评论 -
Leetcode:714. Best Time to Buy and Sell Stock with Transaction Fee
DescriptionYour are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.You may complete原创 2017-12-13 14:02:46 · 379 阅读 · 0 评论 -
Leetcode: 64. Minimum Path Sum
DescriptionGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or ri原创 2017-12-13 14:18:47 · 132 阅读 · 0 评论 -
Leetcode:70. Climbing Stairs
DescriptionYou 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?Note: Given n will be a po原创 2017-12-05 11:26:50 · 126 阅读 · 0 评论 -
Leetcode: 698. Partition to K Equal Sum Subsets
DescriptionGiven an array of integers nums and a positive integer k, find whether it’s possible to divide this array into k non-empty subsets whose sums are all equal.Example 1: Input: nums = [4,原创 2017-12-13 15:14:52 · 312 阅读 · 0 评论 -
Leetcode:690. Employee Importance
DescriptionYou are given a data structure of employee information, which includes the employee’s unique id, his importance value and his direct subordinates’ id.For example, employee 1 is the leader of原创 2017-12-24 22:25:21 · 195 阅读 · 0 评论 -
Leetcode:100. Same Tree
Description解题思路采用DFS递归式的查找比较,若有一个不相等则返回false/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x)原创 2017-12-25 23:52:46 · 157 阅读 · 0 评论 -
算法分析与设计:NP完全问题
8.12 The k-SPANNING TREE problem is the following.Input: An undirected graph G=(V,E) Output:A spanning tree of G in which each node has degree <= k,if such a tree exits. Show that for any k>= 2: (a)原创 2017-12-31 20:07:55 · 979 阅读 · 0 评论 -
Leetcode:Longest Common Prefix
DescriptionWrite a function to find the longest common prefix string amongst an array of strings.找到一个字符串数组中的最长公共前缀,遍历每个字符串数组,比较两个相邻的字符串前缀是否相同,若相同则加入prefix中,若不相同则返回prefix,代码如下class Solution {public:原创 2017-12-18 22:26:44 · 167 阅读 · 0 评论 -
Leetcode:17. Letter Combinations of a Phone Number
Description解题思路1.DFSclass Solution {public: vector<string> letterCombinations(string digits) { vector<string> result; string save; if (digits.size() == 0) return vector<str原创 2017-12-18 22:51:12 · 170 阅读 · 0 评论 -
Leetcode:210. Course Schedule II
DescriptionThere 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 have to first take course 1, which is expr原创 2018-01-03 13:24:17 · 196 阅读 · 0 评论 -
Leetcode:329. Longest Increasing Path in a Matrix
Description解题思路拓扑排序题,但用DFS算法更为简单class Solution {public: int DFS(vectorvectorint>>& matrix, int y, int x, int val, vectorvectorint>>& hash) { if(y 0 || y >= matrix.size() || x原创 2018-01-03 13:31:37 · 151 阅读 · 0 评论 -
Leetcode:486. Predict the Winner
DescriptionGiven an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a play原创 2018-01-03 13:54:06 · 198 阅读 · 0 评论 -
Leetcode:464. Can I Win
DescriptionIn the “100 game,” two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins.What if we change原创 2018-01-07 00:45:10 · 437 阅读 · 0 评论 -
Leetcode:375. Guess Number Higher or Lower II
DescriptionWe are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number I pick原创 2018-01-09 14:15:29 · 278 阅读 · 0 评论 -
Simple Tree Traverse(遍历)
遍历算法(the first node as the root node)1.先(根)序遍历的递归算法定义: 若二叉树非空,则依次执行如下操作: ⑴ 访问根结点; ⑵ 遍历左子树; ⑶ 遍历右子树。2.中(根)序遍历的递归算法定义: 若二叉树非空,则依次执行如下操作: ⑴遍历左子树; ⑵访问根结点; ⑶遍历右子树。3.后(根)序遍历得递归算法定义: 若原创 2016-05-16 23:30:44 · 1095 阅读 · 0 评论 -
Leetcode:740. Delete and Earn
Description解题思路挑选任意一个数字nums[i],得到nums[i]分,同时需要删除所有等于nums[i] - 1和nums[i] + 1的整数。求最大得分。用动态规划(Dynamic Programming)求解 dp[x]表示删除不大于x的所有数字的最大得分。c[x]存储数字x的个数。 状态转移方程: dp[x] = max(dp[x - 1], dp[x - 2] + c[x原创 2017-12-10 16:31:14 · 1266 阅读 · 0 评论 -
Leetcode:6. ZigZag Conversion
Description解题思路本题就利用一个字符串数组,每行一个字符串,每个字符串循环加入字符,最后将四个字符串累加到一个字符串中,返回结果。class Solution {public: string convert(string s, int numRows) { if (numRows <= 1) return s; int len = s.lengt原创 2017-12-09 22:49:01 · 138 阅读 · 0 评论 -
Leetcode:5. Longest Palindromic Substring
Description:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example: Input: “babad” Output: “bab”Note: “aba” is also a原创 2017-12-09 22:00:59 · 139 阅读 · 0 评论 -
Leecode:Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { int maxlen = 0, flag = -1; int count = 0; int length = s.length(); if (length == 1) return true;原创 2017-09-20 21:23:36 · 148 阅读 · 0 评论 -
Leetcode: Course Schedule
Description:There 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 have to first take course 1, which is exp原创 2017-09-23 20:16:30 · 139 阅读 · 0 评论 -
Leetcode:Maximum Subarray
Question:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1原创 2017-09-09 16:11:33 · 233 阅读 · 0 评论 -
Leetcode:493. Reverse Pairs
Description:Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j].You need to return the number of important reverse pairs in the given array. Example1: Inp原创 2017-10-08 23:25:16 · 400 阅读 · 0 评论 -
Leetcode:Median of Two Sorted Arrays
Description:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1:原创 2017-10-01 20:41:52 · 128 阅读 · 0 评论 -
Leetcode: 654. Maximum Binary Tree
Description:Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array. The left subtree is the maximum tree cons原创 2017-10-10 10:27:51 · 181 阅读 · 0 评论 -
Leetcode:303. Range Sum Query - Immutable(week 8)
Description:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -原创 2017-10-28 19:57:36 · 143 阅读 · 0 评论 -
Leetcode:2. Add Two Numbers(Week 6)
Description: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 a原创 2017-10-14 19:13:47 · 139 阅读 · 0 评论 -
Leetcode:198. House Robber(week 9)
Description: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 of them is that adj原创 2017-11-01 13:53:07 · 146 阅读 · 0 评论 -
Leetcode:657. Judge Route Circle (week 7)
Description:Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is re原创 2017-10-16 12:52:51 · 130 阅读 · 0 评论 -
Leetcode:455. Assign Cookies (week 7)
Description:Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum si原创 2017-10-16 13:08:17 · 157 阅读 · 0 评论 -
Leetcode:215. Kth Largest Element in an Array (week 10)
DescriptionFind 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. For example, Given [3,2,1,5,6,4] and k = 2, r原创 2017-11-11 14:15:41 · 125 阅读 · 0 评论 -
Leetcode:23. Merge k Sorted Lists (week 10)
DescriptionMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路利用分治的思想把合并k个链表分成两个合并k/2个链表的任务,一直划分,知道任务中只剩一个链表或者两个链表。可以很简单的用递归来实现。因此算法复杂度为T(k) = 2T(k/2原创 2017-11-11 14:34:56 · 170 阅读 · 0 评论 -
Leetcode:402. Remove K Digits (Week 7)
Description:Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and原创 2017-10-16 13:50:49 · 181 阅读 · 0 评论 -
Leetcode:435. Non-overlapping Intervals (week 9)
Description:Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: You may assume the interval’s end point原创 2017-11-04 14:18:10 · 134 阅读 · 0 评论 -
Leetcode:240. Search a 2D Matrix II (week 11)
DescriptionWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right. Integers原创 2017-11-19 21:39:44 · 120 阅读 · 0 评论 -
Leetcode:226. Invert Binary Tree
Description:Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9to 4 / \ 7 2 / \ / \ 9 6 3 1解题思路递归算法: 1、交换根节点的左右子树。 2、对左右子树分别执行递归原创 2017-11-20 22:44:28 · 137 阅读 · 0 评论 -
Leetcode:241. Different Ways to Add Parentheses
Description解题思路本题为分治算法问题,题意为给定一串数字和操作符,进行计算,返回所有可能结果。代码如下class Solution {public: vector<int> diffWaysToCompute(string input) { vector<int> result; int size = input.size();原创 2018-01-09 14:36:35 · 209 阅读 · 0 评论