Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).

The algorithm for myAtoi(string s) is as follows:

  1. Read in and ignore any leading whitespace.
  2. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
  3. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored.
  4. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
  5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
  6. Return the integer as the final result.

Note:

  • Only the space character ' ' is considered a whitespace character.
  • Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
class Solution {
public:
    int64_t myAtoi(string s) {
        int64_t ans = 0;
        int start = -1;
        bool neg = false;
        map<int8_t, bool> operators;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] >= 48 && s[i] <= 57)
            {
                ans *= 10;
                ans += (int64_t)((int)s[i] - 48);
                if (ans > (int64_t)pow(2, 31))
                {
                    ans = (int64_t)pow(2, 31);
                    break;
                }
                if (start == -1) start = i;
            }
            else
            {
                if (s[i] != 32 && s[i] != 43 && s[i] != 45) break;
                if (i > 0 && (s[i-1] >= 48 && s[i-1] <= 57)) break;
                if (s[i] == 43) operators[i] = true;
                if (s[i] == 45) operators[i] = false;
                if (operators.size() > 1) break;
            }
            if (start != -1 && operators.size() > 0 && \
                abs(std::prev(operators.end())->first - start) > 1)
            {
                ans = 0;
                break;
            }
        } 
        if (ans > 0 && operators.size() > 0)
        {
            if (abs(std::prev(operators.end())->first - start) == 1)
            {
                if (std::prev(operators.end())->second == false) ans *= -1;
            }
        }
        return (ans == (int64_t)pow(2, 31) ? --ans : ans);
    }
};

'Challenge' 카테고리의 다른 글

[Codility] Passing Cars  (0) 2021.09.27
[LeetCode] Palindrome Number  (0) 2021.09.24
[LeetCode] Reverse Integer  (0) 2021.09.14
[LeetCode] ZigZag Conversion  (0) 2021.09.13
Check for pair with a given sum  (0) 2021.09.13

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

 

Example 1:

Input: x = 123 Output: 321

Example 2:

Input: x = -123 Output: -321

Example 3:

Input: x = 120 Output: 21

Example 4:

Input: x = 0 Output: 0

 

Constraints:

  • -231 <= x <= 231 - 1
class Solution {
public:
    long reverse(long x) {
        bool neg = false;
        if (x < 0)
        {
            neg = true;
            x *= -1;
        }

        long reversed = 0;
        while (x > 0)
        {
            reversed *= 10;
            reversed += (x % 10);
            x /= 10;
        }
        if (reversed > INT_MAX)
        {
            return 0;
        }
        else
        {
            reversed = (neg ? (reversed * -1) : reversed);
        }
        return reversed;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] Palindrome Number  (0) 2021.09.24
[LeetCode] String to Integer (atoi)  (0) 2021.09.24
[LeetCode] ZigZag Conversion  (0) 2021.09.13
Check for pair with a given sum  (0) 2021.09.13
[LeetCode] Longest Palindromic Substring  (0) 2021.09.12

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N A P L S I I G Y I R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

 

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I

Example 3:

Input: s = "A", numRows = 1 Output: "A"

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000
class Solution {
public:
    string convert(string s, int numRows) {
        string ans = "";
        int step = (numRows + (numRows - 2));
        int loops = step < 1 ? 1 : (int)ceil((double)s.length() / (double)step);
        int cur = 0, start, end;
        while (ans.length() < s.length())
        {
            for (int i = 0; i < loops; i++)
            {
                start = (step * i) + cur;
                if (cur == 0)
                {
                    if (start < s.length()) ans += s[start];
                }
                else
                {
                    end = (step + (step * i)) - cur;
                    if (start < s.length()) ans += s[start];
                    if (end < s.length() && end > start) ans += s[end];
                }
            }
            cur++;
        }
        return ans;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] String to Integer (atoi)  (0) 2021.09.24
[LeetCode] Reverse Integer  (0) 2021.09.14
Check for pair with a given sum  (0) 2021.09.13
[LeetCode] Longest Palindromic Substring  (0) 2021.09.12
[LeetCode] Median of Two Sorted Arrays  (0) 2021.09.11

There is an unordered array, P, consisting of integers that are less than 100,000.

Check whether there is a pair of values that can sum up to the given sum, k.

 

bool test(const vector<int>& arr, int k)
{
	unordered_set<int> unord;
	for (int value : arr)
	{
		if (unord.find(value) != unord.end())
		{
			return true;
		}
		unord.insert(k - value);
	}
	return false;
}

 

+ Recent posts