LeetCode题解
文章平均质量分 54
YF_Li123
生活需要奔跑
展开
-
LeetCode——回文数
题目判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。示例 1:输入: 121输出: true示例2:输入: -121输出: false解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。示例 3:输入: 10输出: false解释: 从右向左读, 为 01 。因此它不是一个回文数。进阶:你能不将整数转为字符串来解决这个问题吗?题解解题思路一看到这个题目第..原创 2020-10-26 11:49:11 · 475 阅读 · 0 评论 -
LeetCode——查找常用字符
题目给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。你可以按任意顺序返回答案。示例 1:输入:["bella","label","roller"]输出:["e","l","l"]示例 2:输入:["cool","lock","cook"]输出:["c","o"]提示:1 <= A.length <=原创 2020-10-15 20:29:18 · 358 阅读 · 0 评论 -
LeetCode——两两交换链表中的节点
题目给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例:给定 1->2->3->4, 你应该返回 2->1->4->3.题解根据题目信息“两两交换”我们知道,要有两个节点才能进行交换操作,如果链表只有一个节点或为空链表,则不需要进行交换。这是我们的终止条件。解题思路方法一:递归我们可以将链表两两一组进行拆分,每组内进行节点交换,交换后返回头节点(第一个..原创 2020-10-13 15:16:39 · 298 阅读 · 0 评论 -
LeetCode——二叉搜索树的最小绝对值
题目给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。示例:输入:1\3/2输出:1解释:最小绝对差为 1,其中 2 和 1 的差的绝对值为 1(或者 2 和 3)。题解从题目中可以获得以下几条信息:二叉树,且为二叉搜索树,根据二叉搜索树的定义可知二叉树有序 节点为非负值解题思路中序遍历方法 首先我们先分析另外一个问题,如何求一个...原创 2020-10-12 18:40:53 · 378 阅读 · 0 评论 -
LeetCode——四数之和
题目给定一个包含n 个整数的数组nums和一个目标值target,判断nums中是否存在四个元素 a,b,c和 d,使得a + b + c + d的值与target相等?找出所有满足条件且不重复的四元组。注意:答案中不可以包含重复的四元组。示例:给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。满足要求的四元组集合为:[[-1,0, 0, 1],[-2, -1, 1, 2],[-2,0,...原创 2020-10-12 08:20:14 · 463 阅读 · 0 评论 -
LeetCode——三数之和
题目给你一个包含 n 个整数的数组nums,判断nums中是否存在三个元素 a,b,c ,使得a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。示例:给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2]]题解从题目中可以获取以下几条信息: 数组元素为int类型,无序,数组元素可重复 找到所有满...原创 2020-10-09 15:44:40 · 270 阅读 · 0 评论 -
LeetCode——两数之和
题目给定一个整数数组 nums和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]题解从题目中可获取以下几条信息:数组元素为整数,即int类型,其范围为[-2^31, 2^31-1]...原创 2020-09-29 16:51:32 · 191 阅读 · 0 评论 -
LeetCode刷题(C++)——Divide Two Integers(Medium)
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.class Solution {public: int divide(int dividend, int divisor) { if (d原创 2017-05-07 21:49:59 · 501 阅读 · 0 评论 -
LeetCode刷题(C++)——Minimum Path Sum(Medium)
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 at原创 2017-05-08 15:06:13 · 330 阅读 · 0 评论 -
LeetCode刷题(C++)——Maximum Subarray(Easy)
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] ha原创 2017-05-08 18:56:09 · 376 阅读 · 0 评论 -
LeetCode刷题(C++)——Edit Distance(Hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:原创 2017-05-08 20:10:21 · 635 阅读 · 0 评论 -
LeetCode刷题(C++)——Merge k Sorted Lists(Hard)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne原创 2017-05-10 14:39:00 · 308 阅读 · 0 评论 -
C++中字符串匹配算法strstr()函数用法
strstr()const char* _cdecl strstr(const char* _Str, const char* _Substr);char* _cdecl strstr(char* _String, const char* _SubString);strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首原创 2017-05-07 16:14:28 · 18384 阅读 · 3 评论 -
LeetCode刷题(C++)——Implement strStr()(Easy)
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.strStr()函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NUL原创 2017-05-07 15:34:48 · 537 阅读 · 0 评论 -
LeetCode刷题(C++)——Remove Element(Easy)
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.原创 2017-05-07 14:32:16 · 467 阅读 · 0 评论 -
LeetCode刷题(C++)——Remove Duplicates from Sorted Array(Easy)
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2017-05-07 14:21:42 · 246 阅读 · 0 评论 -
LeetCode刷题(C++)——Generate Parentheses(Medium)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())原创 2017-05-07 09:16:02 · 343 阅读 · 0 评论 -
LeetCode刷题(C++)——Merge Two Sorted Lists(Easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked list. * struct Lis原创 2017-05-06 21:30:57 · 257 阅读 · 0 评论 -
LeetCode刷题(C++)——Letter Combinations of a Phone Number(Medium)
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2017-05-06 20:55:24 · 573 阅读 · 0 评论 -
LeetCode刷题(C++)——4Sum(Medium)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution原创 2017-05-09 10:30:25 · 421 阅读 · 0 评论 -
LeetCode刷题(C++)——Search Insert Position(Easy)
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2017-05-09 11:31:47 · 320 阅读 · 0 评论 -
LeetCode刷题(C++)——Count and Say(Easy )
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2017-05-09 19:52:29 · 268 阅读 · 0 评论 -
LeetCode刷题(C++)——Course Schedule
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 expressed as a原创 2017-08-16 20:52:03 · 534 阅读 · 0 评论 -
LeetCode刷题(C++)——Course Schedule II
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 expressed as原创 2017-08-16 20:49:18 · 390 阅读 · 0 评论 -
LeetCode刷题(C++)—— Pascal's Triangle(Easy)
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solution {pu原创 2017-05-22 09:45:42 · 448 阅读 · 0 评论 -
LeetCode刷题(C++)——Next Permutation(Medium)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2017-05-09 20:54:01 · 362 阅读 · 0 评论 -
LeetCode刷题(C++)——Maximum Depth of Binary Tree(Easy)
题目描述Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for a binary原创 2017-04-28 17:10:19 · 299 阅读 · 0 评论 -
LeetCode刷题(C++)——Same Tree(Easy)
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思路:递归,判断根结原创 2017-05-21 11:28:43 · 327 阅读 · 0 评论 -
LeetCode刷题(C++)——Verify Preorder Serialization of a Binary Tree(Medium)
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.原创 2017-05-19 18:47:07 · 284 阅读 · 0 评论 -
LeetCode刷题(C++)——Sort List(Medium)
Sort a linked list in O(n log n) time using constant space complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :原创 2017-05-19 14:57:37 · 355 阅读 · 0 评论 -
LeetCode刷题(C++)——Odd Even Linked List(Medium)
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2017-05-19 10:53:46 · 283 阅读 · 0 评论 -
LeetCode刷题(C++)——Linked List Cycle II(Medium)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?/**原创 2017-05-18 20:30:11 · 304 阅读 · 0 评论 -
LeetCode刷题(C++)——Linked List Cycle(Easy)
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:设置两个指针fast和slow,fast一次走两步,slow一次走一步,如果链表有环,两指针一定相遇,并且一定是在环中相遇。否则,链表就没有环/**原创 2017-05-18 20:01:10 · 276 阅读 · 0 评论 -
LeetCode刷题(C++)——Valid Parentheses(Easy)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2017-05-06 19:53:51 · 268 阅读 · 0 评论 -
LeetCode刷题(C++)——Remove Nth Node From End of List(Medium)
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2017-05-06 16:46:21 · 609 阅读 · 0 评论 -
LeetCode刷题(C++)——3Sum Closest(Medium)
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact原创 2017-05-04 20:24:40 · 315 阅读 · 0 评论 -
LeetCode刷题(C++)——Integer to Roman(Medium)
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.罗马数字:1-9:{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }10-90:{"X", "XX原创 2017-05-02 14:50:50 · 309 阅读 · 0 评论 -
LeetCode刷题(C++)——Palindrome Number(Easy)
Determine whether an integer is a palindrome. Do this without extra space.class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; int base = 1; while (x /原创 2017-05-02 11:55:59 · 252 阅读 · 0 评论 -
LeetCode刷题(C++)——Median of Two Sorted Arrays(Hard)
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:nums1 =原创 2017-05-01 16:44:35 · 421 阅读 · 0 评论 -
LeetCode刷题(C++)——Longest Substring Without Repeating Characters(Medium)
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 "原创 2017-05-01 11:41:57 · 262 阅读 · 0 评论