剑指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, ...
随机推荐
- .Net操作Excel
先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用. .Net导出代码: /// <summ ...
- CSS基本知识汇总
1.CSS 简介 CSS 指层叠样式表 (Cascading Style Sheets),是一种用来表现 HTML 文档样式的语言,样式定义如何显示 HTML 元素,是能够真正做到网页表现与结构分离的 ...
- css学习笔记(8)
1.元素使用了float以后就脱离了文档流,不能通过margin:0 auto;来居中了. 2.position:relative;定位后可以通过margin:0 auto;来居中,也说明了relat ...
- Unity破解for mac
Mac 版本的破解: 1,确定Unity的版本,然后找到对应的crack包,下载.例如 Unity 4.2.0f4 crack包,我已经放到我的资源里了. 2,解压.这里我解压到了桌面上(也就是 /U ...
- 【解决】SharePoint外部列表保存的日期/时间值不正确
[问题描述]: 在SharePoint中创建一个外部列表后,通过工作流或直接通过外部列表中的新增向外部列表添加数据项.通过外部列表或数据库查看添加的数据项时发现日期类型字段的值都不正确,像是差了若干个 ...
- iframe中的js
iframe之间的js是不能相互访问的,iframe和父窗体之间的js也是不能相互访问的
- QueryRunner类
该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量. QueryRunner类提供了两个构造方法: 默认的构造方法 需要一个 ja ...
- Flex数据交互之WebService
In this article you will learn how to call webservices hosted on asp.net applications from flex. Fir ...
- javascript笔记:流程控制语句
一.条件语句 1.if语句 if 语句即条件判断语句,一共有三种格式: (1)if (条件表达式) 语句: var box = 100; if (box >50) { alert('box大于5 ...
- sublime ctags
安装 ctags: 下载ctags,复制exe到系统目录或者sublime text的目录中 sublime: 安装ctags插件 使用: 快捷键 Command Key Binding Alt Bi ...