剑指offer题目1-10
面试题3:二维数组中的查找
public class Solution {
public boolean Find(int [][] array,int target) {
boolean isFound = false;
int m = array.length;
int n = array[0].length;
if(m>0 && n>0 ){
int row = 0;
int col = n-1;
while(row<m && col>=0){
if(array[row][col] > target){
col = col - 1;
}else if(array[row][col] <target){
row = row + 1;
}else{
isFound = true;
break;
}
}
}
return isFound;
}
}
面试题4:替换空格
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer res = new StringBuffer("");
char[] strChar = str.toString().toCharArray();
int len = strChar.length;
for(int i=0;i<len;i++){
if(strChar[i] == ' '){
res.append("%20");
}else{
res.append(strChar[i]);
}
}
return res.toString();
}
}
面试题5:从尾到头打印链表
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<Integer>();
while(listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> res = new ArrayList<Integer>();
while(!stack.isEmpty()){
res.add(stack.peek());
stack.pop();
}
return res;
}
}
面试题6:重建二叉树
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length != in.length) return null;
if(pre.length == 0 || pre==null || in == null){
return null;
}
return construct(pre,0,pre.length-1,in,0,in.length-1);
}
public TreeNode construct(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn){
int rootVal = pre[startPre];
TreeNode root = new TreeNode(rootVal);
root.left = null;
root.right= null;
if(startPre == endPre && startIn == endIn){
return root;
}
int posIn = startIn;
while(posIn <= endIn && in[posIn] != rootVal) posIn++;
int leftLen = posIn - startIn ;
if(leftLen>0){
root.left = construct(pre,startPre+1 , startPre+leftLen,in,startIn,posIn-1);
}
if(leftLen<endPre-startPre){
root.right = construct(pre,startPre+leftLen+1,endPre,in,posIn+1,endIn);
}
return root;
}
}
面试题7:用两个栈实现队列
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
stack1.push(node);
}
public int pop() {
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
面试题8:旋转数组的最小数字
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length == 0) return 0;
int l = 0;
int r = array.length-1;
while(l<r){
int mid = l + (r - l)/2;
if(array[mid]>array[r] ){
l = mid + 1;
}else if(array[mid]<array[r]){
r = mid ;
}else{
r--;
}
}
return array[l];
}
}
面试题9:斐波那契数列
public class Solution {
public int Fibonacci(int n) {
int twoBeforeF = 1;
int oneBeforeF = 1;
int thisF = 0;
if (n==0) return 0;
if (n==1) return 1;
if (n==2) return 1;
if(n>2){
for(int i=3;i<=n;i++){
thisF = twoBeforeF + oneBeforeF;
twoBeforeF = oneBeforeF;
oneBeforeF = thisF;
}
}
return thisF;
}
}
面试题9:跳台阶
public class Solution {
public int JumpFloor(int target) {
if(target == 0 ) return 0;
if(target == 1 ) return 1;
if(target == 2 ) return 2;
int twoBefore = 1;
int oneBefore = 2;
int res = 0;
for(int i=3;i<=target;i++){
res = twoBefore + oneBefore;
twoBefore = oneBefore;
oneBefore = res;
}
return res;
}
}
面试题9:变态跳台阶
public class Solution {
public int JumpFloorII(int target) {
if(target == 0) return 0;
int[] arr = new int[target+1];
if(target>0){
arr[0] = 0;
for(int i=1;i<=target;i++){
int tmpSum = 1;
for(int j=0;j<i;j++){
tmpSum += arr[j];
}
arr[i] = tmpSum;
}
}
return arr[target];
}
}
面试题10:二进制中1的个数
public class Solution {
public int NumberOf1(int n) {
int count = 0;
while(n!=0){
count++;
n = n & (n-1);
}
return count;
}
}
剑指offer题目1-10的更多相关文章
- 代码题 — 剑指offer题目、总结
剑指offer题目总结: https://www.cnblogs.com/dingxiaoqiang/category/1117681.html 版权归作者所有,任何形式转载请联系作者.作者:马孔多 ...
- 剑指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】剑指offer题目汇总
本文为<剑指Offer>刷题笔记的总结篇,花了两个多月的时间,将牛客网上<剑指Offer>的66道题刷了一遍,以博客的形式整理了一遍,这66道题属于相对基础的算法题目,对于 ...
- 牛客网上的剑指offer题目
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...
- 剑指offer题目系列二
本篇延续上一篇,介绍<剑指offer>第二版中的四个题目:从尾到头打印链表.用两个栈实现队列.旋转数组的最小数字.二进制中1的个数. 5.从尾到头打印链表 题目:输入一个链表的头结点,从尾 ...
- 剑指offer(leetcode 10.) 正则表达式匹配
这题一年前就做过,当时刚开始刷leetcode,提交了几十次过不去,就放那没管了.今天剑指offer又遇到这题,终于做出来了,用的dp. class Solution { public: bool i ...
随机推荐
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- Lab_5_SysOps_Resources_Linux_v2.5
System Operations - Lab 5: Managing Resources Using Tagging (Linux) - 2.5 ========================== ...
- 【学】CSS3基础实例2 - box-shadow, border-radius 圆形图标以及内部旋转
首先要说一下,transition属性ie9是不支持的,从ie10才开始支持 例子是当鼠标移上div后,它会旋转180度. 要点: 用圆角制作圆形盒子,border-radius设置成50%: 用bo ...
- vim 学习日志(4):多窗口使用技巧
原文地址: http://blog.csdn.net/devil_2009/article/details/7006113 vim多窗口使用技巧 1.打开多个窗口打开多个窗口的命令以下几个:横向切割窗 ...
- sql语句,怎么查看一个表中的所有约束
sql语句,怎么查看一个表中的所有约束,比如,一个student表,有唯一,外键,主键,用sql语句怎么查看student表中的所有约束呢? select * from sysobjects wher ...
- mybatis配置优化
1.加入日志log4j 1)加入jar包:log4j-1.2.17.jar 2)加入log4j配置文件: 可以使properties或者xml形式 log4j.rootLogger = DEBUG,C ...
- ORACLE-SELECT学习
(一)select格式:SELECT [ ALL | DISTINCT ] <字段表达式1[,<字段表达式2[,…] FROM <表名1>,<表名2>[,…] [W ...
- 3dsMax脚本插件开发之路
经过这两个月的努力,RDF2.1的升级开发已经基本完成,只待过些天正式发布.所以现在总算有时间思考,来整理一下自己的思路,以及今后的方向. 回顾当初,1.0是纯Maxscript编写的,一机一码的方式 ...
- SQLite事务管理
事务管理对数据库一致性是至关重要的.数据库实现ACID属性以确保一致性.SQLite依赖于本地文件锁和页日志来实现ACID属性.SQLite只支持扁平事务,并不支持事务嵌套和保存点能力. 1.1 事务 ...
- 如何设置a标签的宽高,如何使a标签的文字垂直居中
通常情况下a标签是没有宽高的,设置 width 和 height 没有作用. 若要使用 width 和 height,需要把a标签转为块级元素,即:display:block|inline-block ...