当前位置:网站首页>Leetcode algorithm (1)
Leetcode algorithm (1)
2020-11-09 12:58:00 【I'm sorry.】
This is a leetcode The simplest question !!!
This blogger recommends several simple algorithms to you , Simple to simple , But whether it's easy or not , All change is the same . The blogger will take you from easy to difficult , Update slowly , The accumulation of . If there is a better algorithm , I hope you can share .Algorithm
Topic requirements 1:
Small A and Small B Playing guessing numbers . Small B Each time from 1, 2, 3 Choose one randomly , Small A Every time 1, 2, 3 Choose a guess . They played the game three times in all , Please return Small A I guessed it right several times ?
Input guess
The array is Small A Every guess ,answer
The array is Small B Every choice .guess
and answer
The length of is equal to 3.
Code implementation
public class Solution {
public int game(int[] guess, int[] answer) {
int count = 0;
for (int i = 0; i < 3; i++) {
if (guess[i] == answer[i]){
count+=1;
}
}
return count;
}
public static void main(String[] args) {
Solution n2 = new Solution();
System.out.println(n2.game(new int[]{
1,2,3}, new int[]{
1, 2, 2}));
}
}
Topic requirements 2:
seek 1+2+...+n
, It is required that multiplication and division shall not be used 、for、while、if、else、switch、case Wait for keywords and conditional statements (A?B:C).
Code implementation
public class Solution3 {
public int sumNums(int n) {
// Sum of equal difference sequence
return (1+n)*n/2;
}
public static void main(String[] args) {
Solution3 q = new Solution3();
System.out.println(q.sumNums(3));
}
}
Topic requirements 3:
Here's a square matrix mat
, Please return the sum of the diagonal elements of the matrix .
Please return the sum of the elements on the main diagonal and the sub diagonal of the matrix and not on the main diagonal .
Output :1 + 5 + 9 + 3 + 7 = 25
Be careful , Elements mat[1][1] = 5 Will only be counted once .
Code implementation
public class Solution4 {
int diagonal= 0;
public int diagonalSum(int[][] mat) {
for (int i=0;i<mat.length;i++){
for (int j = 0; j < mat[i].length; j++) {
if ( i==j || i+j == mat.length-1){
diagonal += mat[i][j];
}
}
}
return diagonal;
}
public static void main(String[] args) {
Solution4 s = new Solution4();
System.out.println(s.diagonalSum(new int[][]{
{
1,2,3},{
4,5,6},{
7,8,9}}));
}
}
Topic requirements 4:
It's on the table n
Money is deducted from the pile , The number of each heap is stored in the array coins
in . We can choose any pile at a time , Take one or two of them , Ask for the minimum number of times to deduct money from all efforts .
Code implementation before simplification
public class Solution5 {
public int minCount(int[] coins) {
int count1 = 0;
int count2 = 0;
for (int i = 0; i < coins.length; i++) {
if (coins[i] % 2 ==0){
count1 += coins[i]/2;
}
if (coins[i] % 2 !=0){
count2 += coins[i]/2+1;
}
}
return count1+count2;
}
public static void main(String[] args) {
Solution5 s = new Solution5();
System.out.println(s.minCount(new int[]{
1,1,1}));
}
}
Simplified code implementation
public class Solution5 {
public int minCount1(int[] coins) {
int c = 0;
for (int i = 0; i < coins.length; i++) {
c += coins[i] / 2;
c += coins[i] % 2;
}
return c;
}
public static void main(String[] args) {
Solution5 s = new Solution5();
System.out.println(s.minCount1(new int[]{
2,5,7}));
}
版权声明
本文为[I'm sorry.]所创,转载请带上原文链接,感谢
边栏推荐
- After SQL group query, get the first n records of each group
- 微信视频号播主排行榜2020年10月
- 【分布式】分布式锁都有哪些实现方案?
- Clock service Android implementation of alarm clock
- inet_ Pton () and INET_ Detailed explanation of ntop() function
- List of wechat video Number broadcasters October 2020
- Interface tests how to pass files in post requests
- Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
- Configure switch trunk interface traffic local priority forwarding (cluster / stack)
- Kubernetes business log collection and monitoring
猜你喜欢
Interface tests how to pass files in post requests
A simple ability determines whether you will learn!
Use treeview tree menu bar (recursively call database to create menu automatically)
导师制Processing网课 双十一优惠进行中
Handwriting Koa.js Source code
块级元素和行内元素
手写Koa.js源码
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
20201107第16课,使用Apache服务部署静态网站;使用Vsftpd服务传输文件
实现商品CRUD操作
随机推荐
Is SEO right or wrong?
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
The third way to realize webrtc in embedded devices
在嵌入式设备中实现webrtc的第三种方式③
Tutorial system unity online course double 11 preferential registration is in progress
Handwriting Koa.js Source code
Mobile security reinforcement helps app achieve comprehensive and effective security protection
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
The use of Android studio Aidl
嗯,查询滑动窗口最大值的这4种方法不错....
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
彩虹排序 | 荷兰旗问题
Fedora 33 Workstation 的新功能
Android check box and echo
List of wechat video Number broadcasters October 2020
Online course of tutorial system processing is in progress
Python zero basics tutorial (01)
零基础IM开发入门(四):什么是IM系统的消息时序一致性?
New features of Fedora 33 workstation
在企业的降本增效诉求下,Cube如何助力科盾业务容器化“一步到位”?