第一题
public class 第一题11 {
public static void main(String[] args) {
int res = strIndex("abc", 3);
System.out.println(res);
}
public static int strIndex(String str,int k){
if (str == null || str.length() == 0 || str.length() > k){
return -1;
}
char[] chars = str.toCharArray();
int ans = 0;
int n = str.length();
for (int i = 0,rest = k - 1;i < n;i++,rest--){
ans += (chars[i] - 'a') * f(rest) + 1;
}
return ans;
}
private static int f(int len) {
int ans = 1;
for (int i = 1,base = 26;i <= len;i++,base *= 26){
ans += base;
}
return ans;
}
}
第二题
思路:大根堆或小根堆
public class 第二题11 {
public static void main(String[] args) {
int[][] stones = {{0,4,7},{1,3,4},{0,7,6},{0,1,5}};
int res = minCost(stones);
System.out.println(res);
}
static class Node{
public int red;
public int blue;
public Node(int red,int blue){
this.red = red;
this.blue = blue;
}
}
public static int minCost(int[][] stones){
int n = stones.length;
if (n % 2 == 1){
return -1;
}
int red = 0;
int red_sum = 0;
int blue = 0;
int blue_sum = 0;
int stone_sum = 0;
PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
int del1 = o1.red - o1.blue;
int del2 = o2.red - o2.blue;
return del2 - del1;
}
});
for (int i = 0;i < n;i++){
if (stones[i][0] == 1){
red++;
}else if (stones[i][0] == 2){
blue++;
}else {
stone_sum++;
red_sum += stones[i][1];
queue.add(new Node(stones[i][1],stones[i][2]));
}
}
if (red > n / 2 || blue > n / 2){
return -1;
}
int adv_red = n / 2 - red;
int adv_blue = stone_sum - adv_red;
while (adv_blue > 0){
Node poll = queue.poll();
red_sum = red_sum + poll.blue - poll.red;
adv_blue--;
}
return red_sum;
}
}
第三题
public class 第三题11 {
public static void main(String[] args) {
int[] arr = {0,0,1,1,1,0,0,0};
int[] res = splitNums(arr);
System.out.println(res);
}
public static int[] splitNums(int[] arr){
HashMap<Integer,HashMap<Integer,Integer>> map = new HashMap<>();
int n = arr.length;
int zero = 0;
int one = 0;
int[] ans = new int[n];
for (int i = 0;i < n;i++){
if (arr[i] == 0){
zero++;
}else {
one++;
}
if (zero == 0 || one == 0){
ans[i] = i + 1;
continue;
}
int gcd = gcd(zero,one);
int zero_gcd = zero / gcd;
int one_gcd = one / gcd;
if (!map.containsKey(zero_gcd)){
map.put(zero_gcd,new HashMap<>());
}
if (!map.get(zero_gcd).containsKey(one_gcd)){
map.get(zero_gcd).put(one_gcd,0);
}
int count = map.get(zero_gcd).get(one_gcd) + 1;
map.get(zero_gcd).put(one_gcd,count);
ans[i] = count;
}
return ans;
}
public static int gcd (int a,int b) {
int c;
while (b!=0) {
c = a%b;
a = b;
b = c;
}
return a;
}
}
第四题
public class 第四题11 {
public static void main(String[] args) {
int[] arr = {2,3,1};
int res = pick(arr);
System.out.println(res);
}
public static int pick(int[] arr){
Arrays.sort(arr);
int ans = 0;
int n = arr.length;
for (int i = n - 1;i >= 0;i--){
ans = (ans << 1) + arr[i];
}
return ans;
}
}
第五题
思路:单调栈
public class 第五题11 {
public static void main(String[] args) {
String res = maxStringK("dbcabd", 3);
System.out.println(res);
}
public static String maxStringK(String str,int k){
int n = str.length();
char[] chars = str.toCharArray();
char[] stack = new char[n];
int index = 0;
for (int i = 0;i < n;i++){
while (index > 0 && stack[index - 1] < chars[i] && index + n - i > k){
index--;
}
if (index + n - i == k){
return String.valueOf(stack, 0, index) + str.substring(i);
}
stack[index++] = chars[i];
}
return String.valueOf(stack,0,k);
}
}
第六题
思路:递归
public class 第六题11 {
public static void main(String[] args) {
int res1 = maxPrice1("0010110110011011");
System.out.println(res1);
}
//暴力递归
public static int maxPrice1(String s){
if (s == null || s.length() < 1){
return -1;
}
char[] chars = s.toCharArray();
int n = s.length();
int[] arr = new int[n];
for (int i = 0;i < n;i++){
arr[i] = (chars[i] == '0' ? 0 : 1);
}
return process1(arr,0,0,0);
}
private static int process1(int[] arr, int index, int lastNum, int baseValue) {
if (index == arr.length){
return 0;
}
int curValue = (arr[index] == lastNum ? baseValue + 1 : 1);
int p1 = process1(arr,index + 1,arr[index],curValue);
int p2 = process1(arr,index + 1,lastNum,baseValue);
return Math.max(curValue + p1,p2);
}
}
第七题
思路:从左向右模型,范围尝试
public class 第七题11 {
public static void main(String[] args) {
int res = maxDisappear("1001231");
System.out.println(res);
}
public static int maxDisappear(String str){
if (str == null|| str.length() == 0){
return 0;
}
return process(str.toCharArray(),0,str.length() - 1);
}
private static int process(char[] str, int l, int r) {
if (l >= r){
return 0;
}
if (l == r - 1){
return (str[l] == '0' && str[r] == '1') || (str[l] == '2' && str[r] == '3') ? 2 : 0;
}
//不考虑 l 位置
int p1 = process(str,l + 1,r);
if (str[l] == '1' || str[l] == '3'){
return p1;
}
//l 位置只能是 0 或 2
int p2 = 0;
char find = str[l] == '0' ? '1' : '3';
for (int i = l + 1;i <= r;i++){
if (str[i] == find) {
p2 = Math.max(p2,2 + process(str,l + 1,i) + process(str,i + 1,r));
}
}
return Math.max(p1,p2);
}
}
第八题
思路:离散化 + 下标循环
public class 第八题11 {
public static void main(String[] args) {
int[] arr = {13,56,7,5,14,76,45,8,4,2,23};
int res = minSwapTimes(arr);
System.out.println(res);
}
public static int minSwapTimes(int[] arr){
//离散化
int n = arr.length;
HashMap<Integer, Integer> map1 = new HashMap<>();
HashMap<Integer, Integer> map2 = new HashMap<>();
int[] min_arr = new int[n];
int[] max_arr = new int[n];
int[] nums = Arrays.copyOf(arr, arr.length);
Arrays.sort(nums);
for (int i = 0;i < n;i++){
map1.put(nums[i],i);
}
for (int i = n - 1;i >= 0;i--){
map2.put(nums[i],n - i - 1);
}
for (int i = 0;i < n;i++){
min_arr[i] = map1.get(arr[i]);
max_arr[i] = map2.get(arr[i]);
}
int p1 = minSwap(min_arr);
int p2 = minSwap(max_arr);
return Math.min(p1,p2);
}
//下标循环
private static int minSwap(int[] arr) {
int n = arr.length;
int ans = 0;
for (int i = 0;i < n;i++){
while (arr[i] != i){
swap(arr,arr[i],i);
ans++;
}
}
return ans;
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
第九题
leetcode31. 下一个排列
public class 第九题11 {
public static void main(String[] args) {
int[] nums = {1,2,3};
nextPermutation(nums);
}
public static void nextPermutation(int[] nums) {
int n = nums.length;
int firstLess = -1;
for (int i = n - 1;i > 0;i--){
if (nums[i - 1] < nums[i]){
firstLess = i - 1;
break;
}
}
if (firstLess == -1){
reverse(nums,0,n - 1);
}else {
int secondLess = -1;
for (int i = firstLess + 1;i < n;i++){
if (nums[i] > nums[firstLess]){
secondLess = i;
}else {
break;
}
}
swap(nums,firstLess,secondLess);
reverse(nums,firstLess + 1,n - 1);
}
}
private static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private static void reverse(int[] nums, int start, int end) {
while (start < end){
swap(nums,start++,end--);
}
}
}