程序员成长之路(Day 22)

本文介绍了在LintCode上进行的Java编程学习,涵盖了计算阶乘、使用final关键字、理解static变量、日期格式转换、确定一年中的第N天以及推荐结果的分散排列等题目。通过实例代码展示了如何解决这些问题,加深了对Java语法和编程技巧的理解。
摘要由CSDN通过智能技术生成

目录

学习内容:

LintCode刷题:

·计算阶乘​

·使用final关键字

·使用final变量

·bug修复之static关键字

 ·日期格式转换

 ·一年的第N天

·推荐结果打散 

学习产出:


学习内容:

LintCode刷题:

·计算阶乘

        for循环或者while循环都可以

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int resultnum = 1;
        int n = scanner.nextInt();
        for (int i=1;i<=n;i++){
            resultnum*=i;
        }
        System.out.println(resultnum);
    }
}

·使用final关键字

·使用final变量

        私有变量,建立方法去调用即可,记得加上final

public final class Solution {
    private final String SENTENCES="TO BE OR NOT TO BE";
    public final void test(String name){
        System.out.println(name+" said: "+SENTENCES);
    }
}

·bug修复之static关键字

         静态变量属于类变量,被所有的对象所共享,在内存中只有一个副本

import java.util.Random;

public class Solution {
    // 使用 static 关键字修饰变量,使其成为类变量,随着类的加载而存在,成为共享变量
    static int random1 = new Random().nextInt();
    int random2 = new Random().nextInt();
}

 ·日期格式转换

      1、字符串的分割

import java.text.ParseException;

public class Solution {

    public String dateConversion(String str) throws ParseException {
        int year = Integer.parseInt(str.substring(0,4));
        int month = Integer.parseInt(str.substring(5,7));
        int day = Integer.parseInt(str.substring(9,10));
        return year + "年" + month + "月" + day + "日";
    }
}

        2、   使用日历

     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(str);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        return calendar.get(Calendar.YEAR) + "年"
                + (calendar.get(Calendar.MONTH)+1) + "月"
                + calendar.get(Calendar.DAY_OF_MONTH) + "日";

        3、格式之间的变换

       SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
       Date date =simpleDateFormat.parse(str);
       SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy年M月d日");
       str = simpleDateFormat1.format(date);
       return str;

 ·一年的第N天

        使用日历的set和get方法 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Solution {

    public String specificCates(int year, int day) throws ParseException {
        Calendar calendar =Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.DAY_OF_YEAR,day);
        return (new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
    }
}

·推荐结果打散 

         创建两个队列来放图片P和图片V,遍历elements时需要记录第一个P的位置,所以使用for循环较好方便记录第一个P的位置,建立一个存放结果的的ArrayList,把第一个图片P前面的图片V放进去之后写一个算法添加一个指针指向0,在添加一个P之后把指针设置为n,然后图片V不为null时往ArrayList中加入图片V,并对指针进行--操作,在指针为1时退出这个加入图片V的循环。在图片V队列为null时,指针不为0时,直接跳出添加图片P的大循环即可

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {

    public List<String> scatter(List<String> elements, int n) {
        Queue<String> queueP = new LinkedList<>();
        Queue<String> queueV = new LinkedList<>();
        int firstP = -1;
        for (int i = 0; i < elements.size(); i++) {
            if (elements.get(i).charAt(0) == 'P') {
                if (firstP == -1) {
                    firstP = i;
                }
                queueP.offer(elements.get(i));
            } else {
                queueV.offer(elements.get(i));
            }
        }
        List<String> resultqueue = new ArrayList<>();
        while (firstP > 0) {
            firstP--;
            resultqueue.add(queueV.element());
            queueV.poll();
        }
        int step = 0;
        while (!queueP.isEmpty()) {
            resultqueue.add(queueP.element());
            queueP.poll();
            step = n;
            while (!queueV.isEmpty() && step > 1) {
                resultqueue.add(queueV.element());
                queueV.poll();
                step--;
            }
            if (step > 1) {
                break;
            }
        }
        return resultqueue;
    }
}


学习时间:

2021-8-30 9:00-11:45、13:30-18:23


学习产出:

刷题*6

学习博客*1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值