参考《leetcode 题解,记录自己的 leetcode 解题之路》练脑 (一)

Problem:number-of-1-bits

https://leetcode.com/problems/number-of-1-bits/description/

Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).

 

Example 1:

Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
Example 2:

Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:

Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
 

Note:

Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer -3.

My Answer below:

function hammingWeight(n) {
  let count = 0;
  while (n !== 0) {
    count += n & 1;
    n = n>>1;
  }

  return count;
}

// hammingWeight(3)
// =>2
// hammingWeight(4)
// =>1
// hammingWeight(11)
// =>3

Problem:sum-of-two-integers

https://leetcode.com/problems/sum-of-two-integers/description/

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example 1:

Input: a = 1, b = 2
Output: 3
Example 2:

Input: a = -2, b = 3
Output: 1

My Answer below:


// References from https://github.com/azl397985856/leetcode/blob/master/problems/371.sum-of-two-integers.md
var getTwoSum = function(a, b) {
    if (a === 0) return b;

    if (b === 0) return a;

    return getSum(a ^ b, (a & b) << 1);
};

// Extension: sum-of-n-integers
var getSum = function() {
    const args = [...arguments]
    return (
          args.reduce((sum, cur) => getTwoSum(sum, cur), 0)
};
// getSum(4)
// =>4

// getSum(4, 5)
// =>9

// getSum(1, 4, 5)
// =>10

// getSum(-1, 4, 5)
// =>8

Problem:longest-substring-without-repeating-characters

https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

My Answer below:

const lengthOfLongestSubstring = function(s) {
  let longestStrMapper = {}; // 记录已经出现过的charactor
  let res = 0; // 记录临时结果
  let start = 0; // 开始指针
  let end = 0; // 结束指针
  let char = '' // 临时存储每一轮用于数不重复字符个数的字符
  for(let c of s) {
    char = c
    end = start
    longestStrMapper = {}
    while(!longestStrMapper[char] && char) {
      longestStrMapper[char] = true
      char = s.charAt(++end)
    }
    if(end - start > res) {
      res = end - start
    }
    start++;
  }
  return res;
};

lengthOfLongestSubstring("bbbbb")
// => 1
lengthOfLongestSubstring("abcabcbb")
// => 3
lengthOfLongestSubstring("pwwkew")
// => 3

 知识点快速索引

层次遍历(BFS)(树的基本操作- 遍历) 投票算法  hashmap 存储已经访问过的值 使用栈来代替递归 在push的时候利用辅助栈(双栈) 因数分解 二进制表示、2的幂次方特点 位运算、异或运算、左移一位表示进位 carried变量来实现进位  滑动窗口 动态规划一 动态规划二  分治法 使用双指针 dummyHead简化操作  二分查找  回溯法

作者: 博主

Talk is cheap, show me the code!

发表评论

电子邮件地址不会被公开。

Captcha Code