当前位置:网站首页>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.]所创,转载请带上原文链接,感谢
边栏推荐
- 在企业的降本增效诉求下,Cube如何助力科盾业务容器化“一步到位”?
- Suning's practice of large scale alarm convergence and root cause location based on Knowledge Map
- Navigation component of Android architecture (2)
- 未来中国电信将把云计算服务打造成为中国电信的主业
- Android studio AVD
- 为wget命令设置代理
- 7-10倍写入性能提升:剖析WiredTiger数据页无锁及压缩黑科技
- In the future, China Telecom will make cloud computing service the main business of China Telecom
- Three practical skills of Medical Project Management
- List of wechat video Number broadcasters October 2020
猜你喜欢

从汇编的角度看pdb文件

天啦撸!打印日志竟然只晓得 Log4j?

未来中国电信将把云计算服务打造成为中国电信的主业

Show profile analysis of SQL statement performance overhead

c语言(循环链表)实现贪吃蛇的基本功能

Implement crud operation

JVM learning (6) - memory model and thread

医疗项目管理的三种实用技巧

Well, these four ways to query the maximum value of sliding window are good

JVM学习(五) -执行子系统
随机推荐
Vscode plug-in configuration pointing North
移动安全加固助力 App 实现全面、有效的安全防护
嗯,查询滑动窗口最大值的这4种方法不错....
彩虹排序 | 荷兰旗问题
Windows must be installed with efficiency software!
Depth analysis based on synchronized lock
Online course of tutorial system processing is in progress
Clock service Android implementation of alarm clock
Impact of libssl on CentOS login
Pay attention to the request forwarding problem of. Net core
03.优先链接模型
c语言小白学习历程第六篇
零基础IM开发入门(四):什么是IM系统的消息时序一致性?
分库分表的 9种分布式主键ID 生成方案,挺全乎的
医疗项目管理的三种实用技巧
【golang】GC详解
技美那么贵,不如找顾问 | AALab企业顾问业务
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
手写Koa.js源码
Android NDK 开发实战 - 微信公众号二维码检测