当前位置:网站首页>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
边栏推荐
- 基于synchronized锁的深度解析
- Learning notes of nodejs
- FGC online service troubleshooting, this is enough!
- Efficient Estimation of Word Representations in Vector Space 论文笔记
- “开源软件供应链点亮计划 - 暑期 2020”公布结果 基于 ChubaoFS 开发的项目获得最佳质量奖
- 解密未来数据库设计:MongoDB新存储引擎WiredTiger实现(事务篇)
- EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
- How to use function framework to develop large web application
- 微信视频号播主排行榜2020年10月
- 块级元素和行内元素
猜你喜欢
随机推荐
Oh, my God! Printing log only knows log4j?
Detailed explanation of [golang] GC
安全(杂记)
Idea rest client, yes, I haven't opened postman yet
Idea rest client, yes, I haven't opened postman yet
Front end code style practice prettier + eslint + git hook + lint staged
Rainbow sorting | Dutch flag problem
Android NDK 开发实战 - 微信公众号二维码检测
Suning's practice of large scale alarm convergence and root cause location based on Knowledge Map
接口测试如何在post请求中传递文件
Download Netease cloud music 10W + music library with Python
Android架构之Navigation组件(二)
Viewing PDB files from the angle of assembly
android studio AIDL的使用
JVM learning (6) - memory model and thread
服务应用 ClockService安卓实现闹钟
Flink 系例 之 Reduce
New features of Fedora 33 workstation
手写Koa.js源码
pytorch加载语音类自定义数据集