剑指offer题目11-20
面试题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的更多相关文章
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 【剑指Offer】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 剑指offer题目系列三(链表相关题目)
本篇延续上一篇剑指offer题目系列二,介绍<剑指offer>第二版中的四个题目:O(1)时间内删除链表结点.链表中倒数第k个结点.反转链表.合并两个排序的链表.同样,这些题目并非严格按照 ...
- 再来五道剑指offer题目
再来五道剑指offer题目 6.旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- 剑指 Offer 题目汇总索引
剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格 ...
- 剑指offer题目java实现
Problem2:实现Singleton模式 题目描述:设计一个类,我们只能生成该类的一个实例 package Problem2; public class SingletonClass { /* * ...
- 剑指offer题目系列一
本篇介绍<剑指offer>第二版中的四个题目:找出数组中重复的数字.二维数组中的查找.替换字符串中的空格.计算斐波那契数列第n项. 这些题目并非严格按照书中的顺序展示的,而是按自己学习的顺 ...
- 剑指Offer:面试题20——顺时针打印矩阵(java实现)
题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指 offer面试题20 顺时针打印矩阵
[题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
随机推荐
- 调试技巧--Windows端口号是否被占用
调试技巧--Windows端口号是否被占用 一.端口概念 10.0.0.0~10.255.255.255,172.16.0.0~172.16.255.255, 192.168.0.0~192.168. ...
- windows 代理服务器的搭建,提供Android 端访问公网.
这段时间遇到一个情况,移动的网络收费.但是可以访问学校内部的网络,比如说学校官网图书馆之类了.所以我这里便想到一个方法,用学校内部一个可以访问互联网的主机充当代理服务器(我这里使用自己的电脑,非服务器 ...
- HDU 4135 Co-prime(容斥原理)
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) ...
- ruby中proc和lambda的return区别
学习ruby有一段时间了,但是我看了好几遍proc和lambda的return区别的区别讲解,始终没明白到底什么区别,今天上午又看,终于感觉是茅塞顿开有点领悟了 一下内容部分来自<<rub ...
- Web服务器禁止range请求
range: 请求一般是多线程下载的客户端程序使用 在httpd.conf中增加下面的配置,可以禁止range请求: RewriteEngine onRewriteCond %{HTTP:Range} ...
- php 向asmx发送请求 || php 发送xml请求, 以及处理返回的xml结果
var $live_url = 'https://processing.ukash.com/RPPGateway/process.asmx'; $source = array( 'SecurityTo ...
- 2016-06-08:Windows中的bat脚本
涉及循环嵌套,启用变量延时,算术运算 @echo off setlocal enabledelayedexpansion %路径以及文件名等变量设置% set x264_exe=E:\demo\c++ ...
- ajax返回值给上层函数的方法。
function load_val(callback){//定义一个回调函数 $.getJSON('test.php' , function(dat){ callback(data);//将返回结果当 ...
- python模块概况,json/pickle,time/datetime,logging
参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html http://www.cnblogs.com/alex3714/articles/51 ...
- C++学习基础五之函数参数——形参
一.理论部分 C++中函数形参主要分为两类,如图1所示, 图1 总结: 一.当函数参数为非引用形参时,传进函数体内的是实参的拷贝,(注意,对于基本类型而言,拷贝的是实参的值,对于指针而言拷贝的是实参的 ...