Leetcode Problems

https://leetcode.com/problems/reverse-integer/

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

Constraints:

  • -231 <= x <= 231 - 1
				
					const reverse = (x: number) => {
  const absoluteNumber = Math.abs(x);
  const isNegative = x <= 0;
  let y = absoluteNumber.toString();
  if (y.length > 10 || (y.length === 10 && Number(y[y.length - 1]) >= 3))
    return 0;
  let reversed = Number(y.split("").reverse().join(""));
  return reversed > 2147483647 || reversed < -2147483648
    ? 0
    : isNegative
    ? -reversed
    : reversed;
};
console.log(reverse(1234567893));
				
			

Submission Detail

1032 / 1032 test cases passed.
Status: 

Accepted

Runtime: 70 ms
Memory Usage: 45.2 MB
Submitted: 3 months ago

https://leetcode.com/problems/reverse-words-in-a-string/

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

Example 1:

Input: s = "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Constraints:

  • 1 <= s.length <= 104
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • There is at least one word in s.

Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

				
					let s = "the   sky is blue";




function reverseWords(s: string): string {
  let currentWord = "";
  let result = "";
  for (let i = 0; i < s.length; i++) {
    const char = s[i];
    if (char !== " ") {
      currentWord += char;
    }
    if (currentWord.length > 0 && (char === " " || i === s.length - 1)) {
      result = result === "" ? currentWord + result : currentWord + " " + result;
      currentWord = "";
    }
  }
  return result;
}




console.log(reverseWords(s));
				
			

Submission Detail

58 / 58 test cases passed.
Status: 

Accepted

Runtime: 113 ms
Memory Usage: 44.9 MB
Submitted: 3 months ago

https://leetcode.com/problems/length-of-last-word/

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

 

Example 1:

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

Example 2:

Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is "moon" with length 4.

Example 3:

Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

 

Constraints:

  • 1 <= s.length <= 104
  • s consists of only English letters and spaces ' '.
  • There will be at least one word in s.
				
					function lengthOfLastWord(s: string) {
  let finalCount = 0;
  let k = s.length - 1;
  if (s[k] === " ") {
    k--;
    while (s[k] === " ") {
      k--;
    }
  }
  for (let i = k; i >= 0; i--) {
    if (s[i] !== " ") finalCount++;
    else return finalCount;
  }
  return finalCount;
}
				
			

Submission Detail

58 / 58 test cases passed.
Status: 

Accepted

Runtime: 53 ms
Memory Usage: 43.4 MB
Submitted: 3 months ago

Leave a Comment