剑指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个栈实现队列 斐波那契数列 旋转数字 矩阵中的路径 机器人的运动范围 剪绳子 ...
随机推荐
- 【freemaker】之自定义指令通用select模版
测试代码 @Test public void test08(){ List<Group> groups=Arrays.asList(new Group(1,"山口组") ...
- (C#) Interview Questions.
(Note: Most are collected from Internet. 绝大部分内容来自互联网) 1. What's the difference between Hashtable and ...
- StringIO学习
StringIO StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO.一个简单的例子,让你对StringIO有一 ...
- IO 输入流操作
//get.h #ifndef GET_H #define GET_H #include <iostream> std::istream& get(std::istream& ...
- BeanFactory 和ApplicationContext(Bean工厂和应用上下文)
Bean工厂(BeanFactory接口),提供了基础的依赖注入支持. 应用上下文(ApplicationContext接口),建立在Bean工厂基础之上,提供了系统架构服务. Application ...
- netbean7.2 改变maven插件的中心库地址
今天内网安装netbeans 7.2(因为所使用的JDK版本问题),但安装上去之后发现build failure,提示说仓库主机找不到,因为提示上说的是apache的仓库地址.看执行maven的输出可 ...
- nodejs-express 报错View is not a constructor
可能是express版本问题 view修改为views – app.set('views',__dirname + '/views');
- httpd的警告
1. httpd: apr_sockaddr_info_get() failed for serv05 这个是因为httpd.conf文件没有定义ServerName,所以会用hostname来代替, ...
- php rmdir使用递归函数删除非空目录
我们大家都知道,php rmdir()函数用于删除空目录,但如果要删除非空目录,我们必须将非空目录中的文件或子目录删除,本文章向大家介绍php如何使用递归函数删除非空目录,需要的朋友可以参考一下.首先 ...
- 本机搭建外网web服务器
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 首先声明一下楼主是个开发人员,按理说这些搭建服务器什么的,和楼主半毛钱的关系都没有.但是呢,楼主是个爱学习的人,懂得德智体全面 ...