Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85ffc6b

Browse files
update-0008-string-to-integer
1 parent0a2d881 commit85ffc6b

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

‎cpp/0008-string-to-integer.cpp‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include<bits/stdc++.h>
2+
usingnamespacestd;
3+
4+
/*
5+
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
6+
7+
The algorithm for myAtoi(string s) is as follows:
8+
9+
Whitespace: Ignore any leading whitespace (" ").
10+
Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present.
11+
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.
12+
Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.
13+
Return the integer as the final result.
14+
Example :
15+
16+
Input: s = " -042"
17+
18+
Output: -42
19+
20+
Explanation:
21+
22+
Step 1: " -042" (leading whitespace is read and ignored)
23+
^
24+
Step 2: " -042" ('-' is read, so the result should be negative)
25+
^
26+
Step 3: " -042" ("042" is read in, leading zeros ignored in the result)
27+
*/
28+
29+
classSolution {
30+
public:
31+
intmyAtoi(string s) {
32+
int sign=1;
33+
bool tag=false;
34+
int j=0,ind;
35+
double ans=0;
36+
37+
// trim the string till we get number
38+
for (int i=0;i<s.size();i++){
39+
if (s[i]=='') {
40+
41+
continue;}
42+
else{
43+
if (s[i]=='-') {
44+
sign =-1;
45+
j=i+1;
46+
break;
47+
}
48+
elseif(s[i]=='+'){
49+
sign=1;
50+
j=i+1;
51+
break;
52+
}
53+
else{
54+
j=i;
55+
break;
56+
}
57+
}
58+
}
59+
60+
// convert string to integer
61+
62+
for (int i=j;i<s.size();i++){
63+
if (int(s[i])>47 &&int(s[i])<58){
64+
ans=ans*10;
65+
ans=ans+int(s[i])-48;
66+
67+
}
68+
else {
69+
break;
70+
}
71+
}
72+
ans=sign*ans;
73+
longlong y=pow(2,31);
74+
if (ans <-1*y)return -1*y;
75+
elseif (ans > y-1)return y-1;
76+
elsereturn ans;
77+
}
78+
};

‎python/0008-string-to-integer.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer.
3+
4+
The algorithm for myAtoi(string s) is as follows:
5+
6+
Whitespace: Ignore any leading whitespace (" ").
7+
Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present.
8+
Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.
9+
Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.
10+
Return the integer as the final result.
11+
Example :
12+
13+
Input: s = " -042"
14+
15+
Output: -42
16+
17+
Explanation:
18+
19+
Step 1: " -042" (leading whitespace is read and ignored)
20+
^
21+
Step 2: " -042" ('-' is read, so the result should be negative)
22+
^
23+
Step 3: " -042" ("042" is read in, leading zeros ignored in the result)
24+
'''
25+
26+
classSolution:
27+
defmyAtoi(self,s:str)->int:
28+
s=s.strip()
29+
sign=1
30+
iflen(s)==0:
31+
return0
32+
33+
ifs[0]=="-"ors[0]=="+" :
34+
sign=-1ifs[0]=="-"else1
35+
s=s[1:]
36+
37+
l=0
38+
forcins:
39+
k=ord(c)-48
40+
ifkinrange(0,10):
41+
l=l*10
42+
l=l+k
43+
else:
44+
break
45+
46+
n=sign*l
47+
ifn>=(2**31):
48+
return (2**31)-1
49+
elifn<(-(2**31)):
50+
return-(2**31)
51+
returnn

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp