剑指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 ...
随机推荐
- change-resource-tags.sh
#!/bin/bash ids=$(aws ec2 describe-instances --filter "Name=tag:Project,Values=ERPSystem" ...
- Mysql对用户操作加审计功能——初级版
在某些应用里,需要知道谁对表进行了操作,进行了什么操作,所为责任的追朔.在MYSQL里,可以使用触发器实现. 1:创建测试表 mysql> create table A(a int);Query ...
- Oracle Commit 方式 COMMIT WRITE batch NOWAIT;
1111 CREATE OR REPLACE PROCEDURE update_hav_tpnd IS CURSOR hav_tpnd_cur IS SELECT d.hav_tpnd, d. ...
- silverlight 鼠标事件处理
托管代码注册鼠标事件 不需要再.xaml中声明注册事件. .xaml <Ellipse Name=" Fill="Orange" Canvas.Top=" ...
- APP测试点总结
1.功能性测试: ——根据产品需求文档编写测试用例. ——软件设计文档编写用例. 注意:就是根据产品需求文档编写测试用例而进行测试.2.兼容性测试: ——android版本的兼容性 ——手机分辨率兼容 ...
- oracle查看所有表的数据量并排序
源地址:http://blog.csdn.net/zhanggnol/article/details/6683697 select t.table_name,t.num_rows from user_ ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- 在php中写接口时 对json格式的转换 简单的方法
方法 一 方法二 可以通过urlencode();遍历出来
- NHibernate系列文章七:NHibernate对象状态
摘要 NHibernate对象持久化 NHibernate对象的三个状态:临时态.持久态.游离态(托管态) NHibernate三状态的相互转化 1. NHibernate对象持久化 NHiberna ...
- Python之路【第十六篇】:Django【基础篇】
Python之路[第十六篇]:Django[基础篇] Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了O ...