![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
jimmybao0730
这个作者很懒,什么都没留下…
展开
-
【leetcode】Majority Element
Majority ElementGiven 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 majori原创 2015-04-01 18:08:10 · 390 阅读 · 0 评论 -
【leetcode】Remove Element
Remove ElementGiven an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new le原创 2015-04-01 18:46:07 · 291 阅读 · 0 评论 -
【leetcode】Sort Colors
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the i原创 2015-04-01 19:00:35 · 431 阅读 · 0 评论 -
【leetcode】Unique Binary Search Trees
Unique Binary Search TreesGiven n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s. 这道题目提交将近10遍。。。动态规划的原创 2015-04-01 19:04:32 · 462 阅读 · 0 评论 -
【leetcode】Unique Paths
Unique PathsA robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to r原创 2015-04-01 19:07:16 · 482 阅读 · 0 评论 -
【leetcode】Best Time to Buy and Sell Stock
Best Time to Buy and Sell StockSay 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 s原创 2015-04-01 12:51:13 · 370 阅读 · 0 评论 -
【leetcode】Climbing Stairs
You 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? 用动态规划的思路去做 基本的思路就是,有i节台阶的话,那么其可能原创 2015-04-01 13:01:44 · 346 阅读 · 0 评论 -
【leetcode】Excel Sheet Column Number
Excel Sheet Column NumberGiven a column title as appear in an Excel sheet, return its corresponding column number. For example:A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 A原创 2015-04-01 13:09:19 · 425 阅读 · 0 评论 -
【leetcode】Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted ArraySuppose a sorted array 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 ass原创 2015-04-01 13:17:47 · 373 阅读 · 0 评论 -
【leetcode】Find Peak Element
Find Peak ElementA 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 multipl原创 2015-04-01 13:20:15 · 379 阅读 · 0 评论 -
【leetcode】Generate Parentheses
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "(原创 2015-04-01 17:27:49 · 475 阅读 · 0 评论 -
【leetcode】Linked List Cycle
Linked List CycleGiven a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 本题是链表的题目,注意链表的具体表达~~用双指针来实现/*** Definition for singly-linked list. *原创 2015-04-01 17:35:19 · 338 阅读 · 0 评论 -
【leetcode】Min & Max Depth of Binary Tree
Minimum Depth of Binary TreeGiven 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. 利用递归的算法进行计算原创 2015-04-01 18:20:50 · 430 阅读 · 0 评论 -
【leetcode】Remove Duplicates from Sorted List
Remove Duplicates from Sorted ListGiven a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2原创 2015-04-01 18:45:05 · 353 阅读 · 0 评论 -
【leetcode】Same Tree
Same TreeGiven 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. 注意题目要求原创 2015-04-01 18:50:18 · 312 阅读 · 0 评论 -
【leetcode】Single Number
Single NumberGiven an array of integers, every element appears twice except for one. Find that single one. 超时算法:就是设置两个指针,然后比较所指的数字:int singleNumber(int A[], int n) { int i,j; int answer; f原创 2015-04-01 18:58:31 · 367 阅读 · 0 评论 -
【leetcode】Swap Nodes in Pairs
Swap Nodes in PairsGiven 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 con原创 2015-04-01 19:02:29 · 447 阅读 · 0 评论 -
【leetcode】Gray Code
Gray Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequ原创 2015-04-01 17:33:31 · 387 阅读 · 0 评论 -
【leetcode】Roman & Integer
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.本题要明确罗马数字的规则,如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如 IV = 5 – 1;否 则,将当前值加入到结果中,然后开原创 2015-04-01 18:48:12 · 327 阅读 · 0 评论 -
【leetcode】Maximum Subarray
Maximum SubarrayFind 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原创 2015-04-01 18:18:07 · 282 阅读 · 0 评论 -
【leetcode】Number of 1 Bits
Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representa原创 2015-04-01 18:25:49 · 337 阅读 · 0 评论 -
【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each NodeGiven a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to原创 2015-04-01 18:38:26 · 300 阅读 · 0 评论 -
【leetcode】Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayGiven 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原创 2015-04-01 18:42:24 · 329 阅读 · 0 评论 -
【leetcode】Search Insert Position
Search Insert PositionGiven 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 dupli原创 2015-04-01 18:56:01 · 293 阅读 · 0 评论 -
【leetcode】Plus One
Plus OneGiven a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.这道题目的思路刚开始是把vector转化为原创 2015-04-10 16:32:25 · 318 阅读 · 0 评论 -
【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array IIFollow up for “Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return原创 2015-04-10 17:24:11 · 329 阅读 · 0 评论 -
【leetcode】Sum Root to Leaf Numbers
Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 12原创 2015-04-10 18:18:57 · 329 阅读 · 0 评论 -
【leetcode】Merge Sorted Array
Merge Sorted ArrayGiven two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional原创 2015-04-10 20:04:01 · 348 阅读 · 0 评论 -
【leetcode】Merge Two Sorted Lists
Merge Two Sorted ListsMerge 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.注意: 最后返回值的时候,不能返回newlist,因为newlist的指针原创 2015-04-06 21:41:04 · 324 阅读 · 0 评论 -
【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.BST的定义: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于原创 2015-04-06 19:49:08 · 330 阅读 · 0 评论 -
【leetcode】Balanced Binary Tree
Balanced Binary TreeGiven a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every原创 2015-04-06 22:07:57 · 419 阅读 · 0 评论 -
【leetcode】Container With Most Water
Container With Most WaterGiven n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai)原创 2015-04-07 20:43:40 · 348 阅读 · 0 评论 -
【leetcode】Binary Tree Postorder Traversal
Binary Tree Postorder TraversalGiven a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, return [3,2,1].后序遍历就说先访问左子树再访问右子树,最后访问根节点,用迭代算法/原创 2015-04-07 20:04:44 · 328 阅读 · 0 评论 -
【leetcode】Minimum Path Sum
Minimum Path SumGiven 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.这道题目明显用动态规划,最优子结构为a[i][j]=min(a[i][j原创 2015-04-07 22:10:53 · 329 阅读 · 0 评论 -
【leetcode】Search a 2D Matrix
Search a 2D MatrixWrite 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 from left to right.The first inte原创 2015-04-08 19:19:29 · 300 阅读 · 0 评论 -
【leetcode】Rotate Image
Rotate ImageYou are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise).这道题目就是基本的找规律题:matrix[i][j]=matrix[n-j-1][i];然后再判断下两层循环,基本上就没有问题了。另外一个个慢慢转的方法,leetcode也是接原创 2015-04-08 18:17:32 · 289 阅读 · 0 评论 -
【leetcode】Set Matrix Zeroes
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.建立一个temp来记录原始数组,不能设置为0的时候,改变其原始的数据。之后再进行循环赋值好了。 算法时间复杂度可以用记录的方法进行简化,然后将内部赋值的循环挪到外面来即可。clas原创 2015-04-08 20:14:12 · 325 阅读 · 0 评论 -
【leetcode】Add Binary
Add BinaryGiven two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. 思路: 和链表相加比较相似,但是要注意对字符串的处理。最后输出要反向~class Solution {public: string add原创 2015-05-08 20:44:16 · 292 阅读 · 0 评论 -
【leetcode】Bitwise AND of Numbers Range
Bitwise AND of Numbers RangeGiven a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return原创 2015-05-08 21:12:15 · 317 阅读 · 0 评论 -
【leetcode】Isomorphic Strings
Isomorphic StringsGiven two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be repla原创 2015-05-08 19:17:10 · 296 阅读 · 0 评论