Java数据结构、算法面试题

6 篇文章 1 订阅

转自https://blog.csdn.net/zyx520ytt/article/details/72466255

1.题目:输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

代码:


 
 
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Iterator;
  5. public class Solution {
  6. public static String PrintMinNumber(int [] numbers) {
  7. String result = "";
  8. int length=numbers.length;
  9. if(length< 1){
  10. return result;
  11. }
  12. ArrayList<Integer> list= new ArrayList<Integer>();
  13. for( int i= 0;i<length;i++){
  14. list.add(numbers[i]);
  15. }
  16. Collections.sort(list, new Comparator<Integer>() {
  17. @Override
  18. public int compare(Integer o1, Integer o2) {
  19. String result1=o1+ ""+o2;
  20. String result2=o2+ ""+o1;
  21. return result1.compareTo(result2);
  22. }
  23. });
  24. Iterator<Integer> iterator=list.iterator();
  25. while(iterator.hasNext()){
  26. result+=(iterator.next()+ "");
  27. }
  28. return result;
  29. }
  30. }

2.题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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. return DFS(pre,in, 0,pre.length- 1, 0,in.length- 1);
  13. }
  14. private TreeNode DFS(int []pre,int []in,int prestart,int preend,int instart,int endstart){
  15. if(prestart>preend||instart>endstart){
  16. return null;
  17. }
  18. TreeNode root= new TreeNode(pre[prestart]);
  19. for( int indexstart=instart;indexstart<=endstart;indexstart++){
  20. if(pre[prestart]==in[indexstart]){
  21. root.left=DFS(pre, in, prestart+ 1, prestart+indexstart-instart, instart, indexstart- 1);
  22. root.right=DFS(pre, in, indexstart-instart+prestart+ 1, preend, indexstart+ 1, endstart);
  23. }
  24. }
  25. return root;
  26. }
  27. }


3.题目:给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

代码:


 
 
  1. /*
  2. public class TreeNode {
  3. int val = 0;
  4. TreeNode left = null;
  5. TreeNode right = null;
  6. public TreeNode(int val) {
  7. this.val = val;
  8. }
  9. }
  10. */
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.Comparator;
  15. import java.util.Iterator;
  16. public class Solution {
  17. //思路:二叉搜索树的中序遍历就是按顺序排列的,所以,直接中序查找就可以了
  18. int index= 0;
  19. TreeNode KthNode(TreeNode pRoot, int k) {
  20. if(pRoot!= null){
  21. TreeNode left=KthNode(pRoot.left, k);
  22. if(left!= null)
  23. return left;
  24. index++;
  25. if(index==k)
  26. return pRoot;
  27. TreeNode right=KthNode(pRoot.right, k);
  28. if(right!= null)
  29. return right;
  30. }
  31. return null;
  32. }
  33. }


题目描述

HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至少是1)
代码:

  
  
  1. public class Solution {
  2. public int FindGreatestSumOfSubArray(int[] array) {
  3. if(array.length== 0){
  4. return 0;
  5. }
  6. int sum=array[ 0];
  7. int Maxsum=array[ 0];
  8. for( int i= 1;i<array.length;i++){
  9. if(sum< 0){
  10. sum= 0;
  11. }
  12. sum+=array[i];
  13. Maxsum=Math.max(Maxsum, sum);
  14. }
  15. return Maxsum;
  16. }
  17. }

题目描述

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
代码:

  
  
  1. import java.util.HashSet;
  2. import java.util.Iterator;
  3. import java.util.LinkedHashSet;
  4. import java.util.Set;
  5. public class Solution {
  6. // Parameters:
  7. // numbers: an array of integers
  8. // length: the length of array numbers
  9. // duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
  10. // Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
  11. // 这里要特别注意~返回任意重复的一个,赋值duplication[0]
  12. // Return value: true if the input is valid, and there are some duplications in the array number
  13. // otherwise false
  14. // private static final int Max=(int) (1e5+10);
  15. private static int []vis;
  16. public static boolean duplicate(int numbers[], int length, int[] duplication) {
  17. if(length< 1){
  18. return false;
  19. }
  20. vis= new int [length];
  21. for( int i= 0;i<length;i++){
  22. vis[numbers[i]]++;
  23. }
  24. Set<Integer> set= new HashSet<Integer>();
  25. for( int i= 0;i<length;i++){
  26. if(vis[numbers[i]]> 1){
  27. set.add(numbers[i]);
  28. }
  29. }
  30. Iterator<Integer> iterator=set.iterator();
  31. int cnt= 0;
  32. while(iterator.hasNext()){
  33. duplication[cnt++]=iterator.next();
  34. break;
  35. }
  36. // for(int i=0;i<cnt;i++){
  37. // System.out.print(duplication[i]+" ");
  38. // }
  39. if(cnt!= 0){
  40. return true;
  41. }
  42. return false;
  43. }
  44. }

题目描述

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何。为了方便起见,你可以认为大小王是0。
代码:

  
  
  1. import java.util.HashSet;
  2. import java.util.Set;
  3. public class Solution {
  4. //思路:判断不合法的情况:1.numbers长度不为5,2.numbers中除0外,有重复的数,3.最大值减最小值>=5
  5. //剩下的就是合法的情况了
  6. public boolean isContinuous(int[] numbers) {
  7. int length=numbers.length;
  8. if(length!= 5){
  9. return false;
  10. }
  11. Set<Integer> hashSet= new HashSet<Integer>();
  12. int ans= 0; //0的个数
  13. int Max=- 1,Min= 100;
  14. for( int i= 0;i<length;i++){
  15. if(numbers[i]!= 0){
  16. hashSet.add(numbers[i]);
  17. Max=Math.max(Max, numbers[i]);
  18. Min=Math.min(Min, numbers[i]);
  19. } else{
  20. ans++;
  21. }
  22. }
  23. if(ans+hashSet.size()!=length){
  24. return false;
  25. }
  26. if(Max-Min>= 5){
  27. return false;
  28. }
  29. return true;
  30. }
  31. }

题目描述

汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
代码:

  
  
  1. public class Solution {
  2. public String LeftRotateString(String str, int n) {
  3. if(str.length()== 0){
  4. return str;
  5. }
  6. n%=(str.length());
  7. if(str.length()< 1)
  8. return null;
  9. for( int i= 0;i<n;i++)
  10. str=GetString(str);
  11. return str;
  12. }
  13. private String GetString(String str){
  14. return str.substring( 1, str.length())+str.charAt( 0);
  15. }
  16. }

题目描述

牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
代码:

  
  
  1. public class Solution {
  2. public static String ReverseSentence(String str) {
  3. String string=str.trim();
  4. String a= "";
  5. if(string.equals(a)){
  6. return str;
  7. }
  8. StringBuilder result= new StringBuilder();
  9. String []split=str.split( " ");
  10. for( int i=split.length- 1;i>= 0;i--){
  11. result.append((split[i]+ " "));
  12. }
  13. return result.toString().trim();
  14. }
  15. }

题目描述

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
代码:

  
  
  1. import java.util.ArrayList;
  2. public class Solution {
  3. public static ArrayList<Integer> maxInWindows(int [] num, int size)
  4. {
  5. ArrayList<Integer> list= new ArrayList<Integer>();
  6. int length=num.length;
  7. if(size<= 0){
  8. return list;
  9. }
  10. if(length>= 1){
  11. int Max=Integer.MIN_VALUE;
  12. for( int i= 0;i<length;i++){
  13. Max=Math.max(Max, num[i]);
  14. }
  15. if(size>length){
  16. return list;
  17. } else{
  18. for( int i= 0;i<length-size+ 1;i++){
  19. int MAX=Integer.MIN_VALUE;
  20. for( int j=i;j<size+i;j++){
  21. MAX=Math.max(MAX, num[j]);
  22. }
  23. list.add(MAX);
  24. }
  25. }
  26. }
  27. return list;
  28. }
  29. }

题目描述

每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
代码:

  
  
  1. public class Solution {
  2. private final static int Max=( int) ( 1e5+ 10);
  3. public int LastRemaining_Solution(int n, int m) {
  4. int []array= new int[Max];
  5. int i=- 1,count=n,step= 0;
  6. while(count> 0){ //模拟环
  7. i++;
  8. if(i>=n){
  9. i= 0;
  10. }
  11. if(array[i]==- 1)
  12. continue;
  13. step++;
  14. if(step==m){
  15. step= 0;
  16. count--;
  17. array[i]=- 1;
  18. }
  19. }
  20. return i;
  21. }
  22. }

