面试题21:包含min函数的栈

import java.util.Stack;

public class Solution {
private Stack<Integer> stack = new Stack<Integer>();
private Stack<Integer> minStack = new Stack<Integer>();
public void push(int node) {
if(minStack.isEmpty() || node <= minStack.peek()){
minStack.push(node);
}
stack.push(node);
} public void pop() {
if(stack.peek().equals(minStack.peek())){
minStack.pop();
}
stack.pop();
} public int top() {
return stack.peek();
} public int min() {
if(!minStack.isEmpty()){
return minStack.peek();
}else{
return stack.peek();
}
}
}

面试题22:栈的压入、弹出序列

import java.util.ArrayList;
import java.util.Stack; public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
boolean flag = false;
int n = pushA.length;
if(n > 0){
int i=0,j=0;
Stack<Integer> s = new Stack<Integer>();
while(j < n){
while(s.isEmpty() || s.peek()!=popA[j]){
if(i == n) break;
s.push(pushA[i]);
i++;
}
if(s.peek()!=popA[j]){
break;
}
s.pop();
j++;
}
if(s.isEmpty() && j==n){
flag = true;
}
}
return flag;
}
}

面试题23:从上往下打印二叉树

public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
Queue<TreeNode> que = new LinkedList<TreeNode>();
if(root == null) return res;
que.offer(root);
while(!que.isEmpty()){
int levelNum = que.size();
for(int i=0;i<levelNum;i++){
if(que.peek().left != null) que.offer(que.peek().left);
if(que.peek().right!= null) que.offer(que.peek().right);
res.add(que.poll().val);
}
}
return res;
}
}

面试题24:

import java.util.*;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence == null || sequence.length == 0) return false;
int len = sequence.length;
int rootVal = sequence[len-1];
int i=0;
for(;i<len-1;i++){
if(rootVal < sequence[i]){
break;
}
}
int j=i;
for(;j<len-1;j++){
if(rootVal > sequence[j]){
return false;
}
}
boolean judgeLeft = true;
if(i>0){judgeLeft = VerifySquenceOfBST(getSeq(sequence,0,i-1));}
boolean judgeRight = true;
if(i<len-1){judgeRight = VerifySquenceOfBST(getSeq(sequence,i,len-2));}
return (judgeLeft && judgeRight);
}
public int[] getSeq(int[] seq,int start,int end){
int[] ret = new int[end-start+1];
for(int i=start;i<=end;i++){
ret[i-start] = seq[i];
}
return ret;
}
}

面试题25:二叉树中和为某一值的路径

public class Solution {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tmp = new ArrayList<Integer>();
findHelp(res,tmp,root,0,target);
return res;
}
public void findHelp(ArrayList<ArrayList<Integer>> res,ArrayList<Integer> tmp,TreeNode root , int curSum,int target){
if(root == null) return;
curSum += root.val;
ArrayList<Integer> tmpCur = new ArrayList<Integer>(tmp);
tmpCur.add(root.val);
if(root.left == null && root.right==null && curSum == target) {res.add(tmpCur);return;}
else if(curSum > target) {return;}
else if(curSum < target){
findHelp(res,tmpCur,root.left,curSum,target);
findHelp(res,tmpCur,root.right,curSum,target);
}
return;
}
}

面试题26:复杂链表的复制

public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if(pHead == null) return null;
RandomListNode p = pHead;
while(p != null){
RandomListNode pClone = new RandomListNode(p.label);
pClone.next = p.next;
pClone.random = null;
p.next = pClone;
p = p.next.next;
}
p = pHead;
while(p != null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
} RandomListNode pCloneHead = new RandomListNode(0);
RandomListNode pCloneNode = new RandomListNode(0);
p = pHead;
if(p != null){
pCloneHead = pCloneNode = p.next;
p.next = pCloneNode.next;
p = p.next;
}
while(p != null){
pCloneNode.next = p.next;
pCloneNode = pCloneNode.next;
p.next = pCloneNode.next;
p = p.next;
}
return pCloneHead;
}
}

面试题27:二叉搜索树与双向链表

public class Solution {
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null) return pRootOfTree;
pRootOfTree = convertHelp(pRootOfTree);
while(pRootOfTree.left!=null) pRootOfTree = pRootOfTree.left;
return pRootOfTree;
}
public TreeNode convertHelp(TreeNode root){
if(root == null) return root;
if(root.left != null){
TreeNode left = convertHelp(root.left);
while(left.right!=null) left = left.right;
left.right = root;
root.left = left;
}
if(root.right != null){
TreeNode right = convertHelp(root.right);
while(right.left!=null) right = right.left;
root.right = right;
right.left = root;
}
return root;
}
}

