Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //version 1(use library function)
- public class Solution {
- /**
- * @param str a string
- * @return an integer
- */
- public int stringToInteger(String str) {
- return Integer.valueOf(str);
- }
- }
- //version 2
- public class Solution {
- /**
- * @param str a string
- * @return an integer
- */
- public int stringToInteger(String str) {
- // Write your code here
- int integer = 0;
- int Minus = 0;
- if (str.charAt(0) == '-'){
- Minus = 1;
- }
- for (int i = Minus; i < str.length(); i++){
- integer = integer * 10 + str.charAt(i)-'0';
- }
- if (Minus == 1){
- integer = -integer;
- }
- return integer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement