![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
yinger_0131
这个作者很懒,什么都没留下…
展开
-
LeetCode(138) Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.public class l138_copy_list_with_r...原创 2018-05-24 16:59:55 · 287 阅读 · 0 评论 -
LeetCode(118)-pascal 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] ]具体生成算是:每一行的首个和结尾一个数字都是1,...原创 2018-05-18 23:39:27 · 172 阅读 · 0 评论 -
leetcode-word-break
题目描述给定一个字符串s和一个词典词典dict,确定s是否可以分割成一个或多个词典单词的空格分隔序列。例如,给定 s =“leetcode”, dict = [“leet”,“code”]。因为“leetcode”可以分段为“leet code”,所以返回true。一道动态规划题int len = s.length(); vector<bool&g...原创 2018-04-28 00:31:27 · 223 阅读 · 0 评论 -
LeetCode(2)-add 2 numbers(两个链表相加)
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link...原创 2018-05-06 11:08:17 · 80 阅读 · 0 评论 -
LeetCode(148)-sortlist 链表排序
Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 1)将待排序数组(链表)取中点并一分为二; 2)递归地对左半部分进行归并排序; 3)递归地对右半部分进行归并排序; 4)将两个半部分进行合并(merge),得到结果。public class l148_sortlist { ...原创 2018-05-18 16:46:42 · 150 阅读 · 0 评论 -
leetcode分类
利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心) http://oj.leetcode.com/problems/valid-parentheses/ ...转载 2018-05-18 13:20:32 · 275 阅读 · 0 评论 -
LeetCode(133)-clone Gragh (无向图赋值)
无向图的复制我们使用#作为每个节点的分隔符,以及作为节点标签和节点的每个邻居的分隔符。作为例子,考虑序列化图{0,1,2#1,2#2,2}。 用深度优先DFS遍历,复制图 用map保存新旧值的映射关系class UndirectedGraphNode{ int label; ArrayList&lt;UndirectedGraphNode&gt; nei...原创 2018-05-17 17:13:52 · 341 阅读 · 0 评论 -
LeetCode(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.Example:Given nums = [2, 7...原创 2018-05-04 14:35:40 · 81 阅读 · 0 评论 -
LeetCode(90)-Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate...原创 2018-05-04 01:17:07 · 107 阅读 · 0 评论 -
Leetcode(78)-subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets.For example, If ...原创 2018-05-04 00:52:41 · 80 阅读 · 0 评论 -
LeetCode(149)-Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 求最大的共线点的个数public class l149_maxpoints_in_line { static class Point{ int x; in...原创 2018-05-17 01:18:34 · 185 阅读 · 0 评论 -
leetcode-search-in-rotated-sorted-array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its ind...原创 2018-04-20 17:14:41 · 100 阅读 · 0 评论 -
leetcode-count-and-say
count-and-say序列是从以下开始的整数序列: 1,11,21,1211,111221,…1被读作“one 1”或11。 11被读作“两个1”或21。 21被读作“一个2,然后一个1”或1211。 给定一个整数n,生成第n 个序列。#include <iostream>#include <string>using namespace std;...原创 2018-04-20 16:14:51 · 177 阅读 · 0 评论 -
LeetCode(170)_add and find
Design and implement a TwoSum class. It should support the following operations:add and find.add - Add the number to an internal data structure. find - Find if there exists any pair of numbers whic...原创 2018-05-06 13:11:44 · 179 阅读 · 0 评论 -
LeetCode(15)-3sum 三数之和
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:Elements in a triplet (a,b,c) must b...原创 2018-05-06 15:30:23 · 106 阅读 · 0 评论 -
LeetCode(105)-Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.这道题要求用先序和中序遍历来建立二叉树我们下面来看一个例子, 某一二叉树的前序和中序遍历分别为: Preorde...原创 2018-05-06 18:26:07 · 90 阅读 · 0 评论 -
LeetCode(139/140) word break拆分词句
1. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given s =”leetcode”, dict =[“leet”...原创 2018-05-23 00:42:03 · 336 阅读 · 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”, ...原创 2018-05-15 12:59:13 · 145 阅读 · 0 评论 -
Leetcode(136/137)Single Number-
Single Number1: 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 wi...原创 2018-05-25 01:10:11 · 158 阅读 · 0 评论 -
LeetCode(142)-linked-list-cycle求单链表中的环的起始点
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up: Can you solve it without using extra space?这道题是快慢指针的经典应用。 step1: 设两个指针,一个每次走一步的慢指针和一个每次走...原创 2018-05-21 00:31:55 · 185 阅读 · 0 评论 -
LeetCode(143)-reorder-list
Given a singly linked list L: L 0→L 1→…→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…You must do this in-place without altering the nodes’ values.For example, Given{1,2,3,4}, reorder ...原创 2018-05-21 00:01:32 · 135 阅读 · 0 评论 -
LeetCode(77)-Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],...原创 2018-05-07 16:59:12 · 177 阅读 · 0 评论 -
LeetCode(101)-minimum-depth-binary-tree二叉树最小深度
给定一棵二叉树,找到它的最小深度。 最小深度问题就是就最短路径的节点个数/*思路:递归,若为空树返回0;若左子树为空,则返回右子树的最小深度+1;(加1是因为要加上根这一层,下同)若右子树为空,则返回左子树的最小深度+1;若左右子树均不为空,则取左、右子树最小深度的较小值,+1; */public class l101_minimum_depth_binarytree {...原创 2018-05-07 15:50:03 · 108 阅读 · 0 评论 -
LeetCode(5)-Longest Palindromic Substring 最长回文串两种解法(中心扩散法、DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.思路一:中心扩展法:就是把给定的字符串的每一个字...原创 2018-05-07 15:16:59 · 610 阅读 · 0 评论 -
酷家乐笔试
题目(类似): s = “3[a]2[bc]”, return “aaabcbc”. s = “3[a2[c]]”, return “accaccacc”. #include <iostream>using namespace std; string decode(string s, int &k) { string res = ""; ...原创 2018-04-29 00:23:10 · 2139 阅读 · 0 评论 -
LeetCode(145)-二叉树后序遍历(递归、非递归)
import java.util.ArrayList;import java.util.Stack;public class l145_tree_postorder { class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x){ ...原创 2018-05-20 00:46:52 · 694 阅读 · 0 评论 -
LeetCode(147)-insertion-sort-list(链表插入排序)
题目描述Sort a linked list using insertion sort.要对头结点进行操作时,考虑创建哑节点dummy,使用dummy-&gt;next表示真正的头节点。这样可以避免处理头节点为空的边界问题。public class l147_insertion_sort_list { class ListNode{ int val; ...原创 2018-05-19 23:44:16 · 194 阅读 · 0 评论 -
LeetCode(106)Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.public class Solution { public TreeNode build(int[...原创 2018-05-06 23:21:09 · 115 阅读 · 0 评论 -
刷题-decode ways
题目描述A message containing letters fromA-Zis being encoded to numbers using the following mapping:‘A’ -> 1 ‘B’ -> 2 … ‘Z’ -> 26 Given an encoded message containing digits, determine the...原创 2018-04-14 10:12:37 · 107 阅读 · 0 评论 -
刷题-trapping-rain-water
#include <iostream>#include<algorithm>using namespace std;#define N 100int A[N];int trap(int A[],int n){ int l=A[0]; int r=A[n-1]; int i=1; int j=n-2; int sum=...原创 2018-04-14 08:58:02 · 147 阅读 · 0 评论 -
刷题-binary-tree-preorder-traversal
题目描述给定一个二叉树,返回其节点值的前序遍历。例如: 给定二叉树{1,#,2,3},1 \ 2 / 3返回[1,2,3]。#include <iostream>#include<vector>#include <cstdio>#include <stack>using names...原创 2018-04-14 00:44:00 · 79 阅读 · 0 评论 -
刷题-判断二叉树是否对称
题目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric:1/ \ 2 2 / \ / \ 3 4 4 3But the follow...原创 2018-04-05 12:07:06 · 104 阅读 · 0 评论 -
刷题-寻找第一个缺失的正整数
题目描述给定一个未分类的整型数组,找到第一个缺少的正整数。例如, 给定[1,2,0] return3 和[3,4,-1,1] return2。你的算法应该在O(n)时间运行并且使用恒定的空间。#include <iostream>#include<algorithm>#include<cstdio>using namespace st...原创 2018-04-05 11:17:29 · 772 阅读 · 0 评论 -
刷题—最长回文字符子串
最长回文子串。就是给定一个字符串S,找出其中的最长回文子串,并返回该子串。string longestPalindrome(string s) { int n = s.length(); if (n == 0) return ""; string longest = s.substr(0, 1); // a single char itse...原创 2018-04-03 00:43:35 · 228 阅读 · 0 评论 -
刷题-unique-paths-ii
题目描述现在考虑是否有一些障碍物被添加到网格中。将有多少路径?网格中的障碍物和空白区分别标记为1和0。例如,在3x3网格中间有一个障碍,如下图所示。[ [0,0,0] [0,1,0] [0,0,0] ] 唯一路径的总数是2。注意: m和n最多为100。int uniquePathsWithObstacles(vector<vector<in...原创 2018-04-15 01:34:02 · 81 阅读 · 0 评论 -
刷题-valid sudoku(有效数独)
题目描述根据:Sudoku Puzzles - 规则确定一个数独是否有效。数独板可以部分填充,空单元格填充字符’.’。 数独规则: 每个数字在每一行、每一列和每一区只能出现一次。#include <iostream>#include <vector>using namespace std;bool isValidSudoku(vector<...原创 2018-04-15 00:48:34 · 323 阅读 · 0 评论 -
刷题-生成正确括号组合
题目描述 给定n对括号,编写一个函数来生成正确括号的所有组合。 例如,给定n = 3,解决方案集合为: “((()))”,“(()())”,“(())()”,“()(())”,“()()()”/*采用递归每次左括号比右括号多时可以加左括号或者右括号如果左括号等于右括号则只能加左括号左括号够了那就只加右括号右括号够了那就只加左括号*/void generate(vect...原创 2018-04-08 09:36:19 · 258 阅读 · 0 评论 -
刷题-链表-交换相邻节点
题目描述给定一个链表,交换每两个相邻节点并返回其头部。例如, Given1-> 2-> 3-> 4,您应该返回列表as2-> 1-> 4-> 3。 你的算法应该只使用恒定的空间。您不能 修改列表中的值,只有节点本身可以更改。 使用递归 核心思想: q=p->next; p->next=q->next q->nex...原创 2018-04-08 09:23:01 · 619 阅读 · 0 评论 -
实现全排列
题目描述Given a collection of 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], and[3,2,1].class Solution {pu...原创 2018-04-08 08:38:44 · 103 阅读 · 0 评论 -
刷题-矩阵最小路径和
给定一个填充非负数的m × n网格,找到一条从左上角到右下角的路径,该路径使沿路径上的所有数字的总和最小。注意:您只能在任何时间点向下或向右移动。class Solution {public: int minPathSum(vector<vector<int> > &grid) { int m=grid.size(); ...原创 2018-04-07 20:23:05 · 344 阅读 · 0 评论