Leetcode brush notes – simple introductory questions


Leetcode notes

Title Source:Leetcode

Split balanced string

In a balanced string, the number of ‘l’ and ‘R’ characters is the same.

Give you a balanced string s, please divide it into as many balanced strings as possible.

be careful: each split string must be a balanced string.

Returns the maximum number of balanced strings that can be split.

//Idea: when traversing s, use a variable d to maintain the difference between the number of L and R characters. When d = 0, it means that a balanced string is found and the answer is added by one
var balancedStringSplit = function(s) {
    let ans = 0, count = 0
   for(key of s) {
       key === 'L' ? ans++ : ans--
       if(ans === 0) count++
   return count

For more solutions, please refer to the problem “split balanced string | leetcode”

Unique occurrences

Give you an integer array arr, please help count the number of occurrences of each number in the array

If the number of occurrences of each number is unique, return true; Otherwise, false is returned

//Idea: count the number of characters, and then judge whether the number of words has the same item
var uniqueOccurrences = function(arr) {
  const obj = {}
  for(key of arr) !obj[key] ? obj[key]=1 : obj[key]++
  //If there are duplicates, new set() will be de duplicated and the size will inevitably change
  return Object.values(obj).length == new Set(Object.values(obj)).size

For more solutions, please see the question “unique occurrences | leetcode”

Sum of two numbers

Given an integer array nums and an integer target value target, please find the two integers whose sum is the target value target in the array and return their array subscripts

//Idea: first find the matching item and judge whether the matching item is in the map. If yes, return two value indexes. If not, return map.set
var twoSum = function(nums, target) {
  const map = new Map()
  for(i in nums) {
    if(map.has(target - nums[i])) return [map.get(target - nums[i]), i]
    map.set(nums[i], i)
  return []

//Chestnut: nums = [2,7,1] targer = 9
1. Num [i] = 2, the matching item is 9-2 = 7. At this time, map.has (7) is false, that is, map.set (2, 0) // 2 is the value and 0 is the index
2. Nums [i] = 7, the matching item is 9-7 = 2. At this time, map.has (2) is true, that is, there is a matching item. Return [0, 1] // map.get (2) to get the value 2 index

For more solutions, see the problem “sum of two numbers | leetcode”

Palindrome number

Give you an integer X. if x is a palindrome integer, return true; Otherwise, false is returned.

Palindromes are integers that are read in the same positive order (from left to right) and reverse order (from right to left). For example, 121 is palindrome, not 123.

//Basic solution: using string comparison
var isPalindrome = function(x) {
  //Negative numbers must not palindrome - 121 - > 121-
  if (x < 0) return false;
  //Single digit palindrome 2 - > 2
  if (x < 10) return true;
  //X is not 0, but the bit is 0, and there must be no palindrome 120 - > 021
  if (x != 0 && x % 10 === 0) return false;
  return x.toString() === x.toString().split('').reverse().join('')

For more solutions, see the title “palindromes | leetcode”

Integer inversion

Give you a 32-bit signed integer x and return the result after reversing the number part in X.

If the inverted integer exceeds the range of 32-bit signed integers [2 ^ 31, 2 ^ 31-1], it returns 0.

// 2^31-1=2147483647,-2^31=-2147483648
//Idea: using array inversion, negative numbers plus negative signs return, and positive numbers return directly
var reverse = function(x) {
  let rsNum = parseInt(String(x).split('').reverse().join(''))
  if(Math.pow(-2, 31) <= rsNum && rsNum <= (Math.pow(2, 31) - 1)) return x < 0 ? -rsNum : rsNum
  return 0

For more solutions, see the problem “integer inversion | leetcode”

Roman numeral to integer

Roman numerals contain the following seven characters: I, V, x, l, C, D and m.

Character value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, the Roman numeral 2 is written as II, which is two parallel ones. 12 is written as XII, which is x + II. 27 is written as XXVII, which is XX + V + II.

Usually, the small Roman numerals are to the right of the large ones. But there are also special cases. For example, 4 is not written as IIII, but IV. The number 1 is on the left of the number 5, and the number represented is equal to the value 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is represented as IX.

This special rule applies only to the following six cases:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.

X can be placed to the left of L (50) and C (100) to represent 40 and 90.

C can be placed to the left of D (500) and m (1000) to represent 400 and 900.

Given a Roman numeral, convert it to an integer. Make sure the input is in the range of 1 to 3999

//Solution: first map symbols and values, and then the string from left to right. If the value represented by the current character is greater than the value on its right, add the value; Otherwise, subtract the value. By analogy to the rightmost number, the final result is the answer.

 * @param {string} s
 * @return {number}
function formatRoman(char) {
  let value = null
  switch(char) {
    case 'I': 
      value = 1
    case 'V': 
      value = 5
    case 'X': 
      value = 10
    case 'L': 
      value = 50
    case 'C': 
      value = 100
    case 'D': 
      value = 500
    case 'M': 
      value = 1000
  return value
var romanToInt = function(s) {
  let count = 0
  for(let i = 0; i <= s.length - 1; i++) {
      //String. Charat ([index]) returns the character at the specified position
      let curCharValue = formatRoman(s.charAt(i))
      let nextCharValue = formatRoman(s.charAt(i+1))
      curCharValue < nextCharValue ? count -= curCharValue : count += curCharValue
  return count

For more solutions, see the problem “Roman numeral to integer | leetcode”

To be continued

Recommended Today

Game case | application evolution and practice of service mesh in happy games

author Chen Zhiwei, Tencent level 12 background expert engineer, is now responsible for the public background technology research and development and team management of happy game studio. Rich experience in micro service distributed architecture and game background operation and maintenance research and development. preface The background of happy game studio is a distributed micro service […]