题目描述

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行 以此类推
代码:

  
  
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. /*
  5. public class TreeNode {
  6. int val = 0;
  7. TreeNode left = null;
  8. TreeNode right = null;
  9. public TreeNode(int val) {
  10. this.val = val;
  11. }
  12. }
  13. */
  14. public class Solution {
  15. public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
  16. ArrayList<ArrayList<Integer>> list= new ArrayList<ArrayList<Integer>>();
  17. if(pRoot== null){
  18. return list;
  19. }
  20. int ans= 1;
  21. Queue<TreeNode> queue= new LinkedList<TreeNode>();
  22. queue.add(pRoot);
  23. while(!queue.isEmpty()){
  24. ArrayList<Integer> nodes= new ArrayList<Integer>();
  25. int size=queue.size();
  26. for( int i= 0;i<size;i++){
  27. TreeNode root=queue.poll();
  28. if(ans% 2== 0){
  29. nodes.add( 0,root.val);
  30. } else{
  31. nodes.add(root.val);
  32. }
  33. if(root.left!= null){
  34. queue.add(root.left);
  35. }
  36. if(root.right!= null){
  37. queue.add(root.right);
  38. }
  39. }
  40. list.add(nodes);
  41. ans++;
  42. }
  43. return list;
  44. }
  45. }

题目描述

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
代码:

  
  
  1. /*
  2. public class TreeNode {
  3. int val = 0;
  4. TreeNode left = null;
  5. TreeNode right = null;
  6. public TreeNode(int val) {
  7. this.val = val;
  8. }
  9. }
  10. */
  11. import java.util.ArrayList;
  12. import java.util.LinkedList;
  13. import java.util.Queue;
  14. public class Solution {
  15. ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
  16. ArrayList<ArrayList<Integer>> list= new ArrayList<ArrayList<Integer>>();
  17. if(pRoot== null){
  18. return list;
  19. }
  20. Queue<TreeNode> queue= new LinkedList<TreeNode>();
  21. queue.add(pRoot);
  22. while(!queue.isEmpty()){
  23. ArrayList<Integer> arrayList= new ArrayList<Integer>();
  24. int size=queue.size();
  25. for( int i= 0;i<size;i++){
  26. TreeNode root=queue.poll();
  27. arrayList.add(root.val);
  28. if(root.left!= null){
  29. queue.add(root.left);
  30. }
  31. if(root.right!= null){
  32. queue.add(root.right);
  33. }
  34. }
  35. list.add(arrayList);
  36. }
  37. return list;
  38. }
  39. }

题目描述

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
代码:

  
  
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. public class Solution {
  5. private ArrayList<Integer> list= new ArrayList<Integer>();
  6. public void Insert(Integer num) {
  7. list.add(num);
  8. Collections.sort(list, new Comparator<Integer>() {
  9. @Override
  10. public int compare(Integer o1, Integer o2) {
  11. return o1-o2;
  12. }
  13. });
  14. }
  15. public Double GetMedian() {
  16. int length=list.size();
  17. int MID=length>> 1;
  18. double mid= 0;
  19. if((length& 1)== 0){
  20. Integer a1=list.get(MID);
  21. Integer a2=list.get(MID- 1);
  22. mid=(Double.valueOf(a1+ "")+Double.valueOf(a2+ ""))/ 2;
  23. } else{
  24. Integer a3=list.get(MID);
  25. mid=Double.valueOf(a3+ "");
  26. }
  27. return mid;
  28. }
  29. }

题目描述

小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck! 
输出描述:
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
代码:

  
  
  1. import java.util.ArrayList;
  2. public class Solution {
  3. public static ArrayList<ArrayList<Integer>> FindContinuousSequence( int sum) {
  4. ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
  5. if (sum < 0) {
  6. return list;
  7. }
  8. for ( int i = 1; i <= sum; i++) {
  9. for ( int j = i; j <= sum; j++) {
  10. int n = j - i + 1;
  11. int ans = i*n+(n*(n- 1))/ 2;
  12. if (ans != sum) {
  13. continue;
  14. }
  15. ArrayList<Integer> arrayList = new ArrayList<>();
  16. for ( int k = i; k <= j; k++) {
  17. arrayList.add(k);
  18. }
  19. if(arrayList.size()>= 2){ //至少包括两个数
  20. list.add(arrayList);
  21. }
  22. }
  23. }
  24. return list;
  25. }
  26. }

题目描述

有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度。

给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于500,图像元素小于等于256。

测试样例:
[[1,2,3],[4,5,6],[7,8,9]],3
返回:[[7,4,1],[8,5,2],[9,6,3]]
代码:

  
  
  1. import java.util.*;
  2. public class Transform {
  3. public int[][] transformImage( int[][] mat, int n) {
  4. int [][]A= new int[n][n];
  5. int x= 0,y=n- 1;
  6. for( int i= 0;i<n;i++){
  7. for( int j= 0;j<n;j++){
  8. A[x][y]=mat[i][j];
  9. if(x==n- 1){
  10. y--;
  11. x= 0;
  12. } else{
  13. x++;
  14. }
  15. }
  16. }
  17. return A;
  18. }
  19. }

题目描述

假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串。请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次检查子串的函数。

给定两个字符串s1,s2,请返回bool值代表s2是否由s1旋转而成。字符串中字符为英文字母和空格,区分大小写,字符串长度小于等于1000。

测试样例:
"Hello world","worldhello "
返回:false
"waterbottle","erbottlewat"
返回:true
代码:

  
  
  1. import java.util.*;
  2. public class ReverseEqual {
  3. public boolean checkReverseEqual(String s1, String s2) {
  4. if(s1== null||s2== null||s1.length()!=s2.length()){
  5. return false;
  6. }
  7. return (s1+s1).contains(s2);
  8. }
  9. }

题目描述

输入一个链表,输出该链表中倒数第k个结点。

  
  
  1. /*
  2. public class ListNode {
  3. int val;
  4. ListNode next = null;
  5. ListNode(int val) {
  6. this.val = val;
  7. }
  8. }*/
  9. import java.util.LinkedHashMap;
  10. public class Solution {
  11. public ListNode FindKthToTail(ListNode head, int k) {
  12. LinkedHashMap<Integer, ListNode> map= new LinkedHashMap<Integer, ListNode>();
  13. int cnt= 0;
  14. while(head!= null){
  15. map.put(cnt++, head);
  16. head=head.next;
  17. }
  18. return map.get(cnt-k);
  19. }
  20. }

题目描述

实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。

给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true

代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class ListNode {
  4. int val;
  5. ListNode next = null;
  6. ListNode(int val) {
  7. this.val = val;
  8. }
  9. }*/
  10. public class Remove {
  11. public boolean removeNode(ListNode pNode) {
  12. if(pNode== null){
  13. return false;
  14. }
  15. if(pNode.next== null){
  16. return false;
  17. }
  18. return true;
  19. }
  20. }

题目描述

编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。


代码:

  
  
  1. import java.util.*;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.LinkedList;
  5. /*
  6. public class ListNode {
  7. int val;
  8. ListNode next = null;
  9. ListNode(int val) {
  10. this.val = val;
  11. }
  12. }*/
  13. public class Partition {
  14. public ListNode partition(ListNode pHead, int x) {
  15. if(pHead== null||pHead.next== null){
  16. return pHead;
  17. }
  18. ListNode cur=pHead;
  19. ListNode Ahead= new ListNode(- 1);
  20. ListNode Bhead= new ListNode(- 1);
  21. ListNode Atemp=Ahead;
  22. ListNode Btemp=Bhead;
  23. while(cur!= null){
  24. if(cur.val<x){
  25. Atemp.next= new ListNode(cur.val);
  26. Atemp=Atemp.next;
  27. } else{
  28. Btemp.next= new ListNode(cur.val);
  29. Btemp=Btemp.next;
  30. }
  31. cur=cur.next;
  32. }
  33. ListNode newhead=Ahead;
  34. while(newhead.next!= null&&newhead.next.val!=- 1){
  35. newhead=newhead.next;
  36. }
  37. newhead.next=Bhead.next;
  38. return Ahead.next; //取Ahead->next而不取Ahead是因为Ahead头的val是-1,不是链表中的值
  39. }
  40. }

题目描述

有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。

