剑指offer题目51-60
面试题51:数组中重复的数字
public class Solution {
public boolean duplicate(int numbers[],int length,int [] duplication) {
for(int i=0;i<length;i++){
if(numbers[i] != i){
int tmp = numbers[numbers[i]];
if(tmp == numbers[i]){
duplication[0] = numbers[i];
return true;
}
numbers[numbers[i]] = numbers[i];
numbers[i] = tmp;
}
}
return false;
}
}
面试题52:构建乘积数组
public class Solution {
public int[] multiply(int[] A) {
int len = A.length;
int[] B = new int[len];
int lToR = 1;
int rToL = 1;
B[0] = 1;
for(int i=1;i<=len-1;i++){
lToR *= A[i-1];
B[i] = lToR ;
}
for(int i=len-2;i>=0;i--){
rToL *= A[i+1];
B[i] = B[i]*rToL;
}
return B;
}
}
面试题53:正则表达式匹配
import java.util.Arrays;
public class Solution {
public boolean match(char[] str, char[] pattern)
{
int lenS = str.length;
int lenP = pattern.length;
boolean[] matchArr = new boolean[lenS + 1];
Arrays.fill(matchArr,false);
matchArr[lenS] = true;
for(int i=lenP-1;i>=0;i--){
if(pattern[i] == '*'){
for(int j = lenS-1;j>=0;j--){
matchArr[j] = matchArr[j] || (matchArr[j+1] && (pattern[i-1] == '.' || pattern[i-1] == str[j]));
}
i--;
}else{
for(int j=0;j<lenS;j++){
matchArr[j] = matchArr[j+1] && (pattern[i] == '.' || pattern[i] == str[j]);
}
matchArr[lenS] = false;
}
}
return matchArr[0];
}
}
面试题54:表示数值的字符串
public class Solution {
public boolean isNumeric(char[] str) {
String string = String.valueOf(str);
return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
}
}
面试题55:字符流中第一个不重复的字符
public class Solution {
private int[] arr =new int[256];
private int cnt = 1;
//Insert one char from stringstream
public void Insert(char ch)
{
int index = (int)ch - ((int)'0' - 48);
System.out.println(index);
if(arr[index] == 0){
arr[index] = cnt;
}else{
arr[index] = -1;
}
cnt++;
}
//return the first appearence once char in current stringstream
public char FirstAppearingOnce()
{
char ch = '#';
int minIndex = Integer.MAX_VALUE;
for(int i=0;i<arr.length;i++){
if(arr[i] != -1 && arr[i] !=0){
if(minIndex > arr[i]){
minIndex = arr[i];
ch = (char)(i + '0' - 48 );
}
}
}
return ch;
}
}
面试题56:链表中环的入口节点
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead == null || pHead.next == null) return null;
ListNode fast = pHead;
ListNode slow = pHead;
while(fast != null && fast.next != null ){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
fast = pHead;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
if(fast == slow){
return fast;
}
}
}
return null;
}
}
面试题57:删除链表中重复的节点
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead==null) return null;
ListNode FakeHead=new ListNode(0);
FakeHead.next=pHead;
ListNode pre=FakeHead;
ListNode cur=pHead;
while(cur!=null){
while(cur.next!=null&&cur.val==cur.next.val){
cur=cur.next;
}
if(pre.next==cur){
pre.next = cur;
pre=pre.next;
}
else{
pre.next=cur.next;
}
cur=cur.next;
}
return FakeHead.next;
}
}
面试题58:二叉树的下一个节点
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode == null) return null;
TreeLinkNode next = null;
if(pNode.right != null){
TreeLinkNode tmp = pNode.right;
while(tmp.left != null){
tmp = tmp.left;
}
next = tmp;
}else if(pNode.next != null){
TreeLinkNode cur = pNode;
TreeLinkNode parent = pNode.next;
while(parent != null && cur == parent.right){
cur = parent;
parent = parent.next;
}
next = parent;
}
return next;
}
}
面试题59:对称的二叉树
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot == null) return true;
boolean res = Judge(pRoot.left,pRoot.right);
return res;
}
public boolean Judge(TreeNode left,TreeNode right){
if(left == null && right == null) return true;
if(left == null || right == null) return false;
return (left.val == right.val) &&
Judge(left.left,right.right) &&
Judge(left.right,right.left);
}
}
面试题60:之字形打印二叉树
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
travel(pRoot,res,0);
return res;
}
public void travel(TreeNode cur,ArrayList<ArrayList<Integer>> res,int level){
if(cur==null) return;
if(res.size()<=level){
ArrayList<Integer> newLevel = new ArrayList<Integer>();
res.add(newLevel);
}
ArrayList<Integer> col = res.get(level);
if(level%2==0){
col.add(cur.val);
}else{
col.add(0,cur.val);
}
travel(cur.left,res,level+1);
travel(cur.right,res,level+1);
}
}
剑指offer题目51-60的更多相关文章
- 【剑指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>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 剑指offer题目解答合集(C++版)
数组中重复的数字 二维数组中查找 字符串 替换空格 二叉树的编码和解码 从尾到头打印链表 重建二叉树 二叉树的下一个节点 2个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- Linux查看系统资源使用情况(转)
概述: 用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O ...
- mysql数据库优化小结
一.常见数据库的优化操作 1.表的设计要符合三范式. 2.添加适当的索引,索引对查询速度影响很大,必须添加索引.主键索引,唯一索引,普通索引,全文索引 3.添加适当存储过程,触发器,事务等. 4.读写 ...
- 黄聪:C#操作Word表格的常见操作(转)
几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示: bool saveChange = false; //C#操作Word表格操作 object missing = System. ...
- 天嵌E8卡片电脑USBWIFI驱动linux移植
下载驱动:http://pan.baidu.com/s/1sjL0Axn The drivers can be downloaded from Ralink website, the present ...
- js动态增加html页面元素
问题: <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2 ...
- js实现缓冲运动,和匀速运动有点不相同
缓冲运动和匀速运动有点不同,看图可以知道缓冲运动速度是越来越慢的. <style> *{ padding:0; margin:10px 0; } #div1{ height:100px; ...
- 在c中保存状态
1. 注册表 注册表是一个普通的table,我们可以将c函数中需要保存的状态都存储在注册表中,注册表是可以被多个c模块共享的. 由于注册表是一个普通table,我们同样可以在栈中对其进行操作,只是这个 ...
- NDK 的开发流程
1.NDK开发所需要的工具 windows 需要在windows下的环境 把c代码打包成 手机能用的函数库 首先模拟手机的环境 1 NDK .sh linux 批处理文件 .bat windows 头 ...
- Soket编程
基本概念 lIP地址 每台联网的电脑都有一个唯一的IP地址. 长度32位,分为四段,每段8位,用十进制数字表示,每段范围 0 ~ 255 特殊IP:127.0.0.1 用户本地网卡测试 版本:V4(3 ...
- boost的线程池和内存池 智能指针
内存池为boost自带的 #include <boost/pool/pool.hpp> 或者另外一个开源的库: nedmalloc 一个高效率的库 线程池需要下载另外一个开源库 http: ...