面试题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. 在 github 上获取源码

    比如访问: https://github.com/alifellod/NWebGather 可以找到下面的源码获取方式. 在github开源项目的页面上可以找到上图的三个获取源码方式. 使用git的网 ...

  2. 修复eclipse中使用mave update project后JRE都变成1.5的问题

    在项目中的parent pom中添加如下代码即可 <properties> <project.build.sourceEncoding>UTF-8</project.bu ...

  3. android html 图片处理类--加载富文本工具类

    在android开发中,一些资讯类页面,里面有html标签和图片,html 标签一般通过Html.fromHtml方法,即可以解决,但是如果html 有图片标签,那么,Html.fromHtml 好像 ...

  4. 在excel 中某一单元格添加指定字符的示例

    ="select TestSurveyID,'http://www.findoout.cn/survey/viewsurvey.aspx?tid='+CONVERT(varchar(10), ...

  5. Xcode取消某条警告

      [Xcode取消某条警告] 像下面这样,把双引号“”内的内容替成实际的警告类型即可. #pragma clang diagnostic push #pragma clang diagnostic ...

  6. php disk_free_space与disk_total_space实例介绍

    php disk_free_space 函数与disk_total_space 函数教程,第一个函数是指函数返回的空间,以字节为单位,在指定的目录,而disk_total_space 函数返回的总空间 ...

  7. 教你用Ossim平台检测网络的Shellcode攻击

    教你用Ossim平台检测网络的Shellcode攻击行为 教程: http://www.tudou.com/programs/view/-hxTm0q1tDY/ 以下是视频截图: 更多视频内容: 本文 ...

  8. AngularJS 教程

    AngularJS 教程 AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications). ...

  9. Address already in use的解决方法

    当客户端保持着与服务器端的连接,这时服务器端断开,再开启服务器时会出现: Address already in usr. 可以用netstat -anp | more 可以看到客户端还保持着与服务器的 ...

  10. Java串口通信详解

    http://blog.csdn.net/kabini/article/details/1601324 ———————————————————————————————————————————————— ...