
leetcode题解
吴孟达
关注微信公众号"程序员达叔",免费领取海量学习资料
展开
-
【leetcode】将整数取反,-321返回-123(Reverse Integer)
题目描述如下:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321一目了然,知道了题目的意思了。。下面给出的我一开始的想法public int reverse(int num){ StringBuilder sb=new String原创 2013-12-28 23:04:40 · 2918 阅读 · 1 评论 -
【leetcode Java】二叉树的递归遍历以及最大深度的求解(Java)
递归是非常神奇的方法,代码看起来很简洁。对二叉树的遍历和求最大深度可以用递归的方法,主要思路就是遍历左子树,再遍历右子树。如果左子树上面的结点,有右孩子,则调用右子树的方法;遍历到左子树的叶节点的时候,返回,开始遍历右子树。如果右子树上面的结点有左孩子,则调用左子树的方法,遍历到右子树的叶子结点的时候,程序结束。static void scanNodes(TreeNode root){ i原创 2013-12-20 09:36:25 · 11406 阅读 · 0 评论 -
【leetcode SQL】Second Highest Salary
原题如下:Write a SQL query to get the second highest salary from the Employee table.+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+--------+原创 2015-01-31 21:13:14 · 2767 阅读 · 0 评论 -
【leetcode SQL】Combine Two Tables
突然发现leetcode出sql题了,找了道最简单的,找找自信。。题目如下:Table: Person+-------------+---------+| Column Name | Type |+-------------+---------+| PersonId | int || FirstName | varchar || LastNam原创 2015-01-31 18:33:59 · 3590 阅读 · 0 评论 -
【leetcode SQL】Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.Table: Customers.+----+-------+| Id | Na原创 2015-02-01 22:06:54 · 2854 阅读 · 0 评论 -
【leetcode SQL】Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.+----+-------+--------+-----------+| Id | Name | Salary |原创 2015-02-01 21:20:48 · 3092 阅读 · 0 评论 -
【leetcode Java】Length of Last Word
前段时间忙数据交换,有一个多月没写java了。。找找手感,刷刷题吧。。去leetcode上挑了个简单的,题目是这样的。Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the strin原创 2015-01-31 18:16:17 · 1472 阅读 · 0 评论 -
【leetcode Java】Excel Sheet Column Number
题目如下:Given 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 AB -> 28 可原创 2015-02-01 00:45:39 · 4319 阅读 · 0 评论 -
【leetcode】数组中找出只出现一次的数字(Single Number)
题目是这样说的: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 wit原创 2013-12-23 12:28:08 · 2295 阅读 · 0 评论 -
一段代码让你理解二叉树的递归奥秘
class TreeNode{ TreeNode left; TreeNode right; int val; TreeNode(int val){ this.val=val; } static void scanNodes(TreeNode root){ if(root!=null){ System.out.println("this is node :"+root原创 2013-12-23 10:21:12 · 4609 阅读 · 1 评论 -
【leetcode】非递归先序遍历二叉树(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: R原创 2013-12-25 11:31:53 · 2088 阅读 · 0 评论 -
【leetcode】比较两棵二叉树是否相同(Same Tree)
题目要求如下: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 nodes have the same value.原创 2013-12-23 22:46:54 · 3042 阅读 · 0 评论 -
【leetcode】在一堆每个数字都出现三次的数组中,找到那个只出现一次的数(Single Number II)
题目的描述是这样的: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 wit原创 2013-12-28 19:38:32 · 5521 阅读 · 0 评论 -
【leetcode Java】Majority Element
题目如下Given 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 majority原创 2015-02-01 15:44:17 · 2766 阅读 · 0 评论