剑指offer题目21-30
面试题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的更多相关文章
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- Django常用命令及参数配置(Django 1.8.6)
常用命令 #新建Django项目 django-admin startproject mysite(项目名) #新建一个APP cd mysite python manager.py startapp ...
- <<人性的弱点>>读书笔记
书名的英文名其实是<< How to win friends and influence people & how to stop worrying and start livin ...
- Hololens开发笔记之Gesture手势识别(手势检测反馈)
本文实现当使用者手出现在Hololens视野范围内时,跟踪手并给出反馈的效果. 1.在Manager上添加HandsManager脚本组件,用于追踪识别手 HandsManager.cs如下(直接使用 ...
- BlockingQueue深入分析
1.BlockingQueue定义的常用方法如下 抛出异常 特殊值 阻塞 超时 插入 add(e) offer(e) put(e) offer(e,time,unit) 移除 remove() p ...
- hive和ORACLE语法对比
- 【JavaScript】常用方法
Jquery选择器参考:http://www.w3school.com.cn/jquery/jquery_selectors.asp 获取class="a"元素点击: $(&quo ...
- 第五百八十二天 how can I 坚持
好吧,是我错了,昨天,做好自己就行了,别人怎么样是别人的事,永远保持一颗单纯向上的心. 时间过得真快,明天又周六了.. 睡觉.
- jQuery之ajax的跨域获取数据
如果获取的数据文件存放在远程服务器上(域名不同,也就是跨域获取数据),则需要使用jsonp类型.使用这种类型的话,会创建一个查询字符串参数 callback=? ,这个参数会加在请求的URL后面.服务 ...
- eclipse javascript验证报错
项目右键->properties
- EXTJS 4 动态grid
var grid=Ext.getCmp("GridPanel1"); var store = grid.getStore(); Ext.Ajax.request({ url:&qu ...