给定两个链表ListNode* A,ListNode* B,请返回A+B的结果(ListNode*)。

测试样例:
{1,2,3},{3,2,1}
返回:{4,4,4}
代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class ListNode {
  4. int val;
  5. ListNode next = null;
  6. ListNode(int val) {
  7. this.val = val;
  8. }
  9. }*/
  10. public class Plus {
  11. public static ListNode plusAB(ListNode a, ListNode b) {
  12. if (a == null && b == null) {
  13. return null;
  14. }
  15. ListNode Ahead = a;
  16. ListNode Bhead = b;
  17. ListNode newhead = new ListNode(- 1);
  18. ListNode newtemp = newhead;
  19. int temp = 0;
  20. while (Ahead != null || Bhead != null) {
  21. // 三种情况:1:Ahead!=null&&Bhead!=null
  22. // 2:Ahead==null&&Bhead!=null
  23. // 3:Ahead!=null&&Bhead==null
  24. if (Ahead != null && Bhead != null) {
  25. ListNode node = new ListNode((Ahead.val + Bhead.val + temp) % 10);
  26. temp = (Ahead.val + Bhead.val + temp) / 10;
  27. newtemp.next = node;
  28. newtemp = newtemp.next;
  29. Ahead = Ahead.next;
  30. Bhead = Bhead.next;
  31. } else if (Ahead == null && Bhead != null) {
  32. ListNode node = new ListNode((Bhead.val + temp) % 10);
  33. temp = (Bhead.val + temp) / 10;
  34. newtemp.next = node;
  35. newtemp = newtemp.next;
  36. Bhead = Bhead.next;
  37. } else if (Ahead != null && Bhead == null) {
  38. ListNode node = new ListNode((Ahead.val + temp) % 10);
  39. temp = (Ahead.val + temp) / 10;
  40. newtemp.next = node;
  41. newtemp = newtemp.next;
  42. Ahead = Ahead.next;
  43. }
  44. }
  45. if (temp != 0) {
  46. ListNode node = new ListNode(temp);
  47. newtemp.next = node;
  48. newtemp = newtemp.next;
  49. }
  50. return newhead.next;
  51. }
  52. }

题目描述

输入一个链表,反转链表后,输出链表的所有元素。
代码:

  
  
  1. /*
  2. public class ListNode {
  3. int val;
  4. ListNode next = null;
  5. ListNode(int val) {
  6. this.val = val;
  7. }
  8. }*/
  9. import java.util.LinkedHashMap;
  10. public class Solution {
  11. public ListNode ReverseList(ListNode head) {
  12. if(head== null){
  13. return null;
  14. }
  15. ListNode newhead= null;
  16. ListNode phead=head;
  17. ListNode prehead= null;
  18. while(phead!= null){
  19. ListNode pnext=phead.next;
  20. if(pnext== null){
  21. newhead=phead;
  22. }
  23. phead.next=prehead;
  24. prehead=phead;
  25. phead=pnext;
  26. }
  27. return newhead;
  28. }
  29. }

题目描述

请编写一个函数,检查链表是否为回文。

给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。

测试样例:
{1,2,3,2,1}
返回:true
{1,2,3,2,3}
返回:false
代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class ListNode {
  4. int val;
  5. ListNode next = null;
  6. ListNode(int val) {
  7. this.val = val;
  8. }
  9. }*/
  10. public class Palindrome {
  11. public boolean isPalindrome(ListNode pHead) {
  12. if (pHead == null) {
  13. return false;
  14. }
  15. LinkedList<Integer> linkedList = new LinkedList<Integer>();
  16. while (pHead != null) {
  17. linkedList.add(pHead.val);
  18. pHead = pHead.next;
  19. }
  20. return Check(linkedList);
  21. }
  22. // 检查是否为回文串
  23. private boolean Check(LinkedList<Integer> linkedList) {
  24. boolean result = true;
  25. int len = linkedList.size();
  26. int length =len>> 1;
  27. for ( int i = 0; i < length; i++) {
  28. if (linkedList.get(i) != linkedList.get(len - i - 1)) {
  29. result = false;
  30. break;
  31. }
  32. }
  33. return result;
  34. }
  35. }

题目描述

