ARTS Week 24
Algorithm
This week’s LeetCode problem is 69. Sqrt(x)
Given a non-negative integer x
, compute and return the square root of x
.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5)
or x ** 0.5
.
Input: x = 4
Output: 2
Dichotomy can be used for processing, because using multiplication to judge may overflow, so a better processing strategy is to use division for calculation. And the final result only needs to keep the integer part, so when mid+1 > x / (mid+1)
and mid < x / mid
, then the final result at this moment is mid
.
class Solution {
public int mySqrt(int x) {
if (x == 0 || x == 1) {
return x;
}
int left = 1;
int right = x;
int ans = -1;
int mid;
while (left < right) {
mid = left + (right - left) / 2; if (mid+1 <= x / (mid+1)) {
left = mid;
} else if (mid > x / mid) {
right = mid;
} else {
ans = mid;
break;
}
}
return ans;
}
}
Review
This week’s Review is for the following article: 3 ways I configure SSH for privacy
The author describes how to optimize the SSH experience and protect the server from unauthorized access. Here are the author’s specific recommendations
- Change the default port
The author had an SSH server default port TCP 22 over one cloud provider, and the average attacks per minute were 24. After changing the port to a much higher number, TCP 45678, the average of people connecting and guessing any username or password was two per day.
To change the default port for SSH, open /etc/ssh/sshd_config in your favorite text editor and change the value of the Port from 22 to some number greater than 1024.
#Port 22122
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
Once you’ve changed the port and saved the file, restart the SSH server:
$ sudo systemctl restart sshd
2. No more passwords
The author recommends using asymmetric keys for authentication, one needs to generate an SSH key pair first, the public key needs to be transferred to the server, and the other is private and shared with anyone. Use ssh-keygen
to create a new key, and use the -t
option to specify a good, up-to-date encryption library, such as ed25519
:
$ ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (~/.ssh/id_ed25519):
Follow the prompts to enter the location and custom name of the key, and you can specify a password. Of course, you can also follow the default configuration all the way.
Next use the ssh-copy-id
command to copy the key to the server, for example the following command copies the public key to a server named example.com
.
$ ssh-copy-id jgarrido@example.com
Once you can log in without a password from the computer, edit the option PasswordAuthentication
in sshd_config
to allow password login to no
Finally, use sudo systemctl restart sshd
to restart the ssh service.
3. Decide who can log in
Most Linux distributions do not allow root users to log in via SSH. Open the SSH configuration file sshd_config
and add the following line:
AllowUsers jgarrido jane tux
This will only allow the three users jgarrido, jane, and tux to log in remotely to operate.
Apart from that, there are tons of useful functions and options in the sshd_config
file, and you can also use applications like Fail2ban
to further secure your SSH service.
Tip
The difference between the three comparison functions strcmp
, strncmp
, and memcmp
in the C language, all three of them can be used to compare whether the strings are the same, when the return value is equal to 0, it means the same, and the return value is not 0 means not same. First compare according to their statement:
function name function declaration whether to pass in the length to be compared how to end the comparison strcmp int strcmp(const char *s1, const char *s2); not required ends s1 and s2 with \0
strncmp int strncmp(const char *s1, const char *s2, size_t n); Required Use the incoming parameter n as the comparison length and \0
as the end of s1 and s2 memcmp int memcmp(const void *s1, const void *s2, size_t n); Required Use the incoming parameter n as the comparison length
It should be noted that strncmp()
will have two conditions to judge the end, one is the incoming length, the other is the end of the string \0
, the content after the string \0
and will not be compared.
#include <stdio.h>
#include <string.h>int main()
{
const char s1[] = "abcd\0\0\0\0";
const char s2[] = "abcd\0xyz";
const char s3[] = "abcdabcd"; printf("----- strcmp() -----\n");
printf("strcmp(s1, s2) = %d\n", strcmp(s1, s2)); // 0
printf("strcmp(s1, s2) = %d\n", strcmp(s1, s2)); // 0
printf("strcmp(s1, s2) = %d\n", strcmp(s2, s3)); // -97 printf("----- strncmp() -----\n");
printf("strncmp(s1, s2, 4) = %d\n", strncmp(s1, s2, 5)); // 0
printf("strncmp(s1, s3, 4) = %d\n", strncmp(s1, s3, 5)); // -97
printf("strncmp(s2, s3, 4) = %d\n", strncmp(s2, s3, 5)); // -97
printf("strncmp(s1, s2, 8) = %d\n", strncmp(s1, s2, 8)); // 0
printf("strncmp(s1, s3, 8) = %d\n", strncmp(s1, s3, 8)); // -97
printf("strncmp(s2, s3, 8) = %d\n", strncmp(s2, s3, 8)); // -97 printf("----- memcmp() -----\n");
printf("memcmp(s1, s2, 4) = %d\n", memcmp(s1, s2, 5)); // 0
printf("memcmp(s1, s3, 4) = %d\n", memcmp(s1, s3, 5)); // -97
printf("memcmp(s2, s3, 4) = %d\n", memcmp(s2, s3, 5)); // -97
printf("memcmp(s1, s2, 8) = %d\n", memcmp(s1, s2, 8)); // -120
printf("memcmp(s1, s3, 8) = %d\n", memcmp(s1, s3, 8)); // -97
printf("memcmp(s2, s3, 8) = %d\n", memcmp(s2, s3, 8)); // -97 return 0;
}
Share
This week, of course, I want to share a separate article that I have been dragging for about half a year. The following three articles mainly introduce how to use C language to read and write CSV files. I am very happy to hear your thoughts and suggestions :)
// Note: These essays are not published yet
Reading and Writing CSV files in C language — Part 1. Basics
Reading and Writing CSV files in C language — Part 2. Advanced — writing CSV
Reading and Writing CSV files in C language — Part 3. Advanced — reading CSV