剑指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, ...
随机推荐
- Array补充方法
Array.prototype.Contain = function (item) { var arr = this; if (arr == null || arr.length == 0) { re ...
- 【solr】java整合solr5.0之solrj的使用
1.首先导入solrj需要的的架包 2.需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrClient替代这个类了,在添加之后首先我们需要根据schem ...
- Eplan PPE Pro-panel Electric fluid P8 2.4图文安装教程
Eplan ppe pro-panel electric fluid P8等多个最新2.4中文版本的安装,都是使用相同的虚拟驱动MultiKey,还是只有win32位的安装包,不过支持64位操作系统的 ...
- 修改mongodb3.0副本集用户密码遇到的坑
最近公司对项目安全方面的问题很是重视,进行了多次各种安全漏洞的扫描,于是乎就扫到了mongodb弱口令的问题. 在项目部署初期,因为大家对这个都不是特别重视,大概是因为觉得反正是内网项目吧,所以mon ...
- 2.HTML5 标准改变,准备工作
1.HTML5 标准改变: Html5 不是SGML,XML语言,没有有效性检查,是规范,有松散的写法 不许写结束标签:area,base,br,col,hr,img,input,link,sourc ...
- (C#) Interview Questions.
(Note: Most are collected from Internet. 绝大部分内容来自互联网) 1. What's the difference between Hashtable and ...
- String字符串
主要来源:http://www.cnblogs.com/devinzhang/archive/2012/01/25/2329463.html http://blog.csdn.net/qh_java/ ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- BackTrack 5 开启SSHD服务
BackTrack 5 开启SSHD服务 1 service ssh start 但启动后,仍然无法从远程连接,会有提示: 1 Read from socket failed: Connection ...
- VS2013使用EF6连接MySql
前提:a.安装MySql的VS插件(版本请下载最新版) 我用的是:mysql-for-visualstudio-1.1.4 b.安装用于.net连接程序 mysql-connector-net-6. ...