链表
1、输入一个链表,从尾到头打印链表每个节点的值。
解题思路:需要用到栈Stack和数组列表ArrayList,先链表listNode.next一个一个遍历把对象push到栈里,再从栈pop()出来添加到ArrayList,最后返回ArrayList
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack=new Stack<Integer>();
while(listNode!=null)
{
stack.push(listNode.val);
listNode=listNode.next;
}
ArrayList<Integer> arraylist=new ArrayList<Integer>();
while(!stack.isEmpty())
{
arraylist.add(stack.pop());
}
return arraylist;
}
}
数组
1、在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
解题思路:从二维数组的左下角array[len][i] 开始查询,根据题意,左下角向上(len–)递减,向右(i++)递增,那我们跟target比较,如果target小就向上(len–),target大就向右(i++),直到得到该target就返回true,没有就返回false
public class Solution {
public boolean Find(int target, int [][] array) {
int len = array.length-1;
int i = 0;
while((len >= 0)&& (i < array[0].length)){
if(array[len][i] > target){
len--;
}else if(array[len][i] < target){
i++;
}else{
return true;
}
}
return false;
}
}
2、把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
解题思路:使用二分查找,使用两个指针left,right,分别指向前子数组的最大数和后子数组的最小数
(1)当中间元素大于第一个元素,那么中间元素位于前子数组,最小元素位于中间元素的后面,我们可以让第一个left指向中间元素
(2)当中间元素小于于第一个元素,那么中间元素位于后子数组,最小元素位于中间元素的前面,我们可以让第二个right指向中间元素
第一个指针left总是指向前面递增数组的元素,第二个指针right总是指向后面递增的数组元素。
最终第一个指针将指向前面数组的最后一个元素,第二个指针指向后面数组中的第一个元素。
这道题说的是非递减也就是可以会重复,因此上面的二分查找不可以
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length==0)
return 0;
int count=0;
for(int i=1;i<array.length;i++)
{
if(array[i-1]>array[i])
count=i;
}
return array[count];
}
}
3、输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
解题思路:new一个新的数组
public class Solution {
public void reOrderArray(int [] array) {
if(array.length==0)
{
return ;
}
int[] newarray=new int[array.length];
int count=0;
for(int i=0;i<array.length;i++)
{
if(array[i]%2==1)
{
newarray[count++]=array[i];
}
}
for(int i=0;i<array.length;i++)
{
if(array[i]%2==0)
{
newarray[count++]=array[i];
}
}
for(int i=0;i<array.length;i++)
array[i]=newarray[i];
}
}
讨论的同学说这道题其实是考察快排的变种
树
1、如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
首先,明确概念:
堆是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于(或不小于)其左孩子和右孩子节点的值。
根结点(亦称为堆顶)的关键字是堆里所有结点关键字中最小者的堆称为小根堆。
根结点(亦称为堆顶)的关键字是堆里所有结点关键字中最大者,称为大根堆。
借助类PriorityQueue 可以实现小根堆和大根堆。
对于PriorityQueue ,观察帮助文档,可以发现,这是jdk1.5以后引入的,
对它的说明如下:An unbounded priority queue based on a priority heap,The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
The head of this queue is the least element with respect to the specified ordering.
由此可知,它容量没有界限,且默认排序是自然排序,队头元素是最小元素,故我们可以拿来作为小根堆使用。
(要注意:默认的PriorityQueue并非保证了整个队列都是有序的,只是保证了队头是最小的)
对于大根堆,就要借助于comparator比较器,来实现大根堆。(使用默认的初始容量:11)
PriorityQueue <Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2.compareTo(o1);
}
});
这样就实现了,大根堆的功能。
下面代码思路:大的树放在小根堆,小的树放在大根堆
import java.util.Comparator;
import java.util.PriorityQueue;
public class Solution {
private int count = 0;
private static PriorityQueue<Integer> minHeap = new PriorityQueue<>();
private static PriorityQueue<Integer> maxHeap = new PriorityQueue<>(15, new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o2 - o1;
};
});
public void Insert(Integer num) {
if (count % 2 == 0) {
// 当数据总数为偶数,新加入的元素,应当进入小根堆
// 要经过大根堆筛选后去大根堆最大元素进入小根堆
// 1、新加入的元素先加入大根堆,由大根堆筛选堆中最大元素
maxHeap.offer(num);// 添加元素
// 2、筛选后的大根堆最大元素poll出加入小根堆
int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
} else {
// 为奇数,新加入元素进入大根堆
// 先加入小根堆筛选出最小元素,然后加入大根堆
minHeap.offer(num);
int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
}
public Double GetMedian() {
if (count % 2 == 0) {
return new Double((minHeap.peek() + maxHeap.peek())) / 2;// 返回minHeap返回首元素
} else {
return new Double(minHeap.peek());
}
}
public static void main(String[] args) {
Solution s = new Solution();
for (int i = 0; i < 10; i++) {
s.Insert(i);
}
System.out.println(s.GetMedian());
for(int i=0;i<minHeap.size();i++){
System.out.print(minHeap.poll()+" ");
}
System.out.println("/");
for(int i=0;i<maxHeap.size();i++){
System.out.print(maxHeap.poll()+" ");
}
}
}
2、给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
解题思路:
1、如果为空树,返回null;
2、如果该结点的右孩子不为空,就返回该结点右孩子的左孩子,如果没有该左孩子,返回该结点的右孩子
3、如果该结点不是根结点,返回该结点是其父节点的左孩子,就返回其父节点,否则往上遍历
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<TreeLinkNode> list=new ArrayList<>();
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode==null)
return null;
if(pNode.right!=null){
pNode=pNode.right;
if(pNode.left!=null)
return pNode.left;
return pNode;
}
while(pNode.next!=null)
{
if(pNode==pNode.next.left)
return pNode.next;
pNode=pNode.next;
}
return null;
}
}
栈和队列
1、用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
解题思路:先把元素push到stack1中,这时候是倒序的,再把元素从stack1栈pop到stack2,然后元素在stack2是正顺序,最后把元素从stack2栈pop出就效果等于一个队列,因为栈是先进后出,队列是先进先出,正好相反,两个栈可以起到负负得正的效果
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if(stack1.empty()&&stack2.empty())
{
throw new RuntimeException(" ");
}
if(stack2.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
查找和排序
字符串
1、请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
解题思路:先遍历求出空格数,再根据空格数转换成%20之后的str长度,通过setLength()设置字符串的新长度,把旧的字符串往新的字符串重新设置,遇到空格用20%替换
public class Solution {
public String replaceSpace(StringBuffer str) {
int spacenum = 0;//spacenum为计算空格数
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ')
spacenum++;
}
int indexold = str.length()-1; //indexold为为替换前的str下标
int newlength = str.length() + spacenum*2;//计算空格转换成%20之后的str长度
int indexnew = newlength-1;//indexold为为把空格替换为%20后的str下标
str.setLength(newlength);//使str的长度扩大到转换成%20之后的长度,防止下标越界
for(;indexold>=0 && indexold<newlength;--indexold){
if(str.charAt(indexold) == ' '){ //
str.setCharAt(indexnew--, '0');
str.setCharAt(indexnew--, '2');
str.setCharAt(indexnew--, '%');
}else{
str.setCharAt(indexnew--, str.charAt(indexold));
}
}
return str.toString();
}
}
其他
1、求二进制中1的个数
普通法
int BitCount(unsigned int n){
unsigned int c=0;//计数器
while(n>0){
if(n%1==1){//判断末位是不是1
c++;//计数器加1
}
n>>=1;//相当与除以2,向右移位
}
return c;
}
普通法的精简版
int BitCount(unsigned int n){
unsigned int c=0;
for(c=0;n>0;n>>1)
{
c+=n%1;
}
return c;
}
快速法
这种方法是通过n&(n-1)来清除最右边(最低位)的1,也就是二进制数有多少1就循环多少次
int BitCount(unsigned int n){
unsigned int c=0;
for(c=0;n;++c){
n&=(n-1);// 清除最低位的1
}
return c;
}
为什么n &= (n – 1)能清除最右边的1呢?因为从二进制的角度讲,n相当于在n - 1的最低位加上1。举个例子,8(1000)= 7(0111)+ 1(0001),所以8 & 7 = (1000)&(0111)= 0(0000),清除了8最右边的1(其实就是最高位的1,因为8的二进制中只有一个1)。再比如7(0111)= 6(0110)+ 1(0001),所以7 & 6 = (0111)&(0110)= 6(0110),清除了7的二进制表示中最右边的1(也就是最低位的1)
跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法
public class Solution {
public int JumpFloor(int target) {
if(target<=2){
return target;
}
else{
return JumpFloor(target-1)+JumpFloor(target-2);
}
}
}