other
Analog multiplication
Long cannot be used directly, the problem is that there will be overflow.
Can only simulate multiplication manually
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
String ans = "0";
int m = num1.length(), n = num2.length();
for (int i = n - 1; i >= 0; i--) {
StringBuffer curr = new StringBuffer();
int add = 0;
for (int j = n - 1; j > i; j--) {
curr.append(0); // 模拟× 10 的场景
}
int y = num2.charAt(i) - '0';
for (int j = m - 1; j >= 0; j--) {
int x = num1.charAt(j) - '0';
int product = x * y + add;
curr.append(product % 10);
add = product / 10;
}
if (add != 0) {
curr.append(add % 10);
}
ans = addStrings(ans, curr.reverse().toString());
}
return ans;
}
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1, add = 0;
StringBuffer ans = new StringBuffer();
while (i >= 0 || j >= 0 || add != 0) {
// add直接在这里处理
int x = i >= 0 ? num1.charAt(i) - '0' : 0; // 防止越位以及最后判断谁越位
int y = j >= 0 ? num2.charAt(j) - '0' : 0;
int result = x + y + add;
ans.append(result % 10); // 本次添加余数
add = result / 10; // 留给下次的是本次的结果
i--;
j--;
}
ans.reverse();
return ans.toString();
}
}
More straightforward:
BigDecimal n1 = new BigDecimal(num1);
BigDecimal n2 = new BigDecimal(num2);
BigDecimal res = n1.multiply(n2);
return res.toPlainString();
149. The tree with the most points on a straight line
Y=kx + b to
find all kb, two points determine a kb, the traversal of the square of n
and then enumerate which coordinates satisfy the calculation formula to
find the largest number
Analog power
https://leetcode-cn.com/problems/powx-n/
class Solution {
public double myPow(double x, int n) {
long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
public double quickMul(double x, long N) {
if (N == 0) {
return 1.0;
}
double y = quickMul(x, N / 2);
return N % 2 == 0 ? y * y : y * y * x;
}
}
In-place hash
https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/
Problem-solving ideas If
you do not use additional memory space, you can only modify the array elements in place to mark whether you have accessed the
principle: if It is the same element, so the value of the element indexed by them must be the same value, so the value can be modified to mark whether it has been visited.
Note: If you need to modify the element in place, it cannot affect its own access as an index, then There is only one way, and that is to reverse the element, or add or subtract a certain number, and then restore it by taking positive or adding or subtracting a certain number when visiting.
Use the relationship between index and a[index] to
take the element value n, and then negate a[abs(n)].
If you find that a[abs(n)] is negative next time, it means you have visited before. Join the result set directly.
Bit operation
A bit similar to the idea of hash, using bits to represent the state
The topic of seeking subsets:
https://leetcode-cn.com/problems/subsets/solution/zi-ji-by-leetcode-solution/
Core ideas:
- Brute force search
- Use bits to indicate whether the array at the corresponding position is enabled
Core code:
for (int mask = 0; mask < (1 << n); ++mask) {
t.clear();
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) != 0) {
t.add(nums[i]);
}
}
ans.add(new ArrayList<Integer>(t));
}
Code:
class Solution {
List<Integer> t = new ArrayList<Integer>();
List<List<Integer>> ans = new ArrayList<List<Integer>>();
public List<List<Integer>> subsets(int[] nums) {
int n = nums.length;
for (int mask = 0; mask < (1 << n); ++mask) {
t.clear();
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) != 0) {
t.add(nums[i]);
}
}
ans.add(new ArrayList<Integer>(t));
}
return ans;
}
}
Violent solution: the solution of small problems constitutes the solution of big problems
New elements added to the existing set become a new set and then thrown into it.
191. The number of bits 1
https://leetcode-cn.com/problems/number-of-1-bits/
Key points:
- Within 32 bits, that is, the operation of <<1 is 32 times at most
https://leetcode-cn.com/problems/number-of-1-bits/solution/wei-1de-ge-shu-by-leetcode/
public int hammingWeight(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
// 最多32次
if ((n & mask) != 0) {
bits++; // 为1的位数
}
mask <<= 1;
}
return bits;
}
The AND operator is represented by the symbol "&", and its usage rules are as follows: the
two operands are both 1, and the result is 1, otherwise the result is 0, such as the following program segment.
129&128 = 128. The value of "a" is 129, converted to binary is 10000001, and the value of "b" is 128, converted to binary is 10000000. According to the operation rule of the AND operator, only two bits are 1, the result is 1, and it can be known that the result is 10000000, which is 128.
Sort related records
- Task Scheduler: The shortest time to execute the command with cooling time. 621
https://leetcode-cn.com/problems/task-scheduler/
Solution 1: Sort. Sorting is to confirm the priority, and the element with the largest value is eliminated first in each round. - Merge public intervals: https://leetcode-cn.com/problems/merge-intervals/solution/zhen-dui-xin-shou-jian-ji-yi-dong-de-ti-jie-liang-/sort
and compare After the merger.
Another solution: mark the boolean array. This solution cannot handle a situation: [1,4][5,6], which is not counted as a common interval. - Two ordered custom linked lists merge: https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by- leetcode/
- Double pointer method for in-place sorting: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/submissions/