leetcode
江上渔者21号
这个作者很懒,什么都没留下…
展开
-
1 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 el...原创 2018-04-08 00:08:43 · 101 阅读 · 0 评论 -
两个链表 数字相加
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 i...原创 2018-04-09 01:12:07 · 235 阅读 · 0 评论 -
最长不重复字串长度
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with...原创 2018-04-09 23:24:06 · 122 阅读 · 0 评论 -
三个数的和等于target值
Given array nums = [-1, 0, 1, 2, -1, -4],A solution set is:[ [-1, 0, 1], [-1, -1, 2]]这道题让我们求三数之和,比之前那道Two Sum要复杂一些,博主考虑过先fix一个数,然后另外两个数使用Two Sum那种HashMap的解法,但是会有重复结果出现,就算使用set来去除重复也不行,会T...原创 2018-04-22 02:10:00 · 1556 阅读 · 0 评论 -
有序重复数组中,删除重复的
class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0){ return 0; } int res=0; for(int i=1;i<nums.size();++...原创 2018-05-01 01:02:09 · 256 阅读 · 0 评论 -
链表中每两个节点反转
Given 1->2->3->4, you should return the list as 2->1->4->3./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : v...原创 2018-04-24 00:14:45 · 1347 阅读 · 0 评论 -
数组中去除指定的val
class Solution {public: int removeElement(vector<int>& nums, int val) { int size=nums.size(); if(size<=0){ return 0; } int index=...转载 2018-05-04 10:23:41 · 302 阅读 · 0 评论 -
单词word1到word2的最小编辑次数
Example 1:Input: word1 = "horse", word2 = "ros"Output: 3Explanation: horse -> rorse (replace 'h' with 'r')rorse -> rose (remove 'r')rose -> ros (remove 'e')Example 2:Input: word...转载 2018-05-16 23:54:24 · 983 阅读 · 0 评论 -
s1和s2 能否构成s3
Example 1:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"Output: trueExample 2:Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"Output: false class Solution {public:转载 2018-05-17 08:49:39 · 881 阅读 · 0 评论 -
meger 两个有序的数组
Input:nums1 = [1,2,3,0,0,0], m = 3nums2 = [2,5,6], n = 3Output: [1,2,2,3,5,6]从后面开始进行class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, ...原创 2018-04-27 00:18:20 · 156 阅读 · 0 评论 -
有序序列查找一定的范围
vector<int> searchRange(vector<int>& nums, int target) { int left=0; int right=nums.size()-1; vector<int> res(2,-1); while(left<=right){ ...转载 2018-05-04 23:19:38 · 549 阅读 · 0 评论 -
丑数是求取
Example:Input: n = 10Output: 12Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. class Solution {public: int nthUglyNumber(int n) { ...转载 2018-05-17 23:50:06 · 95 阅读 · 0 评论 -
链表向右移动
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplan...转载 2018-05-12 00:16:13 · 823 阅读 · 0 评论 -
有序数组中找到一个数的插入位置
Example 1:Input: [1,3,5,6], 5Output: 2Example 2:Input: [1,3,5,6], 2Output: 1Example 3:Input: [1,3,5,6], 7Output: 4Example 4:Input: [1,3,5,6], 0Output: 0 class Sol...转载 2018-05-05 21:31:58 · 720 阅读 · 0 评论 -
链表中删除倒数第n个
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* re...转载 2018-04-28 00:09:39 · 150 阅读 · 0 评论 -
判断是否数独
class Solution {public: // use use[] flag judge bool isValidSudoku(vector<vector<char>>& board) { const int row=9; const int col=9; int ...转载 2018-05-06 16:05:57 · 536 阅读 · 0 评论 -
矩阵的最小路径之和dp
Given 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 right a...转载 2018-05-12 23:07:08 · 234 阅读 · 0 评论 -
矩阵中左上角到右下角到种数
Example 1:Input: m = 3, n = 2Output: 3Explanation:From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:1. Right -> Right -> Down2. Right -> Down -...转载 2018-05-13 00:14:18 · 689 阅读 · 0 评论 -
数组的最大乘积子序列
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output: 6Explanation: [...转载 2018-05-19 01:09:46 · 410 阅读 · 0 评论 -
求取数独的一种37. Sudoku Solver
Empty cells are indicated by the character '.'.A sudoku puzzle... class Solution {public: void solveSudoku(vector<vector<char>>& board) { solve(board,0)...转载 2018-05-08 08:37:24 · 146 阅读 · 0 评论 -
矩阵的左上角到右下角到路径数量,中间有障碍物
Input:[ [0,0,0], [0,1,0], [0,0,0]]Output: 2Explanation:There is one obstacle in the middle of the 3x3 grid above.There are two ways to reach the bottom-right corner:1. Right -> Rig...转载 2018-05-14 23:40:35 · 2066 阅读 · 0 评论 -
数字组合sum 可以重复的
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.The same repeated...原创 2018-05-21 23:45:50 · 259 阅读 · 0 评论 -
数字组合sum 不可以重复并且去重复
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in candidates may o...原创 2018-05-21 23:56:34 · 1284 阅读 · 0 评论 -
数组的子集Subsets
Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3], [1], [2...原创 2018-05-22 09:21:18 · 163 阅读 · 0 评论 -
Longest Valid Parentheses最长有效括号
Example 1:Input: "(()"Output: 2Explanation: The longest valid parentheses substring is "()"Example 2:Input: ")()())"Output: 4Explanation: The longest valid parentheses substring is "()()"...转载 2018-05-16 00:14:54 · 152 阅读 · 0 评论 -
dp House Robber 强盗抢劫
class Solution {public: // f[0] = nums[0] f[1]=max(nums[0],nums[1]) f[2]=max(f[1],f[0]+nums[i]) int rob(vector<int>& nums) { int size = nums.size(); if (size == 0){ ...原创 2018-06-03 09:54:14 · 316 阅读 · 0 评论 -
数组的子集Subsets 不能重复
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets.Example:Input: [1,2,2]Output:...原创 2018-05-22 19:12:24 · 228 阅读 · 0 评论 -
House Robber II 组成一个环
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is ...转载 2018-06-03 11:10:50 · 125 阅读 · 0 评论 -
大于s最小的子数字的和 Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.Example: Input: [2,3...转载 2018-05-22 22:31:50 · 168 阅读 · 0 评论 -
字符串最小窗口Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"Note:...转载 2018-05-22 23:22:18 · 208 阅读 · 0 评论 -
判断是否为二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key. T...转载 2018-05-23 23:56:41 · 177 阅读 · 0 评论 -
格雷码生成
格雷码的规律,比如n=3时000001011010110111101100规律是 将格雷码看成是上下两部分 n=1 上 0 下 1n=2上为n=1时的格雷码 下为n=1时的格雷码的倒序再加上最前面的1class Solution {public: // 除去最高位 对称 vector<int> grayCode(int n) { ve...原创 2018-05-29 00:34:31 · 990 阅读 · 0 评论 -
乱序中找出第一次未出现的正整数
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space...转载 2018-05-29 01:03:38 · 520 阅读 · 0 评论 -
kmp 字符串匹配
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Exam...原创 2018-05-30 10:35:48 · 142 阅读 · 0 评论 -
两个非负数相乘
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.Example 1:Input: num1 = "2", num2 = "3"Output: "6"Example 2:Inp...原创 2018-05-30 23:15:33 · 429 阅读 · 0 评论 -
两个非负字符串相加
class Solution {public: string addStrings(string num1, string num2) { int size1 = num1.size(); int size2 = num2.size(); int size3 = max(size1,size2)+1; vector<int>...原创 2018-05-30 23:45:20 · 304 阅读 · 0 评论 -
coin change 到达amoumt最少货币 和到达此amount的种数量 找零钱
class Solution {public: int coinChange(vector<int>& coins, int amount) { // dp[i] 到达钱数i 最小的次数 int Max = amount+1; vector<int> dp(amount+1,Max); ...转载 2018-06-07 01:03:50 · 329 阅读 · 0 评论 -
股票 一次买卖
Example 1:Input: [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than...转载 2018-06-14 00:11:32 · 210 阅读 · 0 评论 -
股票可以买卖多次
class Solution {public: int maxProfit(vector<int>& prices) { int size = prices.size(); if(size == 0){ return 0; }// // 交易次数没有限制 就是所有 下一个工作日的价格大于上一个...原创 2018-06-14 00:17:43 · 375 阅读 · 0 评论 -
股票买卖2次
Example 1:Input: [3,3,5,0,0,3,1,4]Output: 6Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price ...原创 2018-06-14 00:40:10 · 805 阅读 · 0 评论