2.题目二
给定一个整数数组A,长度为n,有 1 <= A[i] <= n,且对于[1,n]的整数,其
中部分整数会重复出现而部分不会出现。
实现算法找到[1,n]中所有未出现在A中的整数。
提示:尝试实现O(n)的时间复杂度和O(1)的空间复杂度(返回值不计入空间复
杂度)。
输入描述:
一行数字,全部为整数,空格分隔
A0 A1 A2 A3…
输出描述:
一行数字,全部为整数,空格分隔R0 R1 R2 R3…
示例1:
输入
1 3 4 3
输出
2
// 请保证arr[0..N-1]上的数字都在[1~n]之间
public static void printNumberNoInArray(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
for (int i = 0; i < arr.length; i++) {//争取做大,i位置上,放的数是i+1
modify(arr[i], arr);
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] != i + 1) {//没做到的位置,就知道是缺的数字
System.out.println(i + 1);
}
}
}
public static void modify(int value, int[] arr) {
while (arr[value - 1] != value) {
int tmp = arr[value - 1];
arr[value - 1] = value;
value = tmp;
}
}
3.小Q得到一个神奇的数列: 1, 12, 123,…12345678910,1234567891011…。
并且小Q对于能否被3整除这个性质很感兴趣。
小Q现在希望你能帮他计算一下从数列的第l个到第r个(包含端点)有多少个数可以被3整除。
输入描述:
输入包括两个整数l和r(1 <= l <= r <= 1e9), 表示要求解的区间两端。
输出描述:
输出一个整数, 表示区间内能被3整除的数字个数。
示例1:
输入
2 5
输出
3
解题思路:判断一个数能不能被3整除,等价于一个数的每位之和能否被3整除。刚开始想打表,但发现数据量是1e9,一维数组最多只能开到1e8.所以就纯暴力判断了,不过数据是有规律的,第一个数是1、第二个数是12,第三个数是123,所以只用判断n*(n+1)/2%3即可。因为数量太大了,所以用long
public static int getNum(int l, int r) {
int sum=0;
for(int i=l;i<=r;i++){
long temp=(long)(i*(i+1))/2;
if(temp%3==0){
sum++;
}
}
return sum;
}
public static void main(String[] args) {
System.out.println(getNum(3, 6));
}
4.CC里面有一个土豪很喜欢一位女直播Kiki唱歌,平时就经常给她点赞、送礼、私聊。最近CC直播平台在举行
中秋之星主播唱歌比赛,假设一开始该女主播的初始人气值为start, 能够晋升下一轮人气需要刚好达到end,
土豪给主播增加人气的可以采取的方法有:
a. 点赞 花费x C币,人气 + 2
b. 送礼 花费y C币,人气 * 2
c. 私聊 花费z C币,人气 - 2
其中 end 远大于start且end为偶数, 请写一个程序帮助土豪计算一下,最少花费多少C币就能帮助该主播
Kiki将人气刚好达到end,从而能够晋级下一轮?
输入描述:
第一行输入5个数据,分别为:x y z start end,每项数据以空格分开。
其中:0<x, y, z<=10000, 0<start, end<=1000000
输出描述:
需要花费的最少C币。
示例1:
输入
3 100 1 2 6
输出
6
(1)递归方式
//start偶数 end偶数 start<=end
public static int minCcoins3(int add, int times, int del, int start, int end) {
if(start>end){
return -1;
}
int limitPopularity=2*end;
int limitCoin=((end-start)/2)*add;
return dfs(0,start,add,times,del,end,limitPopularity,limitCoin);
}
/**
*
* @param preMoney 之前已经花了多少钱 可变
* @param cur 当前来到的人气 可变
* @param add 点赞花费
* @param times 送礼花费
* @param del 私聊花费
* @param aim 目标人气
* @param limitPopularity 人气大到什么程度就不需要再尝试了 固定
* @param limitCoin 已经使用的币大到什么程度就不需要再尝试了 固定
* @return 返回最小币数
*/
public static int dfs(int preMoney,int cur,int add,int times,int del,int aim,int limitPopularity, int limitCoin){
//之前已经花的钱大于了最大钱数
if(preMoney>limitCoin){
return Integer.MAX_VALUE;
}
//人气值小于0
if(cur<0){
return Integer.MAX_VALUE;
}
//当前人气值超过了目标人气值的2倍
if(cur>limitPopularity){
return Integer.MAX_VALUE;
}
//当前人气值等于目标人气值
if(cur==aim){
return preMoney;
}
int min=Integer.MAX_VALUE;
int p1=dfs(preMoney+add,cur+2,add,times,del,aim,limitPopularity,limitCoin);
int p2=dfs(preMoney+times,cur*2,add,times,del,aim,limitPopularity,limitCoin);
int p3=dfs(preMoney+del,cur-2,add,times,del,aim,limitPopularity,limitCoin);
return Math.min(Math.min(p1,p2),p3);
}
(2)动态规划(没写出来)
5.求完全二叉树节点的个数
(1)普通递归遍历
```java
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
int count=dps(root);
return count;
}
public static int dps(TreeNode root){
if(root==null){
return 0;
}
int left=dps(root.left);
int right=dps(root.right);
return left+right+1;
}
(2)左神递归方法
public static int nodeNum(Node head) {
if (head == null) {
return 0;
}
return bs(head, 1, mostLeftLevel(head, 1));
}
//node在第l层,h是总深度(h永远不变,全局变量)
//以node为头的完全二叉树,节点个数是多少
public static int bs(Node node, int l, int h) {
if (l == h) {
return 1;
}
if (mostLeftLevel(node.right, l + 1) == h) {
return (1 << (h - l)) + bs(node.right, l + 1, h);//向左移相当于2的多少次方
} else {
return (1 << (h - l - 1)) + bs(node.left, l + 1, h);
}
}
//如果node在第l层
//求以node为头的子树,最大深度是多少
//node为头的子树,一定是完全二叉树
public static int mostLeftLevel(Node node, int level) {//注意这里要把lever传进来
while (node != null) {
level++;//是在lever上++
node = node.left;
}
return level - 1;
}
6.(leetcode300)最长递增子序列问题
public int lengthOfLIS(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
if(nums.length==1){
return 1;
}
int dp[] = new int[nums.length];
dp[0]=1;
int max=1;//保存dp数组中最大的数
for(int i=1;i<nums.length;i++){
dp[i]=1; //先默认赋值为1
for(int j=0;j<i;j++){
if(nums[j]<nums[i]){ //找到比i位置数小的位置j
dp[i]=Math.max(dp[j]+1,dp[i]); //原先想的是直接找到比i位置小且相差最小的数,但这样不好实现,这样一个一个比较最好实现
}
}
max=Math.max(max,dp[i]);
}
return max;
}