1.二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

  1. public class Solution {
  2. public boolean Find(int target, int [][] array) {
  3. if(array == null||array.length==0) return false;
  4. int rowIdx = 0,colIdx = array[0].length-1;
  5. while(rowIdx<array.length&&colIdx>=0){
  6. if(array[rowIdx][colIdx] == target)
  7. return true;
  8. else if(target>array[rowIdx][colIdx])
  9. rowIdx++;
  10. else if(target<array[rowIdx][colIdx])
  11. colIdx--;
  12. }
  13. return false;
  14. }
  15. }

  

2.替换空格
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

  1. public class Solution {
  2. public String replaceSpace(StringBuffer str) {
  3. StringBuilder sb = new StringBuilder();
  4. for(int i = 0;i<str.length();i++){
  5. if(str.charAt(i)==' '){
  6. sb.append("%20");
  7. }
  8. else
  9. sb.append(str.charAt(i));
  10. }
  11. return sb.toString();
  12. }
  13. }

3.从尾到头打印链表
输入一个链表,从尾到头打印链表每个节点的值。

  1. /**
  2. * public class ListNode {
  3. * int val;
  4. * ListNode next = null;
  5. *
  6. * ListNode(int val) {
  7. * this.val = val;
  8. * }
  9. * }
  10. *
  11. */
  12. import java.util.ArrayList;
  13. import java.util.Stack;
  14. public class Solution {
  15. public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
  16. ArrayList<Integer> result = new ArrayList<>();
  17. Stack<Integer> stack = new Stack<>();
  18. if(listNode == null) return result;
  19. while(listNode != null){
  20. stack.push(listNode.val);
  21. listNode = listNode.next;
  22. }
  23. while(!stack.isEmpty())
  24. result.add(stack.pop());
  25. return result;
  26. }
  27. }

 

4.重建二叉树
使用递归
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

  1. /**
  2. * Definition for binary tree
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
  12. if (pre == null || in == null || pre.length != in.length) return null;
  13. return reConstructBinaryTreeCore(pre, 0, pre.length - 1, in, 0, in.length - 1);
  14. }
  15. public TreeNode reConstructBinaryTreeCore(int[] pre, int preStartIdx, int preEndIdx, int[] in, int inStartIdx, int inEndIdx) {
  16. TreeNode node = new TreeNode(pre[preStartIdx]);
  17. if (preStartIdx == preEndIdx)
  18. if (inStartIdx != inEndIdx || pre[preStartIdx] != in[inStartIdx])
  19. System.out.println("Invalid input.");
  20. int i = 0;
  21. while (pre[preStartIdx] != in[inStartIdx + i])
  22. i++;
  23. if (i == 0)//证明没有左子树
  24. node.left = null;
  25. else
  26. node.left = reConstructBinaryTreeCore(pre, preStartIdx + 1, preStartIdx + i, in, inStartIdx, inStartIdx + i - 1);
  27. if (inStartIdx + i == inEndIdx)//证明没有右子树
  28. node.right = null;
  29. else
  30. node.right = reConstructBinaryTreeCore(pre, preStartIdx + i + 1, preEndIdx, in, inStartIdx + i + 1, inEndIdx);
  31. return node;
  32. }
  33. }

  

5.用两个栈实现队列
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

  1. import java.util.Stack;
  2. public class Solution {
  3. Stack<Integer> stack1 = new Stack<Integer>();
  4. Stack<Integer> stack2 = new Stack<Integer>();
  5. public void push(int node) {
  6. stack1.push(node);
  7. }
  8. public int pop() throws Exception {
  9. if(stack1.isEmpty()&&stack2.isEmpty())
  10. throw new Exception("Queue is empty.");
  11. if(!stack2.isEmpty())
  12. return stack2.pop();
  13. while(!stack1.isEmpty())
  14. stack2.push(stack1.pop());
  15. return stack2.pop();
  16. }
  17. }

  

6.旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

  1. public class Solution {
  2. public int minNumberInRotateArray(int [] array) {
  3. if(array == null||array.length == 0)return 0;
  4. int idx1 = 0,idx2 = array.length-1;
  5. //如果不能进入while循环,则证明第一个元素小于最后一个元素,而且数组为非递减排序,最小值即为首位。
  6. while(array[idx1]>=array[idx2]){
  7. //第一个指针指向前半段递增序列的末尾,第二个指针指向后半段递增序列的首位。
  8. if(idx2-idx1==1)return array[idx2];
  9. //二分法查找临界点
  10. int mid = (idx1+idx2)/2;
  11. //考虑特例:{1,0,1,1,1}
  12. if(array[idx1] == array[idx2]&& array[mid] == array[idx1]){
  13. for(int i = idx1;i<=idx2;i++)
  14. if(array[i]<array[mid])
  15. return array[i];
  16. //特例:{1,1,1,1,1,1,1}
  17. return array[mid];
  18. }
  19. //更新指针,直至idx2-idx1==1;
  20. if(array[mid]>=array[idx1])
  21. idx1 = mid;
  22. else if(array[mid]<=array[idx2])
  23. idx2 = mid;
  24. }
  25. //此时数组为递增排列,第一个元素最小
  26. return array[0];
  27. }
  28. }

  

7.斐波那契数列(这个数列从第3项开始,每一项都等于前两项之和)
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
n<=39

  1. public class Solution {
  2. public int Fibonacci(int n) {
  3. if(n<1) return 0;
  4. int[] fibonacci = new int[2];
  5. fibonacci[0] = 1;
  6. fibonacci[1] = 1;
  7. n-=2;
  8. while(n>0){
  9. int temp = fibonacci[0]+fibonacci[1];
  10. fibonacci[0] = fibonacci[1];
  11. fibonacci[1] = temp;
  12. n--;
  13. }
  14. return fibonacci[1];
  15. }
  16. }

  

8.跳台阶(动态规划)
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

  1. public class Solution {
  2. public int JumpFloor(int target) {
  3. if(target < 1) return 0;
  4. int[] DP = new int[3];
  5. DP[0] = 1;
  6. DP[1] = 2;
  7. DP[2] = DP[0]+DP[1];
  8. if(target<=3)
  9. return DP[target-1];
  10. for(int i =4;i<=target;i++){
  11. DP[0] = DP[1];
  12. DP[1] = DP[2];
  13. DP[2] = DP[0]+DP[1];
  14. }
  15. return DP[2];
  16. }
  17. }

  

9.矩形覆盖
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

  1. public class Solution {
  2. public int RectCover(int target) {
  3. if(target<1) return 0;
  4. int[] DP = new int[3];
  5. DP[0] = 1;
  6. DP[1] = 2;
  7. DP[2] = DP[1]+DP[0];
  8. if(target<4)
  9. return DP[target-1];
  10. for(int i = 4;i<=target;i++){
  11. int temp = DP[1]+DP[2];
  12. DP[0] = DP[1];
  13. DP[1] = DP[2];
  14. DP[2] = temp;
  15. }
  16. return DP[2];
  17. }
  18. }
  1.  

10.二进制中1的个数
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

  1. public class Solution {
  2. public int NumberOf1(int n) {
  3. int count = 0;
  4. while(n!=0){
  5. count+=n&1;
  6. n=n>>>1;
  7. }
  8. return count;
  9. }
  10. }

剑指offer(1)的更多相关文章

  1. 剑指Offer面试题:1.实现Singleton模式

    说来惭愧,自己在毕业之前就该好好看看<剑指Offer>这本书的,但是各种原因就是没看,也因此错过了很多机会,后悔莫及.但是后悔是没用的,现在趁还有余力,把这本书好好看一遍,并通过C#通通实 ...

  2. 剑指Offer面试题:14.链表的倒数第k个节点

    PS:这是一道出境率极高的题目,记得去年参加校园招聘时我看到了3次,但是每次写的都不完善. 一.题目:链表的倒数第k个节点 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题 ...

  3. 《剑指offer》面试题12:打印1到最大的n位数

    面试题12:打印1到最大的n位数 剑指offer题目12,题目如下 输入数字n,按顺序打印出1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的三位数999 方法一 和面试题11< ...

  4. 《剑指offer》面试题11: 数值的整数次方

    面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...

  5. 剑指 Offer 题目汇总索引

    剑指 Offer 总目录:(共50道大题) 1. 赋值运算符函数(或应说复制拷贝函数问题) 2. 实现 Singleton 模式 (C#) 3.二维数组中的查找 4.替换空格              ...

  6. 面试题目——《剑指Offer》

    1.把一个字符串转换成整数——<剑指Offer>P29 2.求链表中的倒数第k个结点——<剑指Offer>P30 3.实现Singleton模式——<剑指Offer> ...

  7. 剑指offer习题集2

    1.把数组排成最小的数 class Solution { public: static bool compare(const string& s1, const string& s2) ...

  8. 剑指offer习题集1

    1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...

  9. 剑指Offer:面试题20——顺时针打印矩阵(java实现)

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

  10. 牛客网上的剑指offer题目

    题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 题目:请实现一个函数,将一 ...

随机推荐

  1. django本身提供了runserver,为什么不能用来部署(runserver与uWSGI的区别)

    runserver方法是调试django时经常用到的运行方式,它使用django自带的. WSGI Server 运行,主要在测试和开发使用,并且runserver 开启的方式也是单线程. uWSGI ...

  2. “Cannot make a static reference to the non-static method”处理方法

    报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTes ...

  3. Python基础4--一看就会的选择与循环

    1 选择 if elif else 注意后面均有: if age>18: print 'adult' elif age>6: print 'teenager' else: print 'k ...

  4. JAVA_全局配置文件(配置网址,url等等)_第二种方式

    @ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中 1.application-local.yml 配置文件 2.Service 3. ...

  5. SQL注入之Sqli-labs系列第二十八关(过滤空格、注释符、union select)和第二十八A关

    开始挑战第二十八关(Trick with SELECT & UNION) 第二十八A关(Trick with SELECT & UNION) 0x1看看源代码 (1)与27关一样,只是 ...

  6. shell统计当前文件夹下的文件个数、目录个数

    1. 统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l 2. 统计当前文件夹下目录的个数 ls -l |grep "^d"|wc -l ...

  7. pytorch基础教程1

    0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...

  8. 【leetcode】58-LengthofLastWord

    problem Length of Last Word 只有一个字符的情况: 最后一个word至字符串末尾之间有多个空格的情况: code1 class Solution { public: int ...

  9. base标签对svg的影响

    页面地址:http://127.0.0.1:8080/fullLink_node.html?project_id=2 base:<base href="http://127.0.0.1 ...

  10. python------Json与pickle数据序列化

    一.json序列化 xml在被json取代,不同平台之间的语言转换,只能处理简单的.复杂的用pickle: pickle只能在python中用,而在Java中json也可以被识别. info = { ...