background
This post was written in 2018 on my personal blog page :), so start digging for gold! Last month, I was doing Java backend development in the company where I worked as an intern. Suddenly, one afternoon when my eyelids were fighting as usual, the front end said to me, “You did not return the number of consecutive check-in days, did you not have the demand to have a good look at the product”, huh? What, open jira and find that the requirements are updated, ok, then I will return a number of days to you!
Train of thought to solve
First of all, here’s the thing. Function is a sign-in to get red envelope activities, but the control cycle is a week, a cycle of 7 days, that is, from Monday to Sunday. Looked at the continuous number of days on the Internet to seek the method, well, may be my way wrong, not appetite, I will write a!
Analysis of the
1. The sign-in record data must be stored in the database, we take it out first, and then do processing;
2. For a 7-day week, write a number for each day rather than a long list of days. 1….. 7.
3. Continuous sign-in, that means that the middle can not be broken, broken is not continuous;
4. Need to know the mark of the current day to judge the condition processing. Second: the current day has been checked in;
Ok ~, after a meal analysis, the question becomes:
Given an array that does not contain repeated numbers and a number whose range is [1,7], find whether there is a continuous interval containing the given number or a number 1 less than the given number. Find the length of the interval (if there is only one number, the length is 1).
Example: array = {1,2,4,5,6}, index = 7
Out: 3
Example: array = {1,2,4}, index = 6
Out: 0
rendering
Figure 1 | Figure 2 |
---|
hands-on
Go directly to code ~
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Index of days logged in
int[] array = {1.2.4};
// Computes the current day index
int currentDayIndex = 7;
int count = judgeContinueDay(array, currentDayIndex);
System.out.println(count);
}
private static int judgeContinueDay(int[] array, int currentDayIndex) {
/** * Omit */ to determine the value range
// I didn't check in one day this week
if (array.length == 0) {
return 0;
}
// Sort first, from smallest to largest
Arrays.sort(array);
/** * Note: Use the binarySearch method to find if the array is present. Make sure the array is sorted before using this method. If not found, returns -1 or -(insertion point). * For example, if {1,2,4,7} is sorted, returns -3 (not in the * array, the index will start at 1). The detailed rules can be consulted. * /
// Whether the current day exists
int index1 = Arrays.binarySearch(array, currentDayIndex);
// Whether the previous day of the current day exists
int index2 = Arrays.binarySearch(array, currentDayIndex - 1);
int tmpIndex;
if (index1 < 0 && index2 < 0) { // The signature is terminated for at least one day
return 0;
} else if (index1 < 0) { // I haven't signed it today, judging from yesterday
tmpIndex = index2;
} else { // Today is signed, judging from today
tmpIndex = index1;
}
// If you can get here, you must have checked in for at least 1 consecutive day
int count = 1;
// If the index is disconnected, it is discontinuous
for (int i = tmpIndex; i > 0; i--) {
if (array[i] - 1 == array[i - 1]) {
count++;
} else {
break; }}returncount; }}Copy the code
One thing to note: the binarySearch method of Arrays and the return value issue is fine.
The end of the
I have little experience in life, and what I write is very basic. Maybe the method is not the best. I just make a record, looking back and witnessing my growth all the way. I hope you don’t hesitate to give me advice!