
leetcode解题报告
在leetcode上刷题时看到的一些比较有意思的题目和解题方案。
lxydo
这个作者很懒,什么都没留下…
展开
-
[leetcode] Sum of Two Integers--用位运算实现加法运算
问题:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example:Given a = 1 and b = 2, return 3.分析:这里要求我们不能用加法、减法等运算符来实现加法运算。这里应该使用位运算来实现加法运算,实际上,这也是计算机CPU内部实原创 2016-07-06 21:13:22 · 6834 阅读 · 0 评论 -
[leetcode] Pow(x, n)
题目:Implement pow(x, n).分析: 题目很短,就是实现pow求幂函数,直觉告诉我,这个题目的主要要求是降低程序的时间复杂度,果不其然,提交了一份带有while循环复杂度是O(n)的代码,返回“Time Limit Exceed“的错误,初次提交代码:class Solution {public: double myPow(double x, int n) {原创 2016-03-23 19:41:17 · 1655 阅读 · 0 评论 -
[leetcode]Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2016-03-22 19:41:51 · 1304 阅读 · 0 评论 -
[leetcode]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.分析: 要得出二叉树中所有从根到叶子节点路径中,经过结点最少路径上的节点数。原创 2016-03-16 18:16:46 · 1223 阅读 · 0 评论 -
[leetcode]Path Sum--巧用递归
题目: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 =原创 2016-03-14 13:50:54 · 1695 阅读 · 0 评论 -
leetcode Binary Tree Paths
题目:Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]分析:让你对一棵树进行遍历,找出所有从跟节点到到叶子节点的原创 2015-12-11 21:01:55 · 1141 阅读 · 0 评论 -
[leetcode] 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 the following is原创 2016-03-12 23:13:55 · 1371 阅读 · 0 评论 -
LeetCode Maximum Subarray 通俗讲解解法的背后原理
题目:Find 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 [4,−1,2,1] has t原创 2015-10-25 18:10:01 · 5567 阅读 · 2 评论 -
LeetCode ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I原创 2015-10-14 22:27:47 · 1533 阅读 · 0 评论 -
LeetCode Single Number III (Java和Python代码)
题目Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums = [1原创 2015-10-13 17:06:17 · 1108 阅读 · 0 评论 -
LeetCode Plus One
问题:Given 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.解决:public class Solution {原创 2015-10-11 22:01:54 · 614 阅读 · 0 评论 -
Leetcode Move Zeros
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct原创 2015-10-09 17:48:54 · 976 阅读 · 0 评论