用两个栈来实现一个队列,完成队列的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() {
  9. while(!stack1.isEmpty()){
  10. stack2.push(stack1.pop());
  11. }
  12. int node=stack2.pop();
  13. while(!stack2.isEmpty()){
  14. stack1.push(stack2.pop());
  15. }
  16. return node;
  17. }
  18. }

题目描述

有一些数的素因子只有3、5、7,请设计一个算法,找出其中的第k个数。

给定一个数int k,请返回第k个数。保证k小于等于100。

测试样例:
3
返回:7
代码:

  
  
  1. import java.util.*;
  2. public class KthNumber {
  3. private static final int Max = ( int) ( 1e5 + 10);
  4. private static int cnt;
  5. private static int []A;
  6. public static int findKth(int k) {
  7. InitData();
  8. return A[k];
  9. }
  10. private static void InitData() {
  11. cnt= 1;
  12. int a= 0,b= 0,c= 0;
  13. A= new int[Max];
  14. A[ 0]= 1;
  15. while(cnt<= 100){
  16. int temp=Math.min(A[a]* 3, Math.min(A[b]* 5, A[c]* 7));
  17. if(temp==A[a]* 3)
  18. a++;
  19. if(temp==A[b]* 5)
  20. b++;
  21. if(temp==A[c]* 7)
  22. c++;
  23. A[cnt++]=temp;
  24. }
  25. }
  26. }


题目描述

现在我们有一个int数组,请你找出数组中每个元素的下一个比它大的元素。

给定一个int数组A及数组的大小n,请返回一个int数组,代表每个元素比他大的下一个元素,若不存在则为-1。保证数组中元素均为正整数。

测试样例:
[11,13,10,5,12,21,3],7
返回:[13,21,12,12,21,-1,-1]
代码:

  
  
  1. import java.util.*;
  2. public class NextElement {
  3. public static int[] findNext( int[] A, int n) {
  4. int []B= new int[n];
  5. for( int i= 0;i<n;i++){
  6. int temp=- 1;
  7. for( int j=i+ 1;j<n;j++){
  8. if(A[i]<A[j]){
  9. temp=A[j];
  10. break;
  11. }
  12. }
  13. B[i]=temp;
  14. }
  15. return B;
  16. }
  17. }

题目描述

现在有一个数组,请找出数组中每个元素的后面比它大的最小的元素,若不存在则为-1。

给定一个int数组A及数组的大小n,请返回每个元素所求的值组成的数组。保证A中元素为正整数,且n小于等于1000。

测试样例:
[11,13,10,5,12,21,3],7
[12,21,12,12,21,-1,-1]
代码:

  
  
  1. import java.util.*;
  2. public class NextElement {
  3. public static int[] findNext( int[] A, int n) {
  4. int []B= new int[n];
  5. for( int i= 0;i<n;i++){
  6. int temp=Integer.MAX_VALUE;
  7. boolean ok= false;
  8. for( int j=i+ 1;j<n;j++){
  9. if(A[i]<A[j]){
  10. ok= true;
  11. temp=Math.min(temp, A[j]);
  12. }
  13. }
  14. if(ok){
  15. B[i]=temp;
  16. } else{
  17. B[i]=- 1;
  18. }
  19. }
  20. return B;
  21. }
  22. }

题目描述

请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。

给定一个int[] numbers(C++中为vector<int>),其中第一个元素为栈顶,请返回排序后的栈。请注意这是一个栈,意味着排序过程中你只能访问到第一个元素。

测试样例:
[1,2,3,4,5]
返回:[5,4,3,2,1]
代码:

  
  
  1. import java.util.*;
  2. public class TwoStacks {
  3. public ArrayList<Integer> twoStacksSort(int[] numbers) {
  4. ArrayList<Integer> list= new ArrayList<>();
  5. if(numbers.length== 0){
  6. return list;
  7. }
  8. Stack<Integer> stack1= new Stack<Integer>();
  9. Stack<Integer> stack2= new Stack<Integer>();
  10. for( int i= 0;i<numbers.length;i++){
  11. stack1.push(numbers[i]);
  12. }
  13. while(!stack1.isEmpty()){
  14. int temp=stack1.pop();
  15. while(!stack2.isEmpty()&&stack2.peek()>temp){
  16. stack1.push(stack2.pop());
  17. }
  18. stack2.push(temp);
  19. }
  20. int len=stack2.size();
  21. for( int i= 0;i<len;i++){
  22. list.add(stack2.pop());
  23. }
  24. return list;
  25. }
  26. }

题目描述

实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。

给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡。

代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class TreeNode {
  4. int val = 0;
  5. TreeNode left = null;
  6. TreeNode right = null;
  7. public TreeNode(int val) {
  8. this.val = val;
  9. }
  10. }*/
  11. public class Balance {
  12. public boolean isBalance(TreeNode root) {
  13. if (root == null)
  14. return true;
  15. TreeNode left=root.left;
  16. TreeNode right=root.right;
  17. int val=Math.abs(GetHigh(left)-GetHigh(right)); //判断左数和右树的高度差
  18. if(val> 1) //如果大于1,不符合
  19. return false;
  20. //如果不大于1,继续判断左树的子树和右树的子树
  21. return isBalance(left)&&isBalance(right);
  22. }
  23. //获取一棵树的高度
  24. private int GetHigh(TreeNode root){
  25. if(root== null)
  26. return 0;
  27. int lefthigh=GetHigh(root.left);
  28. int righthigh=GetHigh(root.right);
  29. return lefthigh>righthigh?(lefthigh+ 1):(righthigh+ 1);
  30. }
  31. }

题目描述

对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径。

给定图中的两个结点的指针UndirectedGraphNode* a,UndirectedGraphNode*b(请不要在意数据类型,图是有向图),请返回一个bool,代表两点之间是否存在一条路径(a到b或b到a)。

代码:

  
  
  1. import java.util.*;
  2. import java.util.ArrayList;
  3. /*
  4. public class UndirectedGraphNode {
  5. int label = 0;
  6. UndirectedGraphNode left = null;
  7. UndirectedGraphNode right = null;
  8. ArrayList<UndirectedGraphNode> neighbors = new ArrayList<UndirectedGraphNode>();
  9. public UndirectedGraphNode(int label) {
  10. this.label = label;
  11. }
  12. }*/
  13. public class Path {
  14. public boolean checkPath(UndirectedGraphNode a, UndirectedGraphNode b) {
  15. if(a==b){
  16. return true;
  17. }
  18. HashMap<UndirectedGraphNode, Boolean> map= new HashMap<UndirectedGraphNode, Boolean>();
  19. boolean ok=Check(a,b,map); //从a开始找,b不动
  20. map.clear();
  21. return ok||Check(b,a,map); //从b开始找,a不动
  22. }
  23. private boolean Check(UndirectedGraphNode a, UndirectedGraphNode b,HashMap<UndirectedGraphNode, Boolean> map){
  24. if(a==b){
  25. return true;
  26. }
  27. map.put(a, true);
  28. for( int i= 0;i<a.neighbors.size();i++){ //从a的邻居找,看看有没有等于b的
  29. if(!map.containsKey(a.neighbors.get(i))&&Check(a.neighbors.get(i), b, map)){
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. }

题目描述

对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树。

给定一个有序序列int[] vals,请返回创建的二叉查找树的高度。

代码:

  
  
  1. import java.util.*;
  2. public class MinimalBST {
  3. public int buildMinimalBST(int[] vals) {
  4. int length=vals.length;
  5. if(length== 0){
  6. return 0;
  7. }
  8. int sum= 1;
  9. for( int i= 1;i<= 10000;i++){
  10. sum<<= 1;
  11. if(sum- 1>=length){
  12. return i;
  13. }
  14. }
  15. return 0;
  16. }
  17. }

题目描述

对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表。

给定二叉树的根结点指针TreeNode* root,以及链表上结点的深度,请返回一个链表ListNode,代表该深度上所有结点的值,请按树上从左往右的顺序链接,保证深度不超过树的高度,树上结点的值为非负整数且不超过100000。

代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class ListNode {
  4. int val;
  5. ListNode next = null;
  6. ListNode(int val) {
  7. this.val = val;
  8. }
  9. }*/
  10. /*
  11. public class TreeNode {
  12. int val = 0;
  13. TreeNode left = null;
  14. TreeNode right = null;
  15. public TreeNode(int val) {
  16. this.val = val;
  17. }
  18. }*/
  19. public class TreeLevel {
  20. public ListNode getTreeLevel(TreeNode root, int dep) {
  21. ListNode listNode= new ListNode(- 1);
  22. ListNode head=listNode;
  23. if(root== null||dep== 0){
  24. return null;
  25. }
  26. Queue<TreeNode> queue= new LinkedList<TreeNode>();
  27. queue.add(root);
  28. int ans= 1;
  29. while(!queue.isEmpty()){
  30. int size=queue.size();
  31. if(ans==dep){
  32. for( int i= 0;i<size;i++){
  33. TreeNode node=queue.poll();
  34. ListNode newhead= new ListNode(node.val);
  35. head.next=newhead;
  36. head=head.next;
  37. }
  38. break;
  39. } else{
  40. for( int i= 0;i<size;i++){
  41. TreeNode node=queue.poll();
  42. TreeNode left=node.left;
  43. TreeNode right=node.right;
  44. if(left!= null){
  45. queue.add(left);
  46. }
  47. if(right!= null){
  48. queue.add(right);
  49. }
  50. }
  51. }
  52. ans++;
  53. }
  54. return listNode.next;
  55. }
  56. }

题目描述

请实现一个函数,检查一棵二叉树是否为二叉查找树。

给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树。

代码:

  
  
  1. import java.util.*;
  2. /*
  3. public class TreeNode {
  4. int val = 0;
  5. TreeNode left = null;
  6. TreeNode right = null;
  7. public TreeNode(int val) {
  8. this.val = val;
  9. }
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.栈和队列的共同特点是(只允许在端点处插入和删除元素) 4.栈通常采用的两种存储结构是(线性存储结构和链表存储结构) 5.下列关于栈的叙述正确的是(D) A.栈是非线性结构B.栈是一种树状结构C.栈具有先进先出的特征D.栈有后进先出的特征 6.链表不具有的特点是(B)A.不必事先估计存储空间 B.可随机访问任一元素 C.插入删除不需要移动元素 D.所需空间与线性表长度成正比 7.用链表表示线性表的优点是(便于插入和删除操作) 8.在单链表中,增加头结点的目的是(方便运算的实现) 9.循环链表的主要优点是(从表中任一结点出发都能访问到整个链表) 10.线性表L=(a1,a2,a3,……ai,……an),下列说法正确的是(D) A.每个元素都有一个直接前件和直接后件 B.线性表中至少要有一个元素 C.表中诸元素的排列顺序必须是由小到大或由大到小 D.除第一个和最后一个元素外,其余每个元素都有一个且只有一个直接前件和直接后件 11.线性表若采用链式存储结构时,要求内存中可用存储单元的地址(D) A.必须是连续的 B.部分地址必须是连续的C.一定是不连续的 D.连续不连续都可以 12.线性表的顺序存储结构和线性表的链式存储结构分别是(随机存取的存储结构、顺序存取的存储结构) 13.树是结点的集合,它的根结点数目是(有且只有1) 14.在深度为5的满二叉树中,叶子结点的个数为(31) 15.具有3个结点的二叉树有(5种形态) 16.设一棵二叉树中有3个叶子结点,有8个度为1的结点,则该二叉树中总的结点数为(13) 17.已知二叉树后序遍历序列是dabec,中序遍历序列是debac,它的前序遍历序列是(cedba) 18.已知一棵二叉树前序遍历和中序遍历分别为ABDEGCFH和DBGEACHF,则该二叉树的后序遍历为(DGEBHFCA) 19.若某二叉树的前序遍历访问顺序是abdgcefh,中序遍历访问顺序是dgbaechf,则其后序遍历的结点访问顺序是(gdbehfca) 20.数据库保护分为:安全性控制、 完整性控制 、并发性控制和数据的恢复。 1. 在计算机中,算法是指(解方案的准确而完整的描述) 2.在下列选项中,哪个不是一个算法一般应该具有的基本特征(无穷性) 说明:算法的四个基本特征是:可行性、确定性、有穷性和拥有足够的情报。 3. 算法一般都可以用哪几种控制结构组合而成(顺序、选择、循环) 4.算法的时间复杂度是指(算法执行过程中所需要的基本运算次数) 5. 算法的空间复杂度是指(执行过程中所需要的存储空间) 6. 算法分析的目的是(分析算法的效率以求改进) ............ .................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值