文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java经典算法题有哪些

2023-06-17 06:25

关注

这篇文章主要介绍“Java经典算法题有哪些”,在日常操作中,相信很多人在Java经典算法题有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java经典算法题有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

题目一:

Java经典算法题有哪些

public class testClockwiseOutput {     //顺时针打印一个矩阵         @Test     public void test(){         int[][] num = new int[100][100];         int n = 4;         int count =1;                 for(int i=0;i<n;i++){             for(int j =0;j<n;j++){                 num[i][j]=count++;             }         }                 output(num,0,n-1);     }         public void output(int[][] num,int start,int end){         if(start>=end || end<=0)return;         for(int i=start;i<=end;i++){             System.out.println(num[start][i]);         }         for(int i=start+1;i<=end;i++){             System.out.println(num[i][end]);         }         for(int i=end-1;i>=start;i--){             System.out.println(num[end][i]);         }         for(int i=end-1;i>start;i--){             System.out.println(num[i][start]);         }         output(num,start+1,end-1);     } }

题目二:

给出一个排序好的数组和一个数,求数组中连续元素的和等于所给数的子数组

//给出一个排序好的数组和一个数,求数组中连续元素的和等于所给数的子数组         @Test     public void test(){         int[] num = {1,2,2,3,4,5,6,7,8,9};         int sum = 7;         findSum(num,sum);     }         public void findSum(int[] num,int sum){         int left=0;         int right=0;                 for(int i=0;i<num.length;i++){             int curSum = 0;             left = i;             right = i;             while(curSum<sum){                 curSum += num[right++];             }                        if(curSum==sum){                 for(int j=left;j<right;j++){                     System.out.print(num[j]+" ");                 }                 System.out.println();             }         }     }

题目三:

Java经典算法题有哪些

//字符数组组成的所有字符串         @Test     public void test(){         //char[] cs = {'a','b','c','d','e'};                char[] cs = {'a','b','c'};            int length = cs.length;                recursionSwap(cs,0,length);     }         public void swap(char[] cs,int index1,int index2){         char temp = cs[index1];         cs[index1]=cs[index2];         cs[index2]=temp;            }         public void recursionSwap(char[] cs,int start,int length){         if(start>=length-1){             print(cs);             return;         }         for(int i=start;i<length;i++){             swap(cs,start,i);             recursionSwap(cs,start+1,length);                swap(cs,start,i);         }     }         public void print(char[] cs){         for(int i=0;i<cs.length;i++){             System.out.print(cs[i]);         }         System.out.println();     }

题目四:

Java经典算法题有哪些

//数组组成的最小数         @Test     public void test(){         int[] num={1,5,9,13,442,44,6,21,211};         qsort(num,0,num.length-1);         System.out.println(Arrays.toString(num));     }         public void qsort(int[] num,int left,int right){         if(left<right){             int partition = partition(num,left,right);             qsort(num,left,partition-1);             qsort(num,partition+1,right);         }        }         public int partition(int[] num,int left,int right){         int partition = num[left];         while(left<right){             while((num[right]==partition || isMBigerThanN(num,num[right],partition)) && left<right){                 right--;             }                        swap(num,left,right);                while((num[left]==partition || isMBigerThanN(num,partition,num[left])) && left<right){                 left++;             }             swap(num,left,right);                }                 return left;     }         public void swap(int[] num,int m,int n){         int temp = num[m];         num[m]=num[n];         num[n]=temp;     }         public boolean isMBigerThanN(int[] num,int m,int n){         String num1 = String.valueOf(m);         String num2 = String.valueOf(n);                 int temp1 = Integer.parseInt(num1+num2);         int temp2 = Integer.parseInt(num2+num1);                 if(temp1>temp2){             return true;         }         else{             return false;         }     }

题目五:

Java经典算法题有哪些

//子数组***和     @Test     public void test(){         int[] num = {1,-2,3,10,-4,7,2,-5};         //int[] num = {1,-2,3,10,-4,10,2,-5};         System.out.println(maxSum(num));     }         public int maxSum(int[] num){         int curSum = 0;         int curMaxSum = -99999999;         int start = 0;         int end = 0;                 for(int i=0;i<num.length;i++){             if(curSum<=0){                 curSum = num[i];                 start = i;             }             else{                 curSum += num[i];             }             if(curSum>curMaxSum){                 curMaxSum = curSum;                        end = i;             }         }            for(int i = start;i<=end;i++){             System.out.println(num[i]);         }         return curMaxSum;     }

题目六:

Java经典算法题有哪些

public class testMinStack {     //自定义栈,min函数得到当前最小值         @Test     public void test(){         MinStack ms = new MinStack();         ms.push(5);         System.out.println(ms.min());         ms.push(6);         ms.push(2);         ms.push(1);         System.out.println(ms.min());         ms.pop();         System.out.println(ms.min());         ms.pop();         System.out.println(ms.min());             } }  class MinStack{     private Stack<Integer> minStack = new Stack<Integer>();        private Stack<Integer> stack = new Stack<Integer>();         public int pop(){         minStack.pop();                return stack.pop();     }         public void push(int num){         if(minStack.size()<=0){             minStack.push(num);             return;         }         Integer min = minStack.lastElement();         if(num<min){             minStack.push(num);         }         else{             minStack.push(min);         }         stack.push(num);     }         public int min(){         if(minStack.size()<=0){             return -1;         }         return minStack.lastElement();     } }

题目七:

Java经典算法题有哪些

//找出数组中出现次数大于一半的数         @Test     public void test(){         int[] num = {1,2,2,2,2,2,2,4,2,4,6,4,2,6,8,2,7,7};         System.out.println(moreThanHaft(num));     }         public int moreThanHaft(int[] num){                int result = -1;         int times = 0;         for(int i=0;i<num.length;i++){             if(times==0){                 result = num[i];                 times++;             }             else{                 if(num[i]==result){                     times++;                 }                 else{                     times--;                 }             }         }                 return result;     }

题目八:

Java经典算法题有哪些

//判断一个数组是否是另一个栈的出栈顺序         @Test     public void test(){         int[] num = {1,2,3,4,5};         //int[] num1={1,2,3,5,4};         int[] num2={2,1,5,3,4};         Stack<Integer> s1 = new Stack<Integer>();         Stack<Integer> s2 = new Stack<Integer>();         for(int i=0;i<5;i++){             s2.push(num2[i]);         }                 System.out.println(testOrder(num,s1,s2));     }         public boolean testOrder(int[] num,Stack<Integer> s1,Stack<Integer> s2){         int length = num.length;                for(int i=0;i<length;i++){             s1.push(num[i]);             int s2Num = s2.lastElement();             if(s2Num==s1.lastElement().intValue()){                 s1.pop();                 s2.pop();             }         }         while(!s1.isEmpty()){             if(!s1.pop().equals(s2.pop())){                 return false;             }         }         return true;     }  复制代码

题目九:

Java经典算法题有哪些

//从扑克牌抽5张牌,0可以为任意数,判断是否是顺子         @Test     public void test(){         int[] num = {0,1,5,3,2};         System.out.println(check(num));     }         public boolean check(int[] num){         //0-13         int[] pai = new int[14];         for(int n : num){             pai[n]+=1;         }         qsort(num,0,num.length-1);         int count = pai[0];         int start = 0;         if(num[0]==0){             start=num[1];         }         else{             start=num[0];         }         for(int i = start;i<=start+5;i++){             if(pai[i]>1)return false;             count += pai[i];         }         if(count == 5)return true;         else return false;             }         public void qsort(int[] num,int left,int right){         if(left<right){             int partition = partition(num,left,right);             qsort(num,left,partition-1);             qsort(num,partition+1,right);         }     }         public int partition(int[] num,int left,int right){         int partition = num[left];         while(left<right){             while(left<right && num[right]>=partition){                 right--;             }             swap(num,left,right);             while(left<right && num[left]<=partition){                 left++;             }             swap(num,left,right);         }                 return left;            }         public void swap(int[] num,int m,int n){         int temp = num[m];         num[m]=num[n];         num[n]=temp;     }

题目十:

Java经典算法题有哪些

//输出第k个丑数(因子只有2,3,5)         @Test     public void test(){         findUglyNum(8);     }         public void findUglyNum(int index){         int[] num = new int[index];         int next = 1;         num[0]=1;         int index2=0;         int index3=0;         int index5=0;             while(next<index){             int num2 = num[index2]*2;             int num3 = num[index3]*3;             int num5 = num[index5]*5;                         num[next] = getSuitable(num2,num3,num5);                         while(num[index2]*2<=num[next]){                 index2++;             }             while(num[index3]*3<=num[next]){                 index3++;             }             while(num[index5]*5<=num[next]){                 index5++;             }                            next++;                     }         System.out.println(num[index-1]);     }         public int getSuitable(int num2,int num3,int num5){         int s = num2;         if(num3<s){             s = num3;         }         if(num5<s){             s = num5;         }         return s;     }

到此,关于“Java经典算法题有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