当前位置:网站首页>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.]所创,转载请带上原文链接,感谢
边栏推荐
- Viewing PDB files from the angle of assembly
- 接口测试如何在post请求中传递文件
- 为什么我强烈建议要定制开发小程序,这些好处你需要了解
- An attempt to read or write to protected memory occurred using the CopyMemory API. This usually indicates that other memory is corrupted.
- Solve the problem of idea shortcut key Alt + insert invalid
- Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
- Visual Studio (MAC) installation process notes
- 块级元素和行内元素
- A simple ability determines whether you will learn!
- 苏宁基于知识图谱的大规模告警收敛和根因定位实践
猜你喜欢

JVM学习(五) -执行子系统

How to use function framework to develop large web application

The history of C1 research in Shenzhen

如何用函数框架快速开发大型 Web 应用 | 实战

TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融

Efficient estimation of word representations in vector space

Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business

Solve the problem of idea shortcut key Alt + insert invalid

What really drags you down is sunk costs

Pay attention to the request forwarding problem of. Net core
随机推荐
【golang】GC详解
通配符SSL证书应该去哪里注册申请
Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business
Viewing PDB files from the angle of assembly
Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
SEO见风使舵,是对还是错?
JVM learning (5) - execution subsystem
Rainbow sorting | Dutch flag problem
From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
Method of creating flat panel simulator by Android studio
块级元素和行内元素
除了解析域名,DNS还能干吗?
03.优先链接模型
块级元素和行内元素
Clock service Android implementation of alarm clock
写给我自己的一封信
Adobe Experience Design /Xd 2020软件安装包(附安装教程)
多线程真的比单线程快?
面试了一位33岁Android程序员,只会面向百度编程,居然要25k,被我一顿怼
Kubernetes business log collection and monitoring