面试题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的更多相关文章

  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>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...

  9. 剑指offer题目系列一

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

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

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

随机推荐

  1. jquery获得图片的真实大小

    $(function(){ var imgSrc = $("#image").attr("src"); getImageWidth(imgSrc,functio ...

  2. Fatal error: Call to undefined function imagettftext()解决办法

    Fatal error: Call to undefined function imagettftext()解决办法   我的问题是php编译安装时指定了gd的目录,其实不用指定.就可以了 博客分类: ...

  3. Swagger使用总结

    Swagger使用总结 1. Swagger是什么? 官方说法:Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作 ...

  4. python中使用xlrd、xlwt操作excel表格详解

    python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...

  5. Jmeter安装

    安装下载方法:http://www.jb51.net/softjc/266834.html 下载地址:http://jmeter.apache.org/download_jmeter.cgi Wind ...

  6. 图像滤波:Gabor滤波

  7. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. 68. Longest Common Prefix

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. metasploit升级(BT5)

    1.apt-get update 2.apt-get install metasploit 3.修改文件:/opt/metasploit/ruby/lib/ruby/1.9.1/i686-linux/ ...

  10. oracle查看所有表的数据量并排序

    源地址:http://blog.csdn.net/zhanggnol/article/details/6683697 select t.table_name,t.num_rows from user_ ...