![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
InterviewQuestions
文章平均质量分 79
feliciafay
开发工程师
展开
-
LeetCode(81)RemoveDuplicatesfromSortedArrayII
题目如下:Follow 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 length = 5, and A is now [1,1原创 2014-01-11 10:47:56 · 1087 阅读 · 0 评论 -
LeetCode(9)PalindromeNumber
这道题,巨汗,提交了好几次才成功,我的基本思路就是不断地比较第i位和倒数第i位,知道遇到最中间的1个数字(输入为奇数个数字)或者遇到最中间的2个数字(输入为偶数个数字)。比较特殊的是,本题要求不能用额外的空间,所以无法借用之前的一道判断字符串是否为回文的题目的经验了。这几个case造成了前几次的失败: Input: 1001 Output: false原创 2013-12-05 03:26:54 · 3093 阅读 · 0 评论 -
LeetCode(136)Single Number
LeetCode刷题刷到现在,经常感到挫败。因为有些题目一想就是一整天,提交上去一看还超时了。太伤心了。现在采取更加科学合理的方式,按照AC率降序来刷题。不再采取以前的按照日期来刷的方式了。题目如下:Given an array of integers, every element appears twice except for one. Find that single one.N原创 2014-01-04 13:09:15 · 1377 阅读 · 0 评论 -
LeetCode(10)RegularExpresssionMatching
这道题目我觉得挺难的,想了很久也没想清楚。If you are stuck, recursion is your friend.原创 2013-12-06 07:19:34 · 3670 阅读 · 0 评论 -
LeetCode(96)Binary Tree Inorder Traversal
题目如下:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution原创 2014-01-15 06:25:40 · 1180 阅读 · 0 评论 -
LeetCode主题整理(1)滤重问题
下面都是滤重题目。(25)Remove duplicate给定一个数,给定一个数组,删除数组中的这个数,修改原数组使得新数组为去掉了这个数之后的剩下元素构成的数组并返回新数组的长度。通过交换来避免数组删除时的逐个移动,从而降低时间复杂度。(26)Remove duplicate from sorted Array排序数组滤重。考虑两种做法。第1种常规,每次都原创 2014-01-11 10:49:28 · 1370 阅读 · 0 评论 -
LeetCode(145)Binary Tree Postorder Traversal
题目如下:Given a binary tree, return the postorder traversal of its nodes' values. 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?原创 2014-01-16 04:01:50 · 4480 阅读 · 1 评论 -
LeetCode(144)Binary Tree Preorder Traversal
题目如下:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution原创 2014-01-15 06:29:16 · 2355 阅读 · 0 评论 -
LeetCode(104)Maximum Depth of Binary Tree
题目如下:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.题目分析:简单题。求树的最大深度。递归求解。cla原创 2014-01-09 06:43:14 · 792 阅读 · 0 评论 -
LeetCode(75)Sort Colors (荷兰三色旗问题 Dutch National Flag)
题目如下:The flag of the Netherlands consists of three colours: red, white and blue. Given balls of these three colours arranged randomly in a line (the actual number of balls does not matter), the task原创 2014-02-10 03:42:16 · 5868 阅读 · 0 评论 -
LeetCode(112)Path Sum
题目如下:My SubmissionsGiven 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 bel原创 2014-02-18 08:13:00 · 1024 阅读 · 0 评论 -
LeetCode(120)Triangle
题目如下: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],原创 2014-02-27 23:50:35 · 2512 阅读 · 0 评论 -
多数投票算法 Majority Vote Algorithm
题目如下:Write a program to find the element in an array that is repeated more than half number of times. Return -1 if no such element is found.分析如下:最容易想到的办法是,用一个hash或者map去统计每个元素的出现次数,然后得出答案。时间复杂度为O原创 2014-01-30 04:19:22 · 16036 阅读 · 0 评论 -
LeetCode主题整理(2)树问题和相关问题
Topic 1: 关于遍历 和树相关的第一个重要主题,是遍历。前序遍历(根,左子树,右子树),中序遍历(左子树,根,右子树),后序遍历(左子树,右子树,根),层序遍历(第一层也就是根节点所在的那层,第二层.....最后一层也就是叶节点所在的那层)是常见问题。 前序,中序,后序,使用递归都非常好些,比较困难的解法是,如何用非递归的方式来遍历。按照代码的难度升序,是前序,中原创 2014-01-20 05:36:07 · 1773 阅读 · 0 评论 -
LeetCode主题整理(4)链表及相关问题
Topic 1 反转链表 Reverse Linked List II 在第m~n个节点中反转单链表,注意这道题可以把代码写得很长,如果分为区间一[0,m-1],区间二[m-n],区间三[n+1,end]这三个区间的话。也可以写得很短,如果仔细观察发现其实只需要考虑第一个区间是否为NULL就可以了。 Reverse Nodes in原创 2014-02-06 01:59:04 · 3336 阅读 · 0 评论 -
LeetCode主题整理(3)实现加减乘除运算
这几道题目更多地不是侧重算法,而是侧重编程语言的基础,corner cases和edge cases是否能够考虑周全。Top1 模拟加法Add Two Numbers 给定2个链表,表示两个数,模拟加法,返回的和用链表的形式表示,解决的方法是逐个节点处理。Add Binary 给定2个string表示的二进制数,模拟加法,返回的和用string的形式表示,解原创 2014-01-23 04:01:00 · 2050 阅读 · 0 评论 -
LeetCode(137)Single NumberII
题目如下:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it withou原创 2014-02-09 10:15:27 · 4045 阅读 · 2 评论 -
LeetCode(99)Recover Binary Search Tree
题目如下:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could yo原创 2014-01-17 14:23:23 · 1250 阅读 · 1 评论 -
LeetCode(63)UniquePath2
题目如下:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2014-03-05 02:54:10 · 1619 阅读 · 0 评论 -
[面试题]在一个先增长后减小的数组中找到最大值
题目如下:Given an array of integers. Find a peak element in it. An array element is peak if it is larger than its neighbors. For corner elements, we need to consider only one neighbor. For example, for原创 2014-03-06 02:04:16 · 11104 阅读 · 3 评论 -
LeetCode(34)SearchInsertPosition
原始题目: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 a原创 2014-03-07 03:18:17 · 846 阅读 · 0 评论 -
LeetCode(90)Decode Ways
题目如下:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the tot原创 2014-03-06 15:17:34 · 824 阅读 · 0 评论 -
[面试题] Add Two String 求两个string型数字的和
题目:输入string a, string b,求a和b的和,并且将和也用string型式返回。分析:来自F的面试题,编程基本功练习。模拟加法,和leetcode上的这道题比较像。 知道如何把对string型的'9'和int型的9进行相互转换即可。我的代码:#ifndef Add_String_Solution_h#define Add_String_Solution_h#原创 2014-02-14 05:31:03 · 1195 阅读 · 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.思考分析:一开始,我觉得这道题不是考算法的,题目确实不涉及算法的内容。但是后来,我交了很久才成功,才明白这道题的坑点在于corner cases.首先看解题方法。原创 2014-03-07 13:49:08 · 3314 阅读 · 2 评论 -
LeetCode(57)LengthOfLastWord
题目如下:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word i原创 2014-01-22 16:58:17 · 1023 阅读 · 0 评论 -
LeetCode(111)Minimum Depth of Binary Tree
题目如下: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.题目分析:挺有意思的一道题目,corner cased a原创 2014-01-17 09:03:53 · 1727 阅读 · 0 评论 -
Leetcode(104)Maximum Depth of Binary Tree
题目如下:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.原创 2014-10-07 04:40:36 · 1076 阅读 · 0 评论 -
LeetCode(100)Same Tree
现在开始按照AC率降序刷题,接下来的这几道题目确实都是挺简单的。题目如下: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 n原创 2014-01-09 06:41:21 · 4159 阅读 · 0 评论 -
LeetCode(70)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?分析如下:看到这里这段评论写得挺好原创 2014-01-20 16:28:51 · 844 阅读 · 0 评论 -
LeetCode(83)Remove Duplicates from Sorted List
题目如下:Given 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->3.题目分析:本题是给定一个链表,对链表原创 2014-01-10 08:06:49 · 3987 阅读 · 1 评论 -
LeetCode(61)UniquePath1
题目如下:A 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 reach原创 2014-03-01 08:54:49 · 2273 阅读 · 0 评论 -
LeetCode(88) Merge Sorted Array
题目如下:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialize原创 2014-02-12 05:57:13 · 844 阅读 · 0 评论 -
LeetCode(110)Balanced Binary Tree
题目如下:分析如下:之前尝试一边计算深度,一边做判断,发现这是无法实现的。因为bool型的返回变量说明了返回值没有深度信息,而如果没有深度信息,那么就无法判断左子树和右子树的深度差是否为1.所以说明还要另外使用一个计算深度的函数来做辅助了。我的代码:看了一下,是个很正常的代码风格。// 104ms过大集合/** * Definition for binary tre原创 2014-01-16 07:46:33 · 6665 阅读 · 1 评论 -
LeetCode(101)Symmetric Tree
题目如下: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原创 2014-01-17 06:48:54 · 3699 阅读 · 0 评论 -
LeetCode(129)SumRoottoLeafNumbers
题目如下:Given 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 123.Find the原创 2014-02-28 15:57:49 · 1840 阅读 · 0 评论 -
LeetCode(151)Reverse Words in a String
题目如下:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".分析如下: 很基础的题目,需要考虑各种corner cases,比如下列:1 "原创 2014-03-10 02:47:53 · 4367 阅读 · 0 评论 -
LeetCode(87)Partition List
题目如下:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in ea原创 2014-01-28 15:00:55 · 1046 阅读 · 0 评论 -
LeetCode(108)Convert Sorted Array to Binary Search Tree
题目如下:分析如下:递归进行。每次找到排序数组的中点,中点左边的子排序数组构成左子树,中点右边的子排序数组构成右子树。就这样递归进行。由于每次都几乎做到了二分,所以每次的左子树和右子树的节点数量是均衡的,所以最后得到的BST树是balanced。(我不知道如何严格地证明它。)其中的边界情况是:(1) 假设当前情况只有array[0]这一个数。那么显然不需要求这段数组的中点了,这个数原创 2014-01-18 02:17:01 · 3276 阅读 · 4 评论 -
LeetCode(116) Populating Next Right Pointers in Each Node
题目如下:分析如下:我的代码:原创 2014-11-18 03:00:46 · 1145 阅读 · 1 评论 -
LeetCode(141)Linked List Cycle
题目如下:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题目分析:最常见的想法是遍历linked list,同时用个set进行记录遍历过的节点,如果遍历linked list时发现当前节点已经在set中出现原创 2014-01-10 00:46:56 · 3607 阅读 · 0 评论