79. if(input.charAt(i)==' ') {
80. outputBuffer.append("%");
81. outputBuffer.append("2");
82. outputBuffer.append("0");
83. } else {
84. outputBuffer.append(String.valueOf(input.charAt(i)));
85. }
86. }
87. return new String(outputBuffer);
88. }
89.}
90.题 面试题 5 :
从尾到头打印链表
91.题目:
输入一个链表的头结点,从尾到头反过来打印出每个结点的值。
92.方法一:
非递归的方式
93.public class PrintListReverse {
94. public static void main (String args[]) {
95. ListNode node1=new ListNode();
96. ListNode node2=new ListNode();
97. ListNode node3=new ListNode();
98. node1.data=1;
99. node2.data=2;
100. node3.data=3;
101. node1.next=node2;
102. node2.next=node3;
103. printListReversversingly test=new printListReversversingly();
104. test.printListReverse(node1);
105. }
106. public static void printListReverse(ListNode headNode) {
107. Stack stack=new Stack();
108. while(headNode!
=null) {
109. stack.push(headNode);
110. headNode=headNode.next;
111. }
112. while(!
stack.isEmpty()) {
113. System.out.println(stack.pop().data);
114. }
115. }
116.}
117.方法二:
:
118.递归方式实现
119.public class PrintListReverse {
120. public static void main (String args[]) {
121. ListNode node1=new ListNode();
122. ListNode node2=new ListNode();
123. ListNode node3=new ListNode();
124. node1.data=1;
125. node2.data=2;
126. node3.data=3;
127. node1.next=node2;
128. node2.next=node3;
129. printListReversversingly test=new printListReversversingly();
130. test.printListReverse(node1);
131. }
132. public static void printListReverse(ListNode headNode) {
133. if(headNode!
=null) {
134. if(headNode.next!
=null) {
135. printListReverse(headNode.next);
136. }
137. }
138. System.out.println(headNode.data);
139. }
140.}
141.题 面试题 6 :
重建二叉树
142.题目描述:
输入二叉树的前序遍历和中序遍历的结果,重建出该二叉树。
假设前
143.序遍历和中序遍历结果中都不包含重复的数字,例如输入的前序遍历序列
144.{1,2,4,7,3,5,6,8}和中序遍历序列 {4,7,2,1,5,3,8,6}重建出如图所示的二叉
145.树。
146.public class BinaryTreeNode {
147. public static int value;
148. public BinaryTreeNode leftNode;
149. public BinaryTreeNode rightNode;
150.}
151.import java.util.Arrays;
152.public class Problem6 {
153. public static void main(String[] args) throws Exception {
154. int[] preSort={1,2,4,7,3,5,6,8};
155. int[] inSort={4,7,2,1,5,3,8,6};
156. BinaryTreeNode root=constructCore(preSort,inSort);
157. }
158. public static BinaryTreeNode constructCore(int[]
159. preorder,int[] inorder) throws Exception {
160. if(preorder==null||inorder==null) {
161. return null;
162. }
163. if(preorder.length!
=inorder.length) {
164. throw new Exception("长度不一样,非法的输入");
165. }
166. BinaryTreeNode root=new BinaryTreeNode();
167. for(int i=0; i168. if(inorder[i]==preorder[0]) {
169. root.value=inorder[i];
170. System.out.println(root.value);
171. root.leftNode=constructCore(Arrays.copyOfRange(preorder,1,
172. i+1), Arrays.copyOfRange(inorder, 0, i));
173. root.rightNode=constructCore(Arrays.copyOfRange(preorder,i+
174. 1, preorder.length),Arrays.copyOfRange(inorder, i+1,
175. inorder.length));
176. }
177. }
178. return root;
179. }
180.}
181.题 面试题 7 :
用两个栈实现队列
182.题目描述:
用两个栈实现一个队列,实现对了的两个函数 appendTail 和
183.deleteHead,分别完成在队列尾插入结点和在队列头部删除结点的功能。
184.public class Problem7 {
185. private Stack stack1=new Stack();
186. private Stack stack2=new Stack();
187. public void appendTail(T t) {
188. stack1.push(t);
189. }
190. public T deleteHead() throws Exception {
191. if(stack2.isEmpty()) {
192. while(!
stack1.isEmpty()) {
193. stack2.push(stack1.pop());
194. }
195. }
196. if(stack2.isEmpty()) {
197. throw new Exception("队列为空,不能删除");
198. }
199. return stack2.pop();
200. }
201. public static void main(String args[]) throws Exception {
202. Problem7 p7=new Problem7<>();
203. p7.appendTail("1");
204. p7.appendTail("2");
205. p7.appendTail("3");
206. p7.deleteHead();
207. }
208.}
209.题 面试题 8 :
旋转数组的最小数字
210.题目描述:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的
211.旋转。
输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。
例如数
212.组 {3,4,5,1,2}为 {1,2,3,4,5}的一个旋转,该数组的最小值为 1.
213.public class Problem8 {
214. public static void main(String[] args) {
215. Problem8 p8=new Problem8();
216.//int[] array={1,1,1,2,0};
217.// int[] array={3,4,5,1,2};
218. int[] array= {1,0,1,1,1};
219. System.out.println(p8.findMinNum(array));
220. }
221. public Integer findMinNum(int[] array) {
222. if(array==null) {
223. return null;
224. }
225. int leftIndex=0;
226. int rightIndex=array.length-1;
227. int mid=0;
228. while(array[leftIndex]>=array[rightIndex]) {
229. if(rightIndex-leftIndex<=1) {
230. mid=rightIndex;
231. break;
232. }
233. mid=(leftIndex+rightIndex)/2;
234. if(array[leftIndex]==array[rightIndex]&&array[leftIndex]==a
235. rray[mid]) {
236. if(array[leftIndex+1]!
=array[rightIndex-1]) {
237. mid=array[leftIndex+1](leftIndex+1):
(r
238. ightIndex-1);
239. break;
240. } else {
241. leftIndex++;
242. rightIndex--;
243. }
244. } else {
245. if(array[mid]>=array[leftIndex])
246. leftIndex=mid;
247. else {
248. if(array[mid]<=array[rightIndex])
249. rightIndex=mid;
250. }
251. }
252. }
253. return array[mid];
254. }
255.}
256.题 面试题 9 :
斐波那契数列
257.题目一:
写一个函数,输入 n,求斐波那契数列的第 n 项。
258.public class Fibonacci {
259. public long fibonacci(int n) {
260. long result=0;
261. long preOne=0;
262. long preTwo=1;
263. if(n==0) {
264. return preOne;
265. }
266. if(n==1) {
267. return preTwo;
268.