面试题11:数值的整数次方

public class Solution {
public double Power(double base, int exponent) { if(exponent == 0)
return 1.0;
if(exponent == 1)
return base;
double res = Power(base ,exponent/2); boolean isNeg = false;
if(exponent < 0)
isNeg = true;
exponent = Math.abs(exponent); res = res * res;
if(exponent % 2 == 1){
if(isNeg == false){
res = res * base;
}else if(isNeg == true){
res = res * 1/base;
}
}
return res;
}
}

面试题14:调整数组顺序使奇数位于偶数前面

public class Solution {
public void reOrderArray(int [] array) {
int len = array.length;
int index = 0;
for(int i=0;i<array.length;i++){
int tmp = array[i];
int j = i-1;
if((array[i]%2)!=0){
while(j>=0 && (array[j]%2)==0){
array[j+1] = array[j];
j--;
}
array[j+1] = tmp;
}
}
}
}

面试题15:链表中倒数第k个结点

public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k==0) return null;
ListNode p1 = head;
ListNode p2 = head;
int i = 0;
while(i<k-1){
if(p2.next != null){
p2 = p2.next;
i++;
}else{
return null;
}
}
while(p2.next!=null){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}

面试题16:反转链表

public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return null;
if(head.next == null) return head;
ListNode pre = null;
ListNode p = head;
while(p != null){
ListNode tmp = p.next;
p.next = pre;
pre = p;
p = tmp;
}
return pre;
}
}

面试题17:合并两个排序的链表

public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null && list2 == null) return null;
ListNode newHead = new ListNode(0);
ListNode p = newHead;
while(list1 != null && list2 != null){
if(list1.val <= list2.val){
p.next = list1;
p = p.next;
list1 = list1.next;
}else{
p.next = list2;
p = p.next;
list2 = list2.next;
}
}
ListNode tmp = (list1==null)?list2:list1;
while(tmp!=null){
p.next = tmp;
p = p.next;
tmp = tmp.next;
}
return newHead.next;
}
}

面试题18:树的子结构

public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root2 == null) return false;
if(root1 == null) return false;
return isSub(root1,root2) || isSub(root1.left,root2) || isSub(root1.right,root2);
}
public boolean isSub(TreeNode root1,TreeNode root2){
if(root2 == null) return true;
if(root1 == null) return false;
if(root1.val != root2.val){
return false;
}else{
return isSub(root1.left,root2.left) && isSub(root1.right,root2.right);
}
}
}

面试题19:二叉树的镜像

public class Solution {
public void Mirror(TreeNode root) {
if(root == null) return;
if(root.left == null && root.right==null) return;
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp; if(root.left != null){
Mirror(root.left);
}
if(root.right != null){
Mirror(root.right);
}
}
}

面试题20:顺时针打印矩阵

import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printMatrix(int [][] matrix) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(matrix.length == 0) return list;
int m = matrix.length;
int n = matrix[0].length;
int x=0,y=0;
while(m>0 && n>0){
if(m==1){
for(int i=0;i<n;i++){
list.add(matrix[x][y++]);
}
break;
}else if(n == 1){
for(int i=0;i<m;i++){
list.add(matrix[x++][y]);
}
break;
}
for(int i=0;i<n-1;i++){
list.add(matrix[x][y++]);
}
for(int i=0;i<m-1;i++){
list.add(matrix[x++][y]);
}
for(int i=0;i<n-1;i++){
list.add(matrix[x][y--]);
}
for(int i=0;i<m-1;i++){
list.add(matrix[x--][y]);
}
m = m - 2;
n = n - 2;
x = x + 1;
y = y + 1;
}
return list;
}
}

剑指offer题目11-20的更多相关文章

  1. 代码题 — 剑指offer题目、总结

    剑指offer题目总结:  https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...

  2. 【剑指Offer】剑指offer题目汇总

      本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...

  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题目java实现

    Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...

  7. 剑指offer题目系列一

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

  8. 剑指Offer:面试题20——顺时针打印矩阵(java实现)

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

  9. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

  10. 剑指 offer面试题20 顺时针打印矩阵

    [题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

随机推荐

  1. 如何创建 CSS

    如何插入样式表 当读到一个样式表时,浏览器会根据它来格式化 HTML 文档.插入样式表的方法有三种: 外部样式表 当样式需要应用于很多页面时,外部样式表将是理想的选择.在使用外部样式表的情况下,你可以 ...

  2. js滚动效果-(up,left)

    // JavaScript Document //图片横向滚动// 2012-1-12 zhx 改版 改为调用方法 调用参数为元素名称 //name 控件名称 //direction 滚动方向 暂时支 ...

  3. < 独立项目 - 文本挖掘 > - 2016/10/25 第一更 - <Linux相关知识准备>

    < 独立项目 -  文本挖掘 > 项目立项的相关背景介绍,TODO方向. 一.Ubuntu环境配置 主机系统:Windows 7 SP1  64位操作系统 | i5-4210 CPU | ...

  4. Unity3D使用Assetbundle打包加载(Prefab、场景)

    之前有一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity游戏开发使用Assetbundle加载场景的原理 本篇文章我们将说说assetbund ...

  5. CPS冥想 - 1 重新审视CPS

    这篇文章是在阅读Eric Lippert大神的MSDN Blog文章时同步写成的,其中主要是各种翻译,同时还混杂自己阅读文章的笔记和感想. 原博文地址 http://blogs.msdn.com/b/ ...

  6. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  7. 性能测试框架Locust初学笔记

    Locust初探 Locust是一款类似于Jmeter开源负载测试工具,所不同的是它是用python实现,并支持python脚本. locust提供web ui界面,能够方便用户实时监控脚本运行状态. ...

  8. 链接测试工具xenu link sleuth的使用

    链接测试工具xenu link sleuth的使用很简单. 可以从这里下载 http://home.snafu.de/tilman/xenulink.html 但是注意到: 如果需要登录才能进入所有的 ...

  9. linux 修改ssh端口号

    vi /etc/ssh/sshd_config 找到#Port 22一段,这里是标识默认使用22端口,修改为如下:  代码如下 复制代码 Port 22 Port 50000 然后保存退出 执行/et ...

  10. Mybatis按SQL查询字段的顺序返回查询结果

    在SpringMVC+Mybatis的开发过程中,可以通过指定resultType="hashmap"来获得查询结果,但其输出是没有顺序的.如果要按照SQL查询字段的顺序返回查询结 ...