随笔分类 - leetcode
摘要:Counting Bits Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary re
阅读全文
摘要:Word Pattern Given a pattern and a string s, find if s follows the same pattern. Here follow means a full match, such that there is a bijection betwee
阅读全文
摘要:Add Digits Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. Example 1: Input: num = 38 Output:
阅读全文
摘要:somorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replac
阅读全文
摘要:Reverse String II Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If
阅读全文
摘要:Base 7 Given an integer num, return a string of its base 7 representation. Example 1: Input: num = 100 Output: "202" Example 2: Input: num = -7 Output
阅读全文
摘要:Fibonacci Number The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the
阅读全文
摘要:Ransom Note Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false other
阅读全文
摘要:Sum of Left Leaves Given the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that i
阅读全文
摘要:Assign Cookies Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each
阅读全文
摘要:Fizz Buzz Given an integer n, return a string array answer (1-indexed) where: answer[i] == "FizzBuzz" if i is divisible by 3 and 5. answer[i] == "Fizz
阅读全文
摘要:Valid Anagram Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearrangin
阅读全文
摘要:Remove Linked List Elements Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and
阅读全文
摘要:Move Zeroes 思路一: 用 left 指针标记求解数组最大下标 + 1,初始化的时候是 0,随着往右遍历,left 会一步步扩大。最后把 left 右边的数都置为 0。 这题的关键在于 left 永远指向求解数组最大下标 + 1 public void moveZeroes(int[] n
阅读全文
摘要:Power Of Two 思路一: 观察 2 的 n 次方的二进制,都只有一位 1 bit,遍历即可 public boolean isPowerOfTwo(int n) { if (n <= 0) return false; int count = 0; for (int i = 0; i < 3
阅读全文
摘要:Majority Element 思路一: map public int majorityElement(int[] nums) { if (nums.length == 1) return nums[0]; Map<Integer, Integer> map = new HashMap<>();
阅读全文
摘要:Reverse Bits 思路一: 遍历 32 位 bit,记录 bit 结果 public int reverseBits(int n) { int result = 0; int x = 32; while (x-- > 0) { int bit = n & 1; result <<= 1; i
阅读全文
摘要:Long Pressed Name 思路一: 暴力,遍历两个字符串,对比。边界情况不太好处理 public boolean isLongPressedName(String name, String typed) { if (typed.length() < name.length()) retur
阅读全文
摘要:X of a Kind in a Deck of Cards 思路一: 统计数字个数,然后用 [2, length] 的约数尝试所有数字个数,判断能否整除 public boolean hasGroupsSizeX(int[] deck) { Map<Integer, Integer> map =
阅读全文
摘要:Monotonic Array 思路一: 遍历 public boolean isMonotonic(int[] nums) { boolean increase = true; for (int i = 0; i < nums.length - 1; i++) { if (nums[i] > nu
阅读全文

浙公网安备 33010602011771号