10 2022 档案
摘要:Sort Integers by The Number Of 1 Bits You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in th
阅读全文
摘要:Keyboard Row Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard li
阅读全文
摘要:First Bad Version You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product f
阅读全文
摘要:Missing Number Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the arr
阅读全文
摘要:Number Of 1 Bits Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). Note: N
阅读全文
摘要:Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that
阅读全文
摘要:Calculate Money in Leetcode Bank Hercy wants to save money for his first car. He puts money in the Leetcode bank every day. He starts by putting in $1
阅读全文
摘要:Add to Array-Form of Integer The array-form of an integer num is an array representing its digits in left to right order. For example, for num = 1321,
阅读全文
摘要:Check if One String Swap Can Make Strings Equal You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose tw
阅读全文
摘要:Minimum Sum of Four Digit Number After Splitting Digits You are given a positive integer num consisting of exactly four digits. Split num into two new
阅读全文
摘要:Sum of Unique Elements You are given an integer array nums. The unique elements of an array are the elements that appear exactly once in the array. Re
阅读全文
摘要:in ing y 因姻殷音阴 银龈吟寅淫 引蚓隐瘾饮尹 印荫 英应鹰婴樱缨鹦 营莹萤盈迎赢 影 映硬应 b 宾滨缤彬 殡鬓 兵冰丙柄秉饼禀 病并 p 拼 贫频 品 聘 乒平苹萍屏瓶凭 m 民 敏皿闽悯泯 名茗铭明鸣冥命 d 丁叮钉仃叮 顶鼎定锭订 t 听厅汀 亭停廷庭蜒 挺艇 n 您 宁狞拧凝 拧
阅读全文
摘要:Largest Perimeter Triangle Given an integer array nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these le
阅读全文
摘要:Consecutive Characters The power of the string is the maximum length of a non-empty substring that contains only one unique character. Given a string
阅读全文
摘要:Valid Palindrome II Given a string s, return true if the s can be palindrome after deleting at most one character from it. Example 1: Input: s = "aba"
阅读全文
摘要:sz, rz 命令能在 ssh 连接的时候快速传输文件 sz (send 发送) rz (receive 接收) 使用这两个命令的前提是服务端安装了命令,没有安装的可以用下面的命令安装 yum install lrzsz 并且 ssh 连接工具也要支持,putty 本身是不支持的,但是我们可以下载
阅读全文
摘要:Occurrences After Bigram second third", where second comes immediately after first, and third comes immediately after second. Return an array of all t
阅读全文
摘要:Remove Palindromic Subsequences You are given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subse
阅读全文
摘要:Count Good Triplets Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets. A triplet (arr[i], ar
阅读全文
摘要:Binary Search Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If
阅读全文
摘要:Minimum Changes To Make Alternatign Binary String You are given a string s consisting only of the characters '0' and '1'. In one operation, you can ch
阅读全文
摘要:一个 Demo,记录一下发布公众号图文时需要用到的接口 公众号开发时需用到的一些网站 微信官方文档平台 ,开发公众号只用查看公众号那一块 微信公众平台接口测试帐号申请 ,申请公众号测试账号 微信公众平台接口调试工具 ,官方提供的接口测试工具 WxJava 仓库 , WxJava仓库地址 natapp
阅读全文
摘要:Minimum Hours of Training to Win a Competition You are entering a competition, and are given two positive integers initialEnergy and initialExperience
阅读全文
摘要:Minimum Index Sum of Two Lists Given two arrays of strings list1 and list2, find the common strings with the least index sum. A common string is a str
阅读全文
摘要:Number Complement The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representa
阅读全文
摘要:Decompress Run-length Encoded List We are given a list nums of integers representing a list compressed with run-length encoding. Consider each adjacen
阅读全文
摘要:NimGame You are playing the following Nim Game with your friend: Initially, there is a heap of stones on the table. You and your friend will alternate
阅读全文
摘要:Self Dividing Numbers A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number beca
阅读全文
摘要:Is Subsequence Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that i
阅读全文
摘要:Make Array Zero by Subtracting Equal Amounts You are given a non-negative integer array nums. In one operation, you must: Choose a positive integer x
阅读全文
摘要:Find Closest Number To Zero Given an integer array nums of size n, return the number with the value closest to 0 in nums. If there are multiple answer
阅读全文
摘要:Summary Ranges You are given a sorted unique integer array nums. A range [a,b] is the set of all integers from a to b (inclusive). Return the smallest
阅读全文
摘要: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
阅读全文
摘要:下面操作是在虚拟机环境下,根目录最好不要扩容或者缩小,一旦出问题很容易导致系统启动分区故障 查看磁盘信息fdisk -l Disk /dev/sda: 48 GiB, 51539607552 bytes, 100663296 sectors ... Device Boot Start End Sec
阅读全文
摘要: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
阅读全文
摘要:Binary Gap 思路一: 记录 bit 为 1 的位置,保留区间最大的值 public int binaryGap(int n) { int idx = -1; int result = 0; int i = 0; while (n > 0) { if ((n & 1) == 1) { if
阅读全文
摘要:Buddy Strings 思路一: 两个字符是 Buddy Strings 的完全分类 字符不一样 && 只存在两个位置交换的字符 字符一样 && 存在重复的字符 按照上面的分类,记录两个不同位置的下标和统计出现字符的情况 public boolean buddyStrings(String s,
阅读全文
摘要:Flipping an Image 思路一: 遍历数组,先对数组进行 flip 然后再对数组进行 invert public int[][] flipAndInvertImage(int[][] image) { int N = image.length; for (int i = 0; i < N
阅读全文
摘要:Teemo Attacking 思路一: 对两个前后攻击序列,完全分类只可能出现两种情况,只要把重叠的时间都减去,就是所求时间。此分类对三个以上的重叠时间依旧成立 没有重叠,攻击时间累加 有重叠,攻击时间需要去除重叠部分 public int findPoisonedDuration(int[] t
阅读全文
摘要:Hamming Distance 思路一: 求 bit 不同的位数,XOR 可以把 bit 不同的位置变成 1,再用 bitCount() 统计 public int hammingDistance(int x, int y) { return Integer.bitCount(x ^ y); }
阅读全文
摘要:思路一: 用数组记录 public List<Integer> findDisappearedNumbers(int[] nums) { int[] m = new int[nums.length]; for (int num : nums) { if (num-1 <= m.length) { m
阅读全文
摘要:问题: 想看看容器内 java 线程锁的状态,运行 jstack 1 提示 “Unable to get pid of LinuxThreads manager thread”,使用其他 jdk 命令也是如此 排查:原因是因为容器内的 java pid 为 1,导致内置的 jdk 命令无法使用,有个
阅读全文
摘要:Find the Difference 思路一: xor 两个字符串 public char findTheDifference(String s, String t) { char result = 0; for (int i = 0; i < s.length(); i++) { result
阅读全文
摘要:Invert Binary Tree 思路一:递归,交换左右。这题比较出名,个人感觉面试的题目和实际工作中遇到的问题还是不太一样的,所以一点准备都不做就跑去面试,答不上来很正常。 一般能力强的人过一遍资料就有大致的知识结构了 public TreeNode invertTree(TreeNode r
阅读全文
摘要:Contains Duplicate II 思路一: for 循环遍历,结果超时 public boolean containsNearbyDuplicate(int[] nums, int k) { int left = -1; for (int i = 0; i < nums.length; i
阅读全文
摘要:Contains Duplicate 思路一: Set 检测 public static boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for (int num : nums) { if (se
阅读全文
摘要:To Lower Case 思路一: 遍历,遇到 A-Z 范围内的 char 转换到对应 a-z 范围 public String toLowerCase(String s) { if (s == null || s.isEmpty()) return s; int gap = 'a' - 'A';
阅读全文
摘要:Add String 思路一: 模拟加法运算,字符串前面填零 public String addStrings(String num1, String num2) { int max = Math.max(num1.length(), num2.length()); num1 = pad(num1,
阅读全文
摘要:Reverse String 思路一: 首尾互换 public void reverseString(char[] s) { int mid = s.length / 2; int begin = 0; int end = s.length - 1; while (begin < mid) { ch
阅读全文
摘要:Ugly Number 思路一: 从数字中依次去除 2,3,5,查看剩余的值是否为 1 public boolean isUgly(int n) { if (n == 0) return false; while (n % 2 == 0) { n /= 2; } while (n % 3 == 0)
阅读全文
摘要:Happy Number 思路一: happy number 的结果完全分类,就两种情况 最后的值为 1 进入循环(用 map 记录) public boolean isHappy(int n) { Set<Integer> set = new HashSet<>(); while (true) {
阅读全文
摘要:Linked List Cycle 思路一: 用 set 记录每个节点的 hashCode,如果遇到重复,说明是循环 public boolean hasCycle(ListNode head) { Set<Integer> set = new HashSet<>(); while (head !=
阅读全文
摘要:Single Number 思路一: 用 set 过滤,剩下唯一一个就是目标数字 public int singleNumber(int[] nums) { Set<Integer> set = new HashSet<>(); for (int num : nums) { if (set.cont
阅读全文
摘要:Maximum Depth of Binary Tree 思路一: 递归 public int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDept
阅读全文
摘要:Symmetric Tree 思路一: 递归 public boolean isSymmetric(TreeNode left, TreeNode right) { if (left == null && right == null) return true; if (left == null ||
阅读全文
摘要:Same Tree public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p != null && q == null) return false; if (p
阅读全文
摘要:Binary Tree Inorder Traversal public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); t(root, list); return lis
阅读全文
摘要:Merge Sorted Array 思路一: 比较两个数组前面最小值,依次插入到新数组中,最后复制新数组到 num1 中 public void merge(int[] nums1, int m, int[] nums2, int n) { int[] result = new int[nums1
阅读全文
摘要:Remove Duplicates from Sorted List 思路一: 双指针,左指针记录链表最后有效位置,右指针向前扫描,遇到不重复的值,加到左指针后面,双指针依次向前 public ListNode deleteDuplicates(ListNode head) { if (head =
阅读全文
摘要:Climbing Stairs 思路一: 动态规划,假设爬上第 n 阶楼梯,完全分类只可能存在两种情况 在 n-1 楼梯处直接一步上来 在 n-2 楼梯处直接两步上来 所以 爬上第 n 阶楼梯的方法: f(n) = f(n-1) + f(n+1) public int climbStairs(int
阅读全文
摘要:Sqrt(x) 思路一: 暴力 public int mySqrt(int x) { long begin = 1L; while ((begin * begin) <= x) { begin++; } return Long.valueOf(begin).intValue() - 1; } 思路二
阅读全文
摘要:问题: docker-compose 启动 java 容器时报错 library initialization failed - unable to allocate file descriptor table - out of memoryPicked up JAVA_TOOL_OPTIONS:
阅读全文
摘要:Add Binary 思路一: 先计算公共部分,最后补充未计算的位置,模拟二进制加法,写的太丑了 public String addBinary(String a, String b) { char ONE = '0' + '1'; char TWO = '1' + '1'; StringBuild
阅读全文
摘要:Plus One 思路一: 暴力,方向想错了,不能把 digits 当做一个整数看 public int[] plusOne(int[] digits) { if (digits[digits.length - 1] != 9) { digits[digits.length - 1]++; retu
阅读全文
摘要:Length of Last Word 思路一: 从后面非空格字符开始扫描,记录非空格字符个数。优化:不用 char[],直接用 charAt() 判断 public int lengthOfLastWord(String s) { char[] chars = s.toCharArray(); i
阅读全文
摘要:Remove Duplicates from Sorted Array 思路一: 双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指针交换。 public int removeDuplicates(int[] nums) { i
阅读全文
摘要:一个简单的 java web 程序,运行在 docker 容器中,提供对外接口服务 现象:使用 docker run 启动容器后可以正常运行,访问一切正常,但是隔了几十分钟后容器自动退出,java 日志一切正常 排查: docker ps -a 查看挂掉的容器 id,docker logs 查看容器
阅读全文
摘要:Search Insert Position 思路一: 二分查找,然后处理没有找到的情况 public int searchInsert(int[] nums, int target) { int i = Arrays.binarySearch(nums, target); if (i >= 0)
阅读全文
摘要:Valid Parentheses 思路一: 用栈处理,括号匹配就出栈,如果最后栈不为空,说明输入的括号不匹配 public boolean isValid(String s) { char[] chars = s.toCharArray(); Deque<Character> stack = ne
阅读全文
摘要:Longest Common Prefix 思路一: 取第一个字符串为基准,依次对比剩余的字符串,取公共串 private static int commonPrefix(char[] arr1, char[] arr2) { int n = Math.min(arr1.length, arr2.l
阅读全文
摘要:remove element 思路一: 先排序,然后去除数字 public static int removeElement(int[] nums, int val) { Arrays.sort(nums); int INVALID = -1; int L = INVALID; for (int i
阅读全文
摘要:merge two sorted lists 思路一: 暴力求解,依次把两链表元素插入到新链表中 public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 == null && list2 == null) r
阅读全文
摘要:Roman to Integer 思路一: 暴力求解,遍历字符,遇到 6 种特殊的组合字符单独计算 static Map<Character, Integer> map = new HashMap<>(); static Map<String, Integer> combine = new Hash
阅读全文
摘要:回文数 思路一: 暴力求解,把数字一个一个拆分,放队列里面,最后取队列的首尾,对比是否相同 public static boolean isPalindrome2(int x) { if (x < 0) return false; if (x < 10) return true; Deque<Int
阅读全文
摘要:思路一:暴力求解 public int[] twoSum(int[] nums, int target) { for(int i = 0; i < nums.length; i++) { for(int j = i+1; j < nums.length; j++) { if (nums[i] + n
阅读全文
摘要:思路一:分别用两个 map 记录最大值和最小值,然后遍历这两个 map 求得最大距离 public int maxDistance(int[] colors) { Map<Integer, Integer> min = new HashMap<>(); Map<Integer, Integer> m
阅读全文