当前位置:网站首页>leetcode算法(1)
leetcode算法(1)
2020-11-09 12:58:00 【osc_tnexgcb8】
这是leetcode最简单的题了吧!!!
本次博主给大家推荐几个究极简单的算法, 简单归简单,但无论难易与否,万变不离其宗. 就由博主带大家从易到难,慢慢更新,积累. 如有更好的算法,还望大家分享.题目需求1:
小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次?
输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。
代码实现
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}));
}
}
题目需求2:
求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
代码实现
public class Solution3 {
public int sumNums(int n) {
//等差数列求和
return (1+n)*n/2;
}
public static void main(String[] args) {
Solution3 q = new Solution3();
System.out.println(q.sumNums(3));
}
}
题目需求3:
给你一个正方形矩阵 mat,请你返回矩阵对角线元素的和。
请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。

输出:1 + 5 + 9 + 3 + 7 = 25
注意,元素 mat[1][1] = 5 只会被计算一次。
代码实现
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}}));
}
}
题目需求4:
桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。
简化前代码实现
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}));
}
}
简化后代码实现
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}));
}
版权声明
本文为[osc_tnexgcb8]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4412486/blog/4709415
边栏推荐
- FGC online service troubleshooting, this is enough!
- How to use function framework to develop large web application
- From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
- List of wechat video Number broadcasters October 2020
- Use treeview tree menu bar (recursively call database to create menu automatically)
- 导师制Processing网课 双十一优惠进行中
- Detailed explanation of [golang] GC
- Gather in Beijing! Openi / O 2020 Qizhi Developer Conference enters countdown
- 利用 Python 一键下载网易云音乐 10W+ 乐库
- 外贸自建网站域名的选择— Namesilo 域名购买
猜你喜欢

嗯,查询滑动窗口最大值的这4种方法不错...

c语言小白学习历程第六篇

Understanding runloop in OC

10款必装软件,让Windows使用效率飞起!

What really drags you down is sunk costs

Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive

Reduce of Flink

导师制Unity网课 双十一优惠报名进行中

SQL Chapter 2 Chapter 3

IDEA rest-client,会了它我还没打开过postman
随机推荐
SQL Chapter 2 Chapter 3
TiDB x 微众银行 | 耗时降低 58%,分布式架构助力实现普惠金融
Implement crud operation
Jsliang job series - 08 - handwritten promise
JVM学习(五) -执行子系统
Android rights
inet_ Pton () and INET_ Detailed explanation of ntop() function
外贸自建网站域名的选择— Namesilo 域名购买
Interview summary on November 7, 2020 (interview 12K)
为什么我强烈建议要定制开发小程序,这些好处你需要了解
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
The history of C1 research in Shenzhen
The use of Android studio Aidl
苏宁基于知识图谱的大规模告警收敛和根因定位实践
Android 集成支付的四部曲
The third way to realize webrtc in embedded devices
Vscode plug-in configuration pointing North
Depth analysis based on synchronized lock
Stack & queue (go) of data structure and algorithm series
彩虹排序 | 荷兰旗问题