面试题28:字符串的排列

import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public ArrayList<String> Permutation(String str) {
ArrayList<String> res = new ArrayList<String>();
if(str == null || str.length()>9 || str.length()<=0){
return res;
}
str= str.trim();
char[] arr = str.toCharArray();
permuHelp(arr,0,res);
Collections.sort(res);
return res;
}
public void permuHelp(char[] arr,int start,ArrayList<String> res){
if(arr.length - 1 == start){
res.add(new String(arr));
return;
}
for(int i=start;i<arr.length;i++){
if(i != start && arr[i]==arr[start]) continue;
char tmp = arr[start];
arr[start] = arr[i];
arr[i] = tmp; permuHelp(arr,start+1,res); tmp = arr[start];
arr[start] = arr[i];
arr[i] = tmp;
}
}
}

面试题29:数组中出现次数超过一半的数字

public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
if(array.length == 0) return 0;
int res = array[0];
int cnt = 1;
for(int i=1;i<array.length;i++){
if(res == array[i]){
cnt++;
}else{
cnt--;
} if(cnt == 0){
res = array[i];
cnt = 1;
}
}
cnt = 0;
for(int i=0;i<array.length;i++){
if(array[i] == res){
cnt++;
}
}
if(cnt>array.length/2) return res;
else return 0;
}
}

面试题30:最小的K个数

import java.util.PriorityQueue;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Queue;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(input.length==0 || k>input.length || k==0) return res;
Comparator<Integer> com = new Comparator<Integer>(){
public int compare(Integer a , Integer b){
if(a>b) {return 1;}
else if(a<b) {return -1;}
else {return 0;}
}
};
Queue<Integer> pq = new PriorityQueue<Integer>(input.length,com);
for(int i=0;i<input.length;i++){
pq.add(input[i]);
}
for(int i=0;i<k;i++){
res.add(pq.poll());
}
return res;
}
}

剑指offer题目21-30的更多相关文章

  1. 【剑指Offer】剑指offer题目汇总

      本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...

  2. 代码题 — 剑指offer题目、总结

    剑指offer题目总结:  https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...

  3. 剑指offer题目系列三(链表相关题目)

    本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...

  4. 再来五道剑指offer题目

    再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...

  5. 剑指 Offer 题目汇总索引

    剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格              ...

  6. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

  7. 剑指offer题目java实现

    Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...

  8. 剑指offer题目系列一

    本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...

  9. 剑指offer题目系列二

    本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...

  10. 剑指offer题目解答合集(C++版)

    数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...

随机推荐

  1. OpenStack nova VM migration (live and cold) call flow

    OpenStack nova compute supports two flavors of Virtual Machine (VM) migration: Cold migration -- mig ...

  2. 【freemaker】之获取xml的值

    测试代码 @Test public void test09() throws Exception{ root.put("doc", NodeModel.parse(new Inpu ...

  3. HTML 事件属性_03

    全局事件属性 HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 如果你想学习更多关于事件属性,请访问 Jav ...

  4. MyBatis Sql语句中的转义字符

    1.在xml的sql语句中,不能直接用大于号.小于号要用转义字符 如果用小于号会报错误如下: org.apache.ibatis.builder.BuilderException: Error cre ...

  5. jquery是如何架构的.

    心里一直有个疑问. jquery是如何做到一个jQuery即可以当方法用比如$();又可以当对象用$.extend(); 现在总结一下吧 function method(){} var m=new m ...

  6. 样例20-汽车SHOW

    观看样例点这里 素材下载 1.设置场景大小为400*3002.执行:文件->导入->导入到库,选择需要的汽车图片文件,将其导入到库面板中3.按照同样的方式,在库面板中导入所需的背景音乐文件 ...

  7. Angular学习

    一.Angular是什么 基于JS的框架,类似JQuery,利用数据绑定和依赖注入实现页面数据的渲染,无需人为写大量的JS,减少了代码量,优美了代码. 二.Angular优缺点 Angular适用与C ...

  8. [EventBus源码解析] EventBus.register 方法详述

    前情概要 在上一篇中,介绍了EventBus的基本使用方法,以及一部分进阶技巧.本篇及以后的几篇blog将会集中解析EventBus.java,看看作者是如何优雅地实现这个看似简单的事件分发/接收机制 ...

  9. C# IO

    在.NET框架中进行的所有IO操作都要用到流(Stream). System.IO命名空间中包含许多IO相关的类,C#文件读写的类几乎都在其中,下面对其进行详细介绍. 主要类列表: 类 说明 Bina ...

  10. php产生随机数函数

    <?php function generate_code($length = 4) { return rand(pow(10,($length-1)), pow(10,$length)-1); ...