【Lintcode】1178. Student Attendance Record I

题目地址:

https://www.lintcode.com/problem/student-attendance-record-i/description

给定一个出勤记录,以字符串表示,如果有超过一个A或者连续超过两个L,则返回false,否则返回true。

直接遍历字符串再判断。代码如下:

public class Solution {
    /**
     * @param s: a string
     * @return: whether the student could be rewarded according to his attendance record
     */
    public boolean checkRecord(String s) {
        // Write your code here
        int countA = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'A') {
                countA++;
            }
            
            if (s.charAt(i) == 'L') {
                int countL = 0, j = i;
                while (j < s.length() && s.charAt(j) == 'L') {
                    countL++;
                    j++;
                }
                
                if (countL > 2) {
                    return false;
                }
                
                i = j - 1;
            }
        }
        
        return countA <= 1;
    }
}

时间复杂度 O ( l s ) O(l_s) O(ls),空间 O ( 1 ) O(1) O(1)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@RequestMapping(value = "export", method = RequestMethod.GET) public void exportToExcel(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { System.out.println("111"); List<Attendance> attendanceList = (List<Attendance>) session.getAttribute("list"); // 创建 Excel 文档 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Attendance"); // 创建表头 Row headerRow = sheet.createRow(0); String[] headers = {"考勤ID", "用户ID", "用户名", "考勤时间", "考勤类型"}; for (int i = 0; i < headers.length; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } // 填充数据 int rowIndex = 1; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (Attendance attendance : attendanceList) { Row dataRow = sheet.createRow(rowIndex); dataRow.createCell(0).setCellValue(attendance.getAttendanceid()); dataRow.createCell(1).setCellValue(attendance.getUserid()); dataRow.createCell(2).setCellValue(attendance.getUsername()); dataRow.createCell(3).setCellValue(sdf.format(attendance.getDate())); dataRow.createCell(4).setCellValue(attendance.getType()); rowIndex++; } // 设置响应头 response.setHeader("content-type", "application/octet-stream"); response.setContentType("application/force-download"); response.addHeader("Content-Disposition", "attachment; filename=attendance.xlsx"); // 将 Excel 数据写入响应输出流 OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); workbook.close(); outputStream.flush(); outputStream.close(); }
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值