当前位置:网站首页>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
边栏推荐
- In the future, China Telecom will make cloud computing service the main business of China Telecom
- inet_pton()和inet_ntop()函数详解
- Several methods of initializing singleton variable in go language
- Understanding runloop in OC
- The third way to realize webrtc in embedded devices
- Well, these four ways to query the maximum value of sliding window are good
- Adobe experience design / XD 2020 software installation package (with installation tutorial)
- New features of Fedora 33 workstation
- The middle stage of vivo Monkey King activity
- 线上服务的FGC问题排查,看这篇就够了!
猜你喜欢
Efficient Estimation of Word Representations in Vector Space 论文笔记
Vscode plug-in configuration pointing North
Online course of tutorial system processing is in progress
Reading design patterns adapter patterns
Ali, Tencent, Baidu, Netease, meituan Android interview experience sharing, got Baidu, Tencent offer
Rainbow sorting | Dutch flag problem
彩虹排序 | 荷兰旗问题
Adobe Experience Design /Xd 2020软件安装包(附安装教程)
天啦撸!打印日志竟然只晓得 Log4j?
Understanding runloop in OC
随机推荐
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
导师制Unity网课 双十一优惠报名进行中
FGC online service troubleshooting, this is enough!
Handwriting Koa.js Source code
For and for... In, for each and map and for of
inet_ Pton () and INET_ Detailed explanation of ntop() function
苏宁基于知识图谱的大规模告警收敛和根因定位实践
Jsliang job series - 08 - handwritten promise
彩虹排序 | 荷兰旗问题
注意.NET Core进行请求转发问题
【golang】GC详解
大型项目Objective-C - NSURLSession接入短信验证码应用实例分享
Kubernetes业务日志收集与监控
SQL Chapter 2 Chapter 3
Tidb x micro banking reduces time consumption by 58%, and distributed architecture helps to realize inclusive finance
分库分表的 9种分布式主键ID 生成方案,挺全乎的
pytorch加载语音类自定义数据集
面试了一位33岁Android程序员,只会面向百度编程,居然要25k,被我一顿怼
Introduction to zero based im development (4): what is message timing consistency in IM systems?
彩虹排序 | 荷兰旗问题