![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode For C++
JackZhangNJU
未来的路还很长
展开
-
leetcode 112. Path Sum DFS深度优先遍历
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example: Given the below binary tree and sum = 2原创 2017-09-13 09:58:16 · 412 阅读 · 0 评论 -
leetcode 114. Flatten Binary Tree to Linked List 扁平化二叉树+前序遍历
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3原创 2017-09-14 12:40:52 · 343 阅读 · 0 评论 -
leetcode 116. Populating Next Right Pointers in Each Node BFS广度优先遍历
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,原创 2017-09-14 13:13:34 · 325 阅读 · 0 评论 -
leetcode 118. Pascal's Triangle 杨辉三角形
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] ]这个就是中国最伟大的杨辉三角形的问题。代码如下:import原创 2017-09-14 13:20:31 · 268 阅读 · 0 评论 -
leetcode 15. 3Sum 以及2Sum的问题的处理和求解
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain duplic原创 2017-08-30 16:27:00 · 341 阅读 · 0 评论 -
leetcode 16. 3Sum Closest
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 exactly原创 2017-08-31 14:33:25 · 269 阅读 · 0 评论 -
leetcode 21. Merge Two Sorted Lists
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.Seen this question in a real interview before? Yes简单的归并排序,原创 2017-08-31 16:40:22 · 207 阅读 · 0 评论 -
leetcode 22. Generate Parentheses DFS深度优先遍历
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-08-31 16:59:28 · 401 阅读 · 0 评论 -
leetcode 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.提议很简单,就是归并排序。首先想到的即使逐个归并得到最终的结果,但是会超时,这是因为这种会造成数组的size大小不一样,导致归并排序的时间变长;最好的做法是两两合并,然后在两两合并,这样不会超时, 需要注原创 2017-08-31 17:19:41 · 341 阅读 · 0 评论 -
leetcode 119. Pascal's Triangle II 杨辉三角形2
Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].就是中国最伟大的杨辉三角形。代码如下:import java.util.ArrayList;import java.util.List;public class So原创 2017-09-14 13:22:55 · 278 阅读 · 0 评论 -
leetcode 120. Triangle 杨辉三角形3 + BFS广度优先遍历
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle [ [2], [3,4], [6原创 2017-09-14 13:29:56 · 444 阅读 · 0 评论 -
leetcode 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may no原创 2017-08-31 17:44:53 · 253 阅读 · 0 评论 -
leetcode 25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nod原创 2017-08-31 19:33:42 · 253 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array 堆排序的简单应用 + 快速排序
Find 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, return 5.这...原创 2017-09-22 09:53:48 · 674 阅读 · 0 评论 -
leetcode 217. Contains Duplicate 遍历 + HashSet
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2017-09-22 10:04:33 · 325 阅读 · 0 评论 -
leetcode 136. Single Number 位操作
Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra me原创 2017-09-15 11:01:17 · 323 阅读 · 0 评论 -
leetcode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you i原创 2017-09-17 10:42:55 · 476 阅读 · 0 评论 -
leetcode 143. Reorder List 双指针
Given a singly linked list L: L0?L1?…?Ln-1?Ln, reorder it to: L0?Ln?L1?Ln-1?L2?Ln-2?…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}原创 2017-09-17 13:34:24 · 364 阅读 · 0 评论 -
leetcode 147. Insertion Sort List 链表插入排序
Sort a linked list using insertion sort.本题就是要做链表的插入排序。代码如下:/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}*//* * 链表的插入排序 * */public class Solution {原创 2017-09-15 11:09:42 · 315 阅读 · 0 评论 -
leetcode 148. Sort List 链表归并排序
Sort a linked list in O(n log n) time using constant space complexity.本题就是考察的是链表的归并排序。代码如下:/*class ListNode { int val; ListNode next; ListNode(int x) { val = x; }}*/public class So原创 2017-09-18 09:26:52 · 540 阅读 · 0 评论 -
leetcode 150. Evaluate Reverse Polish Notation 逆波兰表达式的计算
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: [“2”, “1”, “+”, “3”, ““]原创 2017-09-18 09:39:23 · 464 阅读 · 0 评论 -
leetcode 28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串,我这里是直接循环判断的。代码如下:public class Solut原创 2017-09-01 17:07:12 · 206 阅读 · 0 评论 -
leetcode 29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这种题我不喜欢,这个是网上查阅的答案,这道题就这样吧。代码如下:public class Solution { public int divide(int dividend,原创 2017-09-01 17:33:20 · 292 阅读 · 0 评论 -
leetcode 30. Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wit原创 2017-09-02 15:21:14 · 308 阅读 · 0 评论 -
leetcode 31. Next Permutation
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 orde原创 2017-09-02 15:53:12 · 252 阅读 · 0 评论 -
leetcode 33. Search in Rotated Sorted Array 二分查找
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the ar原创 2017-09-02 16:29:55 · 302 阅读 · 0 评论 -
leetcode 34. Search for a Range 二分查找
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the targ...原创 2017-09-02 16:47:35 · 272 阅读 · 0 评论 -
leetcode 35. Search Insert Position
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.Here原创 2017-09-01 21:41:06 · 184 阅读 · 0 评论 -
leetcode 36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’ 这道题就是判断目前的数独表格是否是合理的,需要判断每一行、每一原创 2017-09-02 16:56:41 · 212 阅读 · 0 评论 -
leetcode 153. Find Minimum in Rotated Sorted Array 旋转数组(不重复)+二分查找
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicat...原创 2017-09-18 10:12:29 · 558 阅读 · 0 评论 -
leetcode 154. Find Minimum in Rotated Sorted Array II 旋转数组(重复)+二分查找
Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose an array sorted in ascending order is rotated a...原创 2017-09-18 10:23:48 · 462 阅读 · 0 评论 -
leetcode 160. Intersection of Two Linked Lists 链表公共节点
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ...原创 2017-09-19 09:13:17 · 511 阅读 · 0 评论 -
leetcode 162. Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in that case原创 2017-09-19 09:19:22 · 429 阅读 · 0 评论 -
leetcode 38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:111211211111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as原创 2017-09-02 18:20:18 · 209 阅读 · 0 评论 -
leetcode 45. Jump Game II 贪心算法&&DFS深度优先搜索
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reac原创 2017-09-03 16:02:57 · 571 阅读 · 0 评论 -
leetcode 46. Permutations 全排列问题+递归
Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]原创 2017-09-03 16:26:55 · 446 阅读 · 0 评论 -
leetcode 47. Permutations II 全排列问题(去掉重复元素)+递归
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1]原创 2017-09-03 16:34:20 · 583 阅读 · 0 评论 -
leetcode 48. Rotate Image 矩阵旋转
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note: You have to rotate the image in-place, which means you have to modify the input 2D matrix direct原创 2017-09-03 16:46:59 · 398 阅读 · 0 评论 -
leetcode 50. Pow(x, n) 一个简单的分治算法的应用
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.题意原创 2017-09-03 10:46:30 · 385 阅读 · 0 评论 -
leetcode 51. N-Queens DFS深度优先遍历
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each s原创 2017-09-04 10:32:55 · 401 阅读 · 0 评论