Java编程
冰美式
Stay hungry.Stay foolish
展开
-
2019京东校招笔试题 合唱队形
合唱队形题目描述:合唱队的N名学生站成一排且从左到右编号为1到NNN,其中编号为i的学生身高为HiH_iHij。现在将这些学生分为若干组(同一组的学生编号连续),并且让每组学生从左到右按照身高从低到高排序,使得最后所有学生同样满足从左到右身高从低到高(中间位置可以相等),那么最多能将这些学生分成多少组呢?问题分析利用辅助空间,复制一个数组,进行排序,记为copy。注意到,arr数组在分...原创 2019-08-24 21:51:34 · 841 阅读 · 0 评论 -
二叉树的下一个节点
题设要求:给定一个二叉树和其中的一个节点,请找出中序遍历顺序的下一个节点并返回。问题分析:首先,中序遍历的顺序是左-根-右。由此,我们可以对当前节点分情况讨论。情况1 如果当前节点有右子树,那么下个节点就是右子树最左边的节点。情况2 如果当前节点没有右子树,再分两种情况:情况2.1 是父节点左孩子,父节点是下一节点;情况2.2 是父节点的右孩子,找他的父节点的父节点,直到当前节点是其父...原创 2019-04-01 17:17:20 · 130 阅读 · 0 评论 -
Two Sum
题目描述:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the s...原创 2019-04-04 16:20:55 · 108 阅读 · 0 评论 -
Add Two Numbers
题目描述: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and re...原创 2019-04-04 16:01:54 · 106 阅读 · 0 评论 -
不用加减乘除做加法
题目描述:写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。问题分析:加减乘除都不能用的时候,自然就会想到位运算。位运算是对二进制而言的,我们可以将考虑的数字想像成二进制表示的(实际在计算机中就是二进制的),0+0=0,1+0=1,0+1=1,1+1=0(进位1)代码如下:public class Solution{ public static void ma...原创 2019-04-03 22:25:27 · 123 阅读 · 0 评论 -
Java编程小技巧
1、不用新的变量交换两个变量的值1)加减法a = a+b;b = a-b;a = a-b;2)异或运算a = a^b;b = a^b;a = a^b;2、移位代替乘除2注意,除法ed效率比移位运算要低得多,在实际编程中尽可能地采用移位运算代替乘除法。a = a<<1; //乘以2a = a>>1; //除以23、二进制最右边的1变成0...原创 2019-04-03 22:16:50 · 363 阅读 · 0 评论 -
不用乘除法和循环对1+2+...+n求和
题目描述:求1+2+3+...+n1+2+3+...+n1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)问题分析:1+2+...+n1+2+...+n1+2+...+n最直观的求解方法是(1+n)∗n/2(1+n)*n/2(1+n)∗n/2,考虑到题目不允许用乘除法和循环,如何实现“循环加”的过程呢?可以借...原创 2019-04-03 22:01:35 · 801 阅读 · 0 评论 -
HashMap知识点整理
以HashMap<String, Car>为例,验证HashMap中的插入元素、删除元素、查找元素等方法。示例所用的HashMap<key,value>的key 是carID,public class Car { String CarID; int maxVelocity; //重写了equals方法 public boolean equa...原创 2019-03-29 18:37:35 · 192 阅读 · 0 评论 -
ArrayList知识点整理
ArrayList的遍历ArrayList主要有三种遍历方式,for循环遍历、for each遍历、iterator遍历,仍旧以以Car为例,具体遍历方法如下:public class Car { String CarID; int maxVelocity; public boolean equals(Car car) { if(this.CarID.equals...原创 2019-03-22 13:24:33 · 292 阅读 · 0 评论 -
动态规划求解连续子数组的最大和
给定一个整数数组,数组里有正有负。数组中的一个活连续多个元素组成一个子数组。求所有子数组的和的最大值。一、枚举所有可能——最基础的解法最直观的解法就是,计算出所有可能组合的子数组的和,返回最大的和。这种解法的复杂度最低也要O(n)O(n)O(n)。import java.util.ArrayList;import java.util.Collections;public class So...原创 2019-03-13 16:19:00 · 1190 阅读 · 0 评论 -
利用正则表达式判断字符串是否表示数值
题设:实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示数值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。分析:如何判断字符串是否为数字?首先,其首位可以是代表正负的符号位,即“+”或“-”,但不是必要的;其次,判断整数部分,其可以为0-9的原创 2019-03-17 22:14:49 · 2186 阅读 · 1 评论 -
最少应该准备多少奖品
有nnn个人参加编程比赛,比赛结束后每个人都得到一个分数;现在所有人排成一圈(第1个和第nnn个相邻)领取奖品,要求:1、如果某个人的分数比左右的人高,那么奖品数量也要比左右的人多;2、每个人至少得到一个奖品;问最少准备多少个奖品。public class Solution { public static void main(String[] args) { int Y = 2;...原创 2019-03-16 14:26:14 · 661 阅读 · 0 评论 -
对称的二叉树
题目要求: 请实现一个函数,用来判断一棵二叉树是不是对称地。注意:如果一个二叉树同此二叉树的镜像是相同的,定义其为对称的。问题分析: 可以通过比较二叉树的前序遍历序列和对称前序遍历序列来判断二叉树是不是对称的。如果这两个序列是一样的,那么二叉树就是对称的。实现代码:/*public class TreeNode { int val = 0; TreeNode left = ...原创 2019-04-01 19:51:12 · 124 阅读 · 0 评论 -
按之字形顺序打印二叉树
题目要求: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二行按照从右到左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。问题分析: 借助stack的数据结构,存放node。构建两个stack,分别用于存放奇数层节点和偶数层节点。代码如下:import java.util.ArrayList;import java.util.Stack;public ...原创 2019-04-01 20:18:32 · 113 阅读 · 0 评论 -
把二叉树打印成多行
题目要求:从上到下打印二叉树,同一层节点从左到右输出。每层输出一行。问题分析:和https://blog.csdn.net/weixin_43277507/article/details/88956331类似,这里可以理解为都是从左到右打印。代码如下:import java.util.ArrayList;import java.util.LinkedList;/*public...原创 2019-04-01 20:56:05 · 116 阅读 · 0 评论 -
二叉树的前序、中序、后序、层序、ZigZag遍历(递归和非递归方法)
//二叉树前序遍历--递归实现 List<Integer> res = new ArrayList<>(); public List<Integer> preorderTraversal(TreeNode root) { if(root==null) return res; res.add(root.v...原创 2019-07-05 21:22:34 · 229 阅读 · 0 评论 -
生成括号
题设要求:如果有n对括号,它们一共有多少种组合呢?譬如对于n=3n=3n=3,有以下5种情况:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”问题分析:一对括号表示左括号“(” 和右括号 “)”是成对出现的。代码: /* * generate-parentheses * Given n pairs of parentheses...原创 2019-05-07 15:20:59 · 155 阅读 · 0 评论 -
寻找只出现一次的数字
题目要求: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 without using ex...原创 2019-04-22 21:00:16 · 363 阅读 · 0 评论 -
用lambda表达式排序
在老版本的Java中,需要给Collections.sort( )传入一个对象和比较器来实现按照指定顺序排列,一般是创建一个匿名的比较器对象传递给sort方法。在Java 8 中,我们可以利用lambda表达式更简洁的实现上述排序。具体代码如下:import java.util.Arrays;import java.util.Collections;import java.util.Com...原创 2019-04-13 10:50:34 · 4643 阅读 · 1 评论 -
扑克牌顺子
题目描述:LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张...原创 2019-04-02 16:39:27 · 140 阅读 · 0 评论 -
滑动窗口的最大值
题目描述:给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5...原创 2019-04-02 15:45:21 · 109 阅读 · 0 评论 -
数据流中的中位数
题目要求:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。问题分析:首先,我们得到的是数据流,需要用一个数据结构来保存从数据流中读出的数据。数组、链表、二叉搜索树、...原创 2019-04-02 15:34:36 · 108 阅读 · 0 评论 -
二叉搜索树的第 k 个节点
题目要求:给定一棵二叉搜索树,请找出其中的第k小的节点。例如,(5,3,7,2,4,6,8)中,按节点数值大小顺序第三小节点的值为4.问题分析:如果按照中序遍历的顺序遍历一棵二叉搜索树,则遍历序列是按数值递增排序的。因此,只需要用中序遍历一棵二叉搜索树,就能得到它的第kkk大节点。代码如下:/*public class TreeNode { int val = 0; Tre...原创 2019-04-02 14:19:50 · 369 阅读 · 0 评论 -
序列化二叉树
题目要求: 请实现两个函数,分别用来序列化和反序列化二叉树问题分析: 1. 对于序列化:使用前序遍历,递归的将二叉树的值转化为字符,并且在每次二叉树的结点不为空时,在转化val所得的字符之后添加一个’ , '作为分割。对于空节点则以 ‘#’ 代替。2. 对于反序列化:按照前序顺序,递归的使用字符串中的字符创建一个二叉树(特别注意:在递归时,递归函数的参数一定要是char ,这样才能保证每次...原创 2019-04-01 22:05:47 · 124 阅读 · 0 评论 -
最长连续数组
24、最长连续数组longest-consecutive-sequence:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecut...原创 2019-03-07 21:38:14 · 569 阅读 · 0 评论 -
合并排序序列
21、合并排序序列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 o...原创 2019-03-06 10:07:35 · 282 阅读 · 0 评论 -
回文数
20、回文数palindrome-number: Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the ...原创 2019-03-06 09:53:45 · 228 阅读 · 0 评论 -
下一个排列
19、下一个排列next-permutation: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange i...原创 2019-03-05 15:37:35 · 188 阅读 · 0 评论 -
排序数组转化二叉查找树
18、排序数组转化二叉查找树convert-sorted-array-to-binary-search-tree:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题设要求:将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。问题分析:AVL...原创 2019-03-05 15:32:56 · 410 阅读 · 0 评论 -
消除间隔
17、消除间隔merge-intervals:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].题设要求:给定区间的集合,合并所有重叠的区间。例如,给定区间集合[1,3...原创 2019-03-05 14:51:36 · 254 阅读 · 0 评论 -
删除元素
16.删除元素remove-element: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn’t matter what you leave beyond ...原创 2019-03-05 13:30:21 · 400 阅读 · 0 评论 -
第一个丢失的正数
15、第一个丢失的正数first-missing-positive: Given an unsorted integer array, find the first missing positive integer.For example, Given[1,2,0] return3, and[3,4,-1,1] return2.Your algorithm should run in O(n...原创 2019-03-05 13:20:18 · 262 阅读 · 0 评论 -
3数组合最近值
14、3数组合最近值3sum-closest:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each ...原创 2019-03-05 13:12:10 · 224 阅读 · 0 评论 -
螺旋矩阵-II
Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.For example,Given n =3, You should return the following matrix:[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ]...原创 2019-03-05 11:50:49 · 296 阅读 · 0 评论 -
螺旋矩阵-I
12、螺旋矩阵-Ispiral-matrix: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Given the following matrix:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Y...原创 2019-03-05 10:36:47 · 310 阅读 · 0 评论 -
在二维矩阵中找目标数
11、在二维矩阵中找目标数search-a-2d-matrix:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right....原创 2019-03-05 10:08:51 · 419 阅读 · 0 评论 -
能装最多水的容器
22、能装最多水的容器container-with-most-water: Given n non-negative integers a1 , a2 , …, an , where each represents a point at coordinate (i, ai ). n vertical lines are drawn such that the two endpoints of l...原创 2019-03-06 10:37:27 · 400 阅读 · 0 评论 -
包围区域
23、包围区域surrounded-regions: Given a 2D board containing’X’and’O’, capture all regions surrounded by’X’.A region is captured by flipping all’O’s into’X’s in that surrounded region .For example,X X X ...原创 2019-03-06 13:19:41 · 370 阅读 · 0 评论 -
从排序数组中删除重复项
1、从排序数组中删除重复项remove-duplicates-from-sorted-array: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space f...原创 2019-03-03 19:37:00 · 1299 阅读 · 2 评论 -
寻找只出现一次的数字
题目要求:一个整型数组里除了两个数字之外,其他的数字都出现了偶数次,请写程序找出这两个只出现一次的数字。程序要求如下://num1,num2分别为长度为1的数组。传出参数//将num1[0],num2[0]设置为返回结果问题分析:建立一个hashmap,key为数字,value为相应数字出现的次数。再找出value为1的key即可。import java.util.ArrayList;i...原创 2019-03-15 10:12:34 · 151 阅读 · 0 评论