自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(66)
  • 问答 (1)
  • 收藏
  • 关注

原创 剑指offer 字符串的排列

题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 输入描述: 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。以前leetcode上做过类似的题,但当时没有要求要按字典序输出一开始用了Collections.sort来排序,实在...

2018-03-31 21:49:09 125

原创 剑指offer 二叉搜索树与双向链表

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。其实两个递归是一样的,只是返回的节点不一样,一个是头,一个是尾,为了清晰把它分开了public TreeNode Convert(TreeNode pRootOfTree){ if(pRootOfTree == null) return null; ...

2018-03-31 15:18:36 91

原创 剑指offer 二叉树中和为某一值的路径

题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { ArrayList<ArrayList&lt...

2018-03-30 21:59:38 90

原创 剑指offer 二叉搜索树的后序遍历序列

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。判断数组是不是一个二叉搜索树的后序遍历由于是后序遍历,所以最后一个节点是根节点,它左边如果可以分为两段(前一段比根节点小,后一段比根节点大)那么就继续遍历它的左右子树下面是我优化过的递归版本public boolean VerifySquen...

2018-03-30 16:08:43 138

原创 剑指Offer 栈的压入、弹出序列

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)维护一个辅助栈来按照顺序出栈入栈 public boolean IsPopOrd...

2018-03-30 14:17:47 106

原创 剑指offer 包含min函数的栈

题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。这个题要求时间复杂度是o(1) 竟然想了好久没想出来,原来思维定式了,一直在想既然要从小到大排序怎么可能实现o(1)呢其实由于出栈入栈是由顺序的,用来记录最小值的栈也不用完整记录所有元素的顺序,只要根据入栈的顺序来就行 比如 入栈 9 8 10 4 5 2 如果入栈是发现比辅助栈的头元素小就入辅...

2018-03-30 13:43:11 72

原创 剑指offer 数的子结构

题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)递归每个节点,需要一个函数辅助public boolean HasSubtree(TreeNode root1,TreeNode root2) { if(root1 == null) return false; return isSubTree(root...

2018-03-30 00:13:25 192

原创 LeetCode 236. Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v ...

2018-03-29 22:24:02 87

原创 剑指offer 合并两个排序列表

题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。简单,但是有三种方法1.创建一个额外链表(最好理解,但是浪费空间)public ListNode Merge(ListNode list1,ListNode list2) { ListNode p1 = list1; ListNode p2 = l...

2018-03-29 16:16:21 194

原创 网易2019实习编程题 数对

牛牛以前在老师那里得到了一个正整数数对(x, y), 牛牛忘记他们具体是多少了。 但是牛牛记得老师告诉过他x和y均不大于n, 并且x除以y的余数大于等于k。 牛牛希望你能帮他计算一共有多少个可能的数对。这题暴力法是会超时的,主要是要找到被除数递增时余数的规律因为是正数,所以k等于0的时候要特殊考虑public static void main(String[] args) { ...

2018-03-29 00:07:25 633

原创 网易实习2019编程题 牛牛背包

牛牛准备参加学校组织的春游, 出发前牛牛准备往背包里装入一些零食, 牛牛的背包容量为w。 牛牛家里一共有n袋零食, 第i袋零食体积为v[i]。 牛牛想知道在总体积不超过背包容量的情况下,他一共有多少种零食放法(总体积为0也算一种放法)。这个题笔试的时候没有写出来import java.util.*;public class Main{ public static int ...

2018-03-29 00:03:29 583

原创 java类初始化的顺序

先从最简单的说起,我们一般是这样理解的: 先初始化静态 成员/块 然后是构造代码块,最后是构造函数 看一个例子这是两个类ShunXu 和 Otherpublic class ShunXu { public ShunXu() { System.out.println("构造函数"); } public static Other othe...

2018-03-27 15:26:21 186

原创 今日头条编程题1 找"最大"点

P为给定的二维平面整数点集。定义 P 中某点x,如果x满足 P 中任意点都不在 x 的右上方区域内(横纵坐标都大于x),则称其为“最大的”。求出所有“最大的”点的集合。(所有点的横坐标和纵坐标都不重复, 坐标轴范围在[0, 1e9) 内) 如下图:实心点为满足条件的点的集合。请实现代码找到集合 P 中的所有 ”最大“ 点的集合并输出。 输入描述: 第一行输入点集的个数 N, 接下来 N ...

2018-03-27 00:23:36 1190

原创 剑指offer 变态跳台阶

题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。首先用动态规划的思考方式跳到第i阶台阶的方法数量应为之前所有阶数各自的方法总和即dp[0] + dp[1] +dp[2] + ……+dp[i -1]注意起始位置也应该算为一种。找到规律dp[i] = 2 * dp[i -1]因此dp[i] = 2 ^ (i - 1)...

2018-03-26 16:03:34 89

原创 剑指offer 旋转数组的最小数字

题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。顺序查找当然是可以的,但那样时间复杂度就是o(n),为了使得时间达到o(logn)就要用二分查找了...

2018-03-26 15:11:45 83

原创 LeetCode 461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note: 0 ≤ x, y < 2...

2018-03-24 23:15:13 83

原创 LeetCode 733. Flood Fill

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate (sr, sc) representing the starting pixel (row and colu...

2018-03-24 23:04:10 199

原创 LeetCode 389. Find the Difference

Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was adde...

2018-03-24 15:29:39 84

原创 LeetCode 617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree...

2018-03-22 23:57:19 174

原创 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one ...

2018-03-22 23:28:49 119

原创 374. Guess Number Higher or Lower

We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number is higher or...

2018-03-21 00:51:42 115

原创 LeetCode 501. Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.Assume a BST is defined as follows:The left subtree of a node contain...

2018-03-20 23:55:21 96

原创 LeetCode 448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Coul...

2018-03-20 21:30:02 81

原创 SSM框架pom配置文件

<properties> <!-- 设置项目编码编码 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reportin...

2018-03-20 14:28:41 277

转载 java复习之HashMap和Hashtable的区别

1.两者继承结构不同 虽然都实现了Map、Cloneable、Serializable三个接口。但是HashMap继承自抽象类AbstractMap,而HashTable继承自抽象类Dictionary。其中Dictionary类是一个已经被废弃的类,这一点我们可以从它代码的注释中看到:以下代码及注释来自java.util.Dictionary* <strong>NOTE...

2018-03-20 00:29:14 143

转载 java复习之 Vector、ArrayList和LinkedList 的区别

ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数据慢。Vector中的方法由于添加了synchronized修饰,因此Vector是线程安全的容器,但性能上较ArrayList差,因此已经是Java中的遗留容器。...

2018-03-20 00:05:40 159

原创 java复习之 String,StringBuffer,StringBuilder

 最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下,便于大家观看,也便于加深自己学习过程中对这些知识点的记忆,如果哪里有误,恳请指正。  这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。首先说运行速度,或者说是执行速度,在这...

2018-03-20 00:01:00 155

原创 LeetCode 438. Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be large...

2018-03-19 20:52:58 111

原创 LeetCode 690. Employee Importance

You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his direct subordinates’ id.For example, employee 1 is the leader of employe...

2018-03-19 16:27:59 151

原创 LeetCode 543. Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may n...

2018-03-19 00:25:39 145

原创 java中遍历map的集中方法

map的遍历是经常用到的,今天就总结一下java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点先初始化一个mappublic class TestMap { public static Map<Integer, Integer> map = new Has...

2018-03-18 20:15:08 509 2

原创 LeetCode 697. Degree of an Array

Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements.Your task is to find the smallest possible length of a ...

2018-03-18 19:36:30 130

原创 LeetCode 204.Count Primes

Description:Count the number of prime numbers less than a non-negative number, n.求n之内共有几个质数 一开始用死办法求超时了,在网上看到一种 厄拉多塞筛法 时间复杂度为o(n),很不错这种方法叙述大致如下西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少...

2018-03-18 18:47:04 105

原创 LeetCode 696. Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.Substri...

2018-03-18 16:38:25 134

原创 LeetCode 405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note:All letters in hexadecimal (a-f) must be in lowercase. The hexadecima...

2018-03-17 14:46:18 167

原创 关于 原码 反码 与 补码

今天做leetcode 405. Convert a Number to Hexadecimal要求把一个数转化成二进制十六进制,是补码形式的正数的时候很容易,但到了负数就有点懵逼了.然后恶补了一下原码 反码 补码 的知识,成功解题1. 原码所谓原码就是符号位加上数字的二进制表示,int为例,第一位表示符号 (0正数 1负数)简单期间一个字节表示+7的原码为: 000...

2018-03-17 14:31:14 641

原创 606. Construct String from Binary Tree

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. And you ...

2018-03-17 10:09:35 129

原创 492. Construct the Rectangle

For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L...

2018-03-16 01:34:28 105

原创 LeetCode 596. Classes More Than 5 Students

There is a table courses with columns: student and classPlease list out all classes which have more than or equal to 5 students.For example, the table:+---------+------------+| student | clas...

2018-03-16 00:54:11 177

原创 LeetCode 563. Binary Tree Tilt

Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree ...

2018-03-15 23:40:00 116

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除