一、力扣
1、第 N 位数字
400. 第 N 位数字

class Solution {
public int findNthDigit(int n) {
int digit = 1;
long start = 1;
long count = 9;
while (n > count) {
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n - 1) / digit;
String ans = Long.toString(num);
return ans.charAt((n - 1) % digit) - '0';
}
}
2、36进制加法
36进制加法_牛客题霸_牛客网

import java.util.*;
public class Solution {
public String thirtysixAdd (String A, String B) {
int pre = 0;
StringBuilder ans = new StringBuilder();
int x = A.length() - 1, y = B.length() - 1;
while (x >= 0 || y >= 0 || pre > 0) {
int fir = 0;
if (x >= 0) {
char temp1 = A.charAt(x);
if (Character.isDigit(temp1)) {
fir = temp1 - '0';
} else {
fir = temp1 - 'a' + 10;
}
x--;
}
int sec = 0;
if (y >= 0) {
char temp1 = B.charAt(y);
if (Character.isDigit(temp1)) {
sec = temp1 - '0';
} else {
sec = temp1 - 'a' + 10;
}
y--;
}
pre = pre + fir + sec;
int target = pre % 36;
pre = pre / 36;
if (target < 10) {
ans.append(target + "");
} else {
char temp = (char)(target - 10 + 'a');
ans.append(temp);
}
}
return ans.reverse().toString();
}
}
3、加油站
134. 加油站

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int curSum = 0;
int totalSum = 0;
int res = 0;
for (int i = 0; i < n; i++) {
curSum += gas[i] - cost[i];
totalSum += gas[i] - cost[i];
if (curSum < 0) {
curSum = 0;
res = (i + 1) % n;
}
}
return totalSum >= 0 ? res : -1;
}
}