首页 文章 精选 留言 我的

精选列表

搜索[国密算法],共10000篇文章
优秀的个人博客,低调大师

leetcode算法题解(Java版)-11-贪心大法

一、全排列变式(递归) 题目描述 Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1]. 思路 多了一个条件,就是有重复数字出现。那可以考虑先排序,然后递归选择在相应位置放置数字的时候,可以添加判断是否被用过,也就是和前面那个比较一下:如果前面那个数和它一样值,而且目前的used 显示false那说明这个数已经在这个位置被用过了,而且已经计入了结果res中。 代码 import java.util.ArrayList; import java.util.Arrays; public class Solution { public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) { ArrayList<ArrayList<Integer>> res=new ArrayList<>(); int len=num.length; if(num==null||len==0){ return res; } boolean [] used=new boolean[len]; ArrayList<Integer> list=new ArrayList<>(); Arrays.sort(num); dfs(list,num,used,res); return res; } public void dfs(ArrayList<Integer> list,int [] num,boolean [] used,ArrayList<ArrayList<Integer>> res){ if(list.size()==num.length){ res.add(new ArrayList<Integer>(list)); return ; } for(int i=0;i<num.length;i++){ if(i>0&&num[i]==num[i-1]&&!used[i-1]){//如果它前一个和它一样的数现在没有被用过,那就跳过, //说明之前那个数已经形成过结果list之一了,目前这个分支处于回溯阶段。。。有点绕,希望能明白 continue; } if(!used[i]){ used[i]=true; list.add(num[i]); dfs(list,num,used,res); list.remove(list.size()-1); used[i]=false; } } } } 二、贪心 题目描述 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A =[2,3,1,1,4], returntrue. A =[3,2,1,0,4], returnfalse 思路 贪心的题,每走一步,跟新一下从当前这个位置所能到达的最大距离。这就是这题的贪心,贪心一般证明很难,但是可以逻辑简单想一下,肯定是能走的距离越大越好,越有可能到达终点,再说,如果最大距离都不能到终点,那其他情况更加不可能了。 代码 public class Solution { public boolean canJump(int[] A) { int maxlen=A[0]; for(int i=1;i<A.length;i++){ if(maxlen==0){ return false; } maxlen-=1; maxlen=Math.max(maxlen,A[i]); //if(maxlen+i==A.length-1){//剪枝 // return true; // } } return true; } } 三、动态规划or贪心 题目描述 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. For example:Given array A =[2,3,1,1,4] The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index 思路 dp解法: 题目想考察的是贪心,想了一下,思绪有点乱。先看一下动态规划的解法。 首先,满足dp的条件:1.子问题的最优解也是全局的最优解,有最优子结构;2.当前状态只依赖于它上一个状态,与怎么到达它上一个状态的路径无关。 代码 public class Solution { public int jump(int[] A) { int [] dp=new int[A.length];//存放起点到各点的最小步数 for(int i=0;i<A.length;i++){ int maxpos=Math.min(i+A[i],A.length-1); for(int j=i+1;j<=maxpos;j++){ if(dp[j]==0){ dp[j]=dp[i]+1; } } if(dp[A.length-1]!=0){ break; } } return dp[A.length-1]; } } 思路二 贪心解法: 贪心大法好,但真的难想明白。。首先,要设置三个值:当前位置(是一个区域:从i~furthest_pre中,区域中的点中能到达的最大位置)所能到达的最大位置(furthest_cur),当前位置的上一个位置(也是区域)所能到达的最大位置(furthest_pre),还有就是所走的步数。 有一点可能有点会懵,furthest_cur是还没有step++的时候,具体结合代码,也就是是一个预判能走到的但还没走的状态。 感觉讲的还是有点乱,现在抛开代码,想象我们站在题目给的数组的开头位置:从开始位置到开始位置能走到的最大距离(furthest_pre)之间构成了一块区域,然后我们开始一格一格走,每走一下刷新一下当前这块区域能到的最大位置(furthest_cur),如果走到从开始位置走到了furthest_pre那我们也刷新出了最大的furthest_cur,如果furthest_cur比终点大,那恭喜!再跳一不就到终点了!可以开始跳一步咯!然后重复上述的动作,直到到达终点。 如果能一步到最大位置furthest_pre,那肯定能到一步到它前面那块区域的某一位置,实行下一步跳,跳到furthest_cur。 给一个测试用例,可以在纸上画画: 4 1 6 9 7 4 5 0 6 8 1 2 3 5 8 0 2 1 2 代码 public class Solution { public int jump(int[] A) { int len=A.length; if(len==0||A==null){ return 0; } int furthest_cur=0; int furthest_pre=0; int steps=0; for(int i=0;i<len;i++){ if(furthest_pre>=len){ return steps; } if(i>furthest_pre){ furthest_pre=furthest_cur; steps++; } furthest_cur=Math.max(furthest_cur,A[i]+i); } return steps; } }

优秀的个人博客,低调大师

leetcode算法题解(Java版)-7-循环链表

一、循环链表 题目描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 思路 不能用多余空间,刚开始没有考虑多个指针什么,一下子想到个歪点子:循环就是重复走,那我可以标记一下每次走过的路,如果遇到标记过的路,那说明就是有回路了。 代码一 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode p=new ListNode(0); p=head; int u=-987123; while(p.val!=u&&p.next!=null){ p.val=u; p=p.next; } if(p.val==u){ return true; } else{ return false; } } } 思路二 当然标准的是应该用两个指针来“追赶”,前提是这两个指针走的速度不一样,一前一后如果相遇了则说明有回路。 代码二 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode p=head; ListNode q=head.next; while(p!=q&&q!=null&&p!=null){ q=q.next; if(q!=null){ q=q.next; } p=p.next; } if(p==q&&p!=null){ return true; } else{ return false; } } } 优化过的代码: /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head==null){ return false; } ListNode fastNode=head; ListNode lowNode=head; while(fastNode!=null&&fastNode.next!=null){ fastNode=fastNode.next.next; lowNode=lowNode.next; if(fastNode==lowNode){ return true; } } return false; } } 今天有场考试,到七点半才结束,就刷这么多了。

优秀的个人博客,低调大师

算法学习之路|POJ2689(素数筛)

题目大意:选出区间L,R之间相邻素数中差值最大和最小的素数对 素数筛(线性筛): #include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define MAX 10000000 long long su[MAX],cnt; bool isprime[MAX]; void prime() { cnt=1; memset(isprime,1,sizeof(isprime));//初始化 isprime[0]=isprime[1]=0;//0和1不是素数 for(long long i=2;i<=MAX;i++) { if(isprime[i]) su[cnt++]=i;//保存素数 for(long long j=1;j<cnt&&su[j]*i<MAX;j++) { isprime[su[j]*i]=0;//筛掉小于等于i的素数和i的积构成的合数 } } } int main() { prime(); for(long long i=1;i<cnt;i++) printf("%d ",su[i]); return 0; } 思路:直接用素数筛会超时(int范围线性复杂度时间复杂度已经达到10e9),而区间间隔比较小,只有1e6,而且对于int范围内的合数来说,最小质因子必定小于2^16。所以可以进行二次筛素数,第一次对50000以内筛素数,第二次筛出L,R区间内素数即可。 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; #define INF 1e9 #define maxn 50005 #define maxm 100005 int l,u; int su[maxn],isprime[maxn],f[maxm]; int cnt=0; void prime() { memset(isprime,1,sizeof(isprime)); isprime[0]=isprime[1]=0; for(int i=2;i<=maxn;i++) { if(isprime[i]) su[cnt++]=i; for(int j=1;j<cnt&&su[j]*i<maxn;j++) { isprime[su[j]*i]=0; } } } int main() { prime(); while(cin>>l>>u) { if(l==1)l=2;//注意l=1 memset(f,0,sizeof(f)); for(int i=0;i<cnt;i++) { int a=(l-1)/su[i]+1; int b=u/su[i]; for(int j=a;j<=b;j++) if(j>1) f[j*su[i]-l]=1; } int p=-1,max_ans=-1,min_ans=INF,x1,y1,x2,y2; for(int i=0;i<=u-l;i++)//暴力枚举 { if(f[i]==0) { if(p==-1) { p=i; continue; } if(max_ans<i-p) { max_ans=i-p; x1=p+l;y1=i+l; } if(min_ans>i-p) { min_ans=i-p; x2=p+l;y2=i+l; } p=i; } } if(max_ans==-1)cout<<"There are no adjacent primes."<<endl; else cout<<x2<<","<<y2<<" are closest, "<<x1<<","<<y1<<" are most distant."<<endl; } return 0; }

优秀的个人博客,低调大师

算法学习之路|hdu 1035 Robot Motion(模拟)

题目大意 给一个地图,由ESWN(东南西北)组成,机器人根据脚下的指令移动,求如果机器人能走出地图,走的步数多少,如果不能走出,求每绕一圈的步数和绕圈之前走的步数。 不是图的题目,直接做就行。 代码: #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; char map1[15][15]; int flag[15][15]; int main() { int n,m,k; int x,y; while(scanf("%d%d",&n,&m)&&n) { int sum1=0,sum2=0; memset(flag,0,sizeof(flag)); memset(map1,0,sizeof(map1)); scanf("%d",&k); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>map1[i][j]; } } x=1; y=k; while(x<=n&&x>=1&&y<=m&&y>=1) { if(map1[x][y]=='E') { flag[x][y]++; y++; sum2++; } else if(map1[x][y]=='W') { flag[x][y]++; y--; sum2++; } else if(map1[x][y]=='S') { flag[x][y]++; x++; sum2++; } else if(map1[x][y]=='N') { flag[x][y]++; x--; sum2++; } if(flag[x][y]==1) { sum1++; } else if(flag[x][y]>1) break; } if(sum1>0) printf("%d step(s) before a loop of %d step(s)\n",sum2-sum1*2,sum1); else printf("%d step(s) to exit\n",sum2); } return 0; }

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册