剑指offer 第二章
来源:互联网 发布:公安机关优化发展环境 编辑:程序博客网 时间:2024/06/01 23:17
1 数据结构
1.1 数组
面试题3:在一个二维数组中,每一行都按照从左往右递增的顺序排列,每一列都按照从上到下递增的顺序排列。请完成一个函数,输入这样的一个二维数组和整数,判断数组中是否存在该整数。
public class FindElement { public static void main(String[] args) { int [][] arr ={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}}; System.out.println(search(arr,7)); } private static boolean search(int[][] arr, int value) { int a = arr[0].length; int b = arr.length; int i = 0;//行数 int j = a-1;//列数 while (i<=b-1&&j>=0){ if(arr[i][j] == value){ return true; } if(arr[i][j]>value){ j--; }else{ i++; } } return false; }}
1.2 字符串
面试题4:替换空格
请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we%20are%20happy.”
import java.util.Scanner;public class ReplaceBlank { public static String replace1(String str){ String str1 = new String(str); return str1.replace(" ", "%20"); } public static String replace2(String str){ String str1 = new String(str); char[] charArr = str1.toCharArray(); int originalLength = charArr.length; int count = 0; for (int i=0;i<charArr.length;i++){ char item = charArr[i]; if (item == ' '){ count++; } } int newLength = originalLength + count*2; char[] newcharArr = new char[newLength]; int indexOfOriginal = originalLength-1; int indexOfNew = newLength-1; while (indexOfOriginal >= 0){ if (charArr[indexOfOriginal] == ' '){ newcharArr[indexOfNew--] = '0'; newcharArr[indexOfNew--] = '2'; newcharArr[indexOfNew--] = '%'; indexOfOriginal--; } else{ newcharArr[indexOfNew--] = charArr[indexOfOriginal--]; } } return String.valueOf(newcharArr); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s0 = sc.nextLine(); System.out.println(replace1(s0)); System.out.println(replace2(s0)); }}
1.3 链表
链表的空间效率比数组高,但时间效率
面试题6:输入一个链表的头节点,从尾到头反过来打印每个节点的值。
import java.util.Stack;public class No5PrintListNodeReverse { public static void main(String[] args) { ListNode head = new ListNode(0); ListNode node_1 = new ListNode(1); ListNode node_2 = new ListNode(2); ListNode node_3 = new ListNode(3); ListNode node_4 = new ListNode(4); head.setNext(node_1); node_1.setNext(node_2); node_2.setNext(node_3); node_3.setNext(node_4); node_4.setNext(null); System.out.println("第一种方式:递归实现"); printListReverse_1(head); System.out.println(); System.out.println("第二种方式:栈结构实现"); printListReverse_2(head); } //用递归实现 private static void printListReverse_1(ListNode head) { if (head != null){ if(head.getNext() != null){ printListReverse_1(head.getNext()); } System.out.print(head.getValue() + " "); } } //用栈实现 private static void printListReverse_2(ListNode head){ Stack<Integer> s = new Stack<Integer>(); ListNode p = head; //进栈 while (p != null){ //每经过一个结点,把节点放到一个栈中 s.push(p.getValue()); p = p.getNext(); } //出栈 while (!s.isEmpty()){ //遍历完整个链表以后,从栈顶开始逐个输出结点的值 System.out.print(s.pop()+" "); } System.out.println(); }}class ListNode{ private int value; private ListNode next; public ListNode(int value){ this.value = value; } public ListNode(int value, ListNode next){ this.value = value; this.next = next; } public void setValue(int value){ this.value = value; } public void setNext(ListNode next){ this.next = next; } public int getValue(){ return this.value; } public ListNode getNext(){ return this.next; }}
0 0
- 剑指offer 第二章
- 《剑指Offer》第二章
- 阅读《剑指offer》第二章
- 剑指offer 第二章 基础知识
- 剑指offer-第二章-基础篇
- 剑指offer-python代码-第二章
- 剑指offer第二天
- 剑指offer-第二天
- 剑指offer第二题
- 【剑指offer】第二题
- 《剑指offer》学习心得第二日
- 剑指Offer读书笔记之第二章2字符替换
- 剑指Offer第二章面试题(Java版)
- 剑指offer—第二章算法之快速排序
- 剑指offer第二题替换空格
- 剑指Offer读书笔记之第二章1二维有序数组中的查找
- 剑指Offer读书笔记之第二章3从后往前打印链表
- 剑指offer第二题【替换空格】c++实现
- opencv学习_13 (trajkovic 角点检测)
- 重写和重载的区别(转)
- hadoop+hive+hbase+zookeeper常见Bug
- PAT 1038 统计同成绩学生
- Java学习内容
- 剑指offer 第二章
- Java 垃圾回收 (未完成)
- ubuntu 14.04.5 第一次使用PostgreSQL如何创建、删除数据库
- MyBatis学习笔记(1)
- Linux环境下一些有用但不常见的命令(一)
- spring 的配置文件applicationContext.xml和beans.xml
- 《牛客网剑指offer27题》输入一个字符串,按字典序打印出该字符串中字符的所有排列
- 博览网C++课程 第五周
- Android中不得不谈的setContentView