It's the Xinrui Ma

Blog

Tag: JavaScript

Remove duplicate char in TypeScript

Posted by in LeetCode on

Question: How will you remove duplicate characters from a sting?

You: This is very similar to first non repeating char. You will asks similar question. Is it case sensitive.

387. First Unique Character in a String

Posted by in LeetCode on

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = “leetcode”
return 0.

s = “loveleetcode”,
return 2.

LeeCode: Summary Ranges answer in JavaScript and Java

Posted by in LeetCode on

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return [“0->2″,”4->5″,”7”].

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Valid Palindrome answer in JavaScript and Java

Posted by in LeetCode on

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

Note: the O(N) is not accepted through LeeCode

Solution in Java:

Solution in JavaScript

Invert a binary tree in Java and JavaScript

Posted by in LeetCode on

to

Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

LeeCode: contains any duplicates in Java and JavaScript

Posted by in LeetCode on

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Soluton in JavaScript:

Solution in Java Using HashTable:

The length of last word in the string in Java and JavaScript

Posted by in LeetCode on

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given s = “Hello World”,
return 5.

SOLUTION IN JAVASCRIPT:

Longest Common Prefix

Posted by in LeetCode on

Write a function to find the longest common prefix string amongst an array of strings.

本题的意思是求所有字符串的最长公共前缀,即数组的所有字符串都包含这个前缀.

Example: {“aa”, “aaab”,”aac”,”aaaaaf”}
Return : “aa”

Solution in Java:

Solution in JavaScript:

Balanced Binary Tree in JavaScript and Java

Posted by in LeetCode on

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Solution in JavaScript:

Java Solution:

Count and Say Answer in Java and JavaScript

Posted by in LeetCode on

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, …

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

JavaScript Solution: