当前位置:网站首页>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.]所创,转载请带上原文链接,感谢
边栏推荐
- Well, these four ways to query the maximum value of sliding window are good
- Handwriting Koa.js Source code
- Suning's practice of large scale alarm convergence and root cause location based on Knowledge Map
- 除了解析域名,DNS还能干吗?
- JVM学习(六)-内存模型和线程
- What really drags you down is sunk costs
- Python zero basics tutorial (01)
- SQL Chapter 2 Chapter 3
- JVM learning (5) - execution subsystem
- Solve the problem of idea shortcut key Alt + insert invalid
猜你喜欢
技美那么贵,不如找顾问 | AALab企业顾问业务
AndroidStudio导入定制化的framework classess.jar AS 4.0.1版本亲测有效
The choice of domain name of foreign trade self built website
解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)
The use of Android studio Aidl
基于synchronized锁的深度解析
深圳C1考证历程
Rainbow sorting | Dutch flag problem
医疗项目管理的三种实用技巧
使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
随机推荐
块级元素和行内元素
10款必装软件,让Windows使用效率飞起!
Idea solves garbled Chinese output of YML configuration file
解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)
深圳C1考证历程
After SQL group query, get the first n records of each group
Online course of tutorial system processing is in progress
Setting up a proxy for the WGet command
Use treeview tree menu bar (recursively call database to create menu automatically)
JVM learning (5) - execution subsystem
C语言—————三子棋游戏
用一种简单的方式实现终端文字粘贴板
手写Koa.js源码
在嵌入式设备中实现webrtc的第三种方式③
【golang】GC详解
Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business
IDEA解决yml配置文件中文输出乱码问题
彩虹排序 | 荷兰旗问题
AI fresh student's annual salary has increased to 400000, you can still make a career change now!
Detailed explanation of [golang] GC