ARTS #030
ARTS #030
ARTS 030
This is article 30
ARTS is an activity initiated by
由左耳朵耗子--陈皓: Do at least one leetcode algorithm question every week, read and comment on at least one English technical article, learn at least one technical skill, and share an article with opinions and thoughts. (That is, Algorithm, Review, Tip, and Share are referred to as ARTS) and persist for at least one year.
Algorihm algorithm question
7. Integer reversal
Difficulty: Easy
Given a 32-bit signed integer, you need to reverse each bit in the integer.
Example 1:
输入: 123
输出: 321
** Example 2:**
输入: -123
输出: -321
Example 3:
输入: 120
输出: 21
Note:
Assuming that our environment can only store 32-bit signed integers, the value range is [−2<sup>31</sup>, 2<sup>31 </sup>− 1]. Please follow this assumption and return 0 if the integer overflows after inversion.
Solution
Language: C
int reverse(int x) {
long temp,result=0;;
if(x>1534236461||x>=2147483642||x<(-2147483641)) return 0;
while(x){
temp=x%10;
x=x/10;
result=result*10+temp;
}
if(result>2147483642||result<(-2147483641)) return 0;
return result;
}
int reverse(int x) {
long temp,result=0;
while(x){
temp=x%10;
x=x/10;
result=result*10+temp;
}
if(result>INT_MAX||result<INT_MIN) return 0;
return result;
}
8. String conversion integer (atoi)
Difficulty: Medium
Please implement a atoi function so that it can convert a string into an integer.
First, the function discards useless leading space characters as necessary until it finds the first non-space character.
When the first non-null character we find is a positive or negative sign, the symbol is combined with as many consecutive digits as possible as the positive or negative sign of the integer; if the first non-null character is a number, it is directly combined with the subsequent consecutive numeric characters to form an integer.
The string may also contain extra characters after the valid integer part. These characters can be ignored and should have no effect on the function.
Note: Your function does not need to convert if the first non-space character in the string is not a valid integer character, the string is empty, or the string contains only whitespace characters.
In any case, if the function cannot perform a valid conversion, return 0.
Description:
Assuming that our environment can only store 32-bit signed integers, the range of values is [−2<sup>31</sup>, 2<sup>31 </sup>− 1]. If the value exceeds this range, qing returns INT_MAX (2<sup>31 </sup>− 1) or INT_MIN (−2<sup>31</sup>).
Example 1:
输入: "42"
输出: 42
Example 2:
输入: " -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
Example 3:
输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
Example 4:
输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
因此无法执行有效的转换。```
**Example 5:**
输入: “-91283472332” 输出: -2147483648 解释: 数字 “-91283472332” 超过 32 位有符号整数范围。 因此返回 INT_MIN (−231) 。
#### Solution
Language: **C**
```c
#include "limits.h"
int myAtoi(char* str) {
long temp = 0;
const char *ptr = str; //ptr保存str字符串开头
while (*str) {
if (*str == ' ') {
str++;
continue;
}
if (*str == '-') {
str++;
long tem = 0;
while (*str) {
if (*str>= '0' && *str<= '9') {
tem= tem * 10 + (*str - '0');
if(-tem<INT_MIN)
return INT_MIN;
str++;
continue;
}
else{
return -tem;
}
}
return -tem;
}
else if(*str == '+'){
str++;
long tem = 0;
while (*str) {
if (*str>= '0' && *str<= '9') {
tem= tem * 10 + (*str - '0');
if(tem> INT_MAX)
return INT_MAX;
str++;
continue;
}
else{
return tem;
}
}
return tem;
}
else{
int tem = 0;
while (*str) {
if (*str>= '0' && *str<= '9') {
tem= tem * 10 + (*str - '0');
str++;
if(tem> INT_MAX)
return INT_MAX;
continue;
}
else{
return tem;
}
}
return tem;
}
break;
}
return 0;
}
The first submission passed, but the code was ugly
This is the code after tidying up:
int myAtoi(char* str) {
while (*str) {
if (*str == ' ') {
str++;
continue;
}
if(*str == '-' || *str == '+' || (*str>= '0' && *str<= '9')){
int f=1;
if ( *str == '-') {
f=-1;
}
if (*str == '-' || *str == '+') {
str++;
}
long tem = 0;
while (*str) {
if (*str>= '0' && *str<= '9') {
tem= tem * 10 + (*str - '0');
if (f==1) {
if(tem> INT_MAX)
return INT_MAX;
}
else{
if(-tem<INT_MIN)
return INT_MIN;
}
str++;
continue;
}
else{
return tem * f;
}
}
return tem * f;
}
else{
return 0;
}
}
return 0;
}
The code is shorter, but not as easy to understand as the first one.
Review
https://dandan2009.github.io/2019/04/25/ios-architecture-patterns/ This article conducts a comparative analysis of MVC, MVP, MVVM and VIPER, commonly used architectures for iOS development.
Tips
The following UIGestureRecognizer agents can solve the problem of gesture conflicts:
//Method called when starting gesture recognition. Returning NO will end the recognition and no longer trigger gestures. Use: gesture recognition can be used at the position specified by the control.
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { return YES; }
//Whether multi-gesture triggering is supported, return YES, then multiple gestures can trigger the method at the same time, return NO, it is mutually exclusive. //Whether multiple gesture recognizers are allowed to recognize together, and whether the gesture recognition of a control blocks the gesture recognition from continuing to propagate downward. The default return is NO; if it is YES, after the upper object of the responder chain triggers the gesture recognition, if the lower object also adds the gesture and successfully recognizes it, it will continue to execute, otherwise it will not continue to propagate after the upper object is recognized. -(BOOL)gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{
}
Share
I’m renovating a house recently, and I plan to do the water and electricity myself. The water has already been done. My experience after doing it is that it seems difficult to do it, but it can’t be said to be difficult. Even if it’s the first time I do it, some unexpected things always happen. For example, when using a hot melter to take over the pipe, you have to control the temperature. At the beginning, I took a few scrap pipes and tried them in advance. Some were bent and some were ironed. The process was a bit tortuous, but the results were still satisfying. In fact, doing water pipes is the same as doing development, both require experience.
读完之后,下一步看什么
如果还想继续了解,可以从下面几个方向接着读。