摘要:Find the minimal average of any slice containing at least two elements.A non-empty zero-indexed array A consisting of N integers is given. A pair of i...
阅读全文
摘要:A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English lettersA, C, G, T.This string actually represents a DNA sequence, and the upper-case letters represent single nucleotides(核苷).You are also given non-empty zero-indexed arrays P and Q cons
阅读全文
摘要:A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.Array A contains only 0s and/or 1s:0 represents a car traveling east,1 represents a car traveling west.The goal is to count passing cars. We say that a pair of
阅读全文
摘要:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8
阅读全文
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.思路:这题不要想得太复杂,什么搜索策略什么贪心什么BFS DFS。其实就是个DP基础题,迭代从上到下从左往右计算每个格子的距离就行了,目标是计算右下角的格子,没必要开二维数组,每次只需
阅读全文
摘要:Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot",&q
阅读全文
摘要:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"思路:有关Parentheses的题目也是比较常见的,除了Generate Parentheses还有Valid Pa
阅读全文
摘要: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?思路这题是Linked List Cycle的进阶版Given a linked list, determine if it has a cycle in it. 1 bool hasCycle(ListNode *head) { 2 if(head == NULL) return false; ...
阅读全文
摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code s
阅读全文
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思路:这个题是Single Number的进阶版,如果其他的数都出现两次而不是三次,可以将所有的数进行异或,出现偶数次的bit最后会是0,而target数字中为1的bit没有被抵消而保
阅读全文
摘要: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 are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0思路:其实二分查找是最基本的,本来没
阅读全文
摘要: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 inde...
阅读全文
摘要:【InorderTraversal】Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?confused what"{1,#,2,3}"means?> read more on how binary tree is seria
阅读全文
摘要:Given a binary tree struct 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, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]confused what"{1,#,2,3}"means?> read more .
阅读全文
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ...
阅读全文
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路:每次转着圈挪四个元素,这步只需要一个int的额外空间,想象一个方阵哦不好意思,这个太不专业了,咱换个大一点6×6的草图上比划比划第二层的元素怎么移动,一次Accept的感觉好爽代码: 1 void rotate(vector > &matrix) { 2 int n = matrix.size(); 3 i
阅读全文
摘要:A robot is located at the top-left corner of amxngrid (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 reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni
阅读全文
摘要:A robot is located at the top-left corner of amxngrid (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 reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni
阅读全文
摘要: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 keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri
阅读全文