【刷题笔记】第六天

文章目录

今天再练一道迭代搜索的题 + 力扣每日一题

Power Calculus

题目描述

给你一个整数n,可以乘或者除,可以使用中间结果,求最少运算多少次可以得到 x n x^n xn

分析

相当于求从整数1开始,可以加或者减,可以使用中间结果,最少运算多少次可以得到n

实现细节见代码

c 复制代码
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;

const int N = 1010; 
int n;
int ans[N]; // 保存中间结果,n最大为1000,每次运算加1,也就需要1000次,所以最多运算1000次,也就是说中间结果最多有1000个
int pos; // 标记当前运算的位置
int depth; // 最大深度

// dep:当前递归深度
bool dfs(int dep) {
	if (dep > depth) return false;
	if (ans[pos] << (depth - dep) < n) {
		// depth - dep是还剩的递归次数
        // 估价函数:用最快的倍增都不能到达n,退出
        // 最快的倍增:假设ans[pos] = m, 下一层递归2m,再下一层4m, 8m 。。。 ,以2的幂倍增
		return false;
	}
	if (ans[pos] == n) {
		return true;
	}
	pos++;
	for (int i = 0; i < pos; ++i) {
		// 讨论上一个中间结果与前面所有的中间结果相加
		ans[pos] = ans[pos - 1] + ans[i];
		if (dfs(dep + 1)) {
			// 继续下一层的递归
			return true;
		}
		// 讨论上一个中间结果与前面所有的中间结果相减
		ans[pos] = ans[pos - 1] - ans[i];
		if (dfs(dep + 1)) {
			return true;
		}
	}
	pos--; // 回溯
	return false;
}

int main() {
	while (scanf("%d", &n)) {
		if (n == 0) break;
		depth = 0;
		pos = 0;
		ans[pos] = 1;
		while (!dfs(0)) {
			depth++;
		}
		printf("%d\n", depth);
	}
	return 0;
}

706. 设计哈希映射

方法一:纯数组
java 复制代码
class MyHashMap {
    private static final int MAX_N = 1000001;
    int[] map;
    public MyHashMap() {
        map = new int[MAX_N];
        Arrays.fill(map, -1);
    }
    
    public void put(int key, int value) {
        map[key] = value;
    }
    
    public int get(int key) {
        return map[key];
    }
    
    public void remove(int key) {
        map[key] = -1;

    }
}

/**
 * Your MyHashMap object will be instantiated and called as such:
 * MyHashMap obj = new MyHashMap();
 * obj.put(key,value);
 * int param_2 = obj.get(key);
 * obj.remove(key);
 */
方法二:数组加链表
java 复制代码
class MyHashMap {

    static class Node {
        int key, value;
        Node next;
        Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
    public Node[] table = new Node[(int)Math.pow(2, 14)];
    public MyHashMap() {

    }

    public int getIndex(int key) {
        int hash = Integer.hashCode(key);
        hash = hash ^ (hash >>> 16);
        return hash & (table.length - 1);
    }
    
    public void put(int key, int value) {
        int index = getIndex(key);
        if (table[index] == null) {
            table[index] = new Node(key, value);
        } else {
            Node cur = table[index];
            Node pre = null;
            while (cur != null) {
                if (cur.key == key) {
                    cur.value = value;
                    return;
                }
                pre = cur;
                cur = cur.next;
            }
            Node node = new Node(key, value);
            pre.next = node;
        }
    }
    
    public int get(int key) {
        int index = getIndex(key);
        if (table[index] != null) {
            Node cur = table[index];
            while (cur != null) {
                if (cur.key == key) {
                    return cur.value;
                }
                cur = cur.next;
            }
        }
        return -1;
    }
    
    public void remove(int key) {
        int index = getIndex(key);
        if (table[index] != null) {
            Node pre = null;
            Node cur = table[index];
            while (cur != null) {
                if (cur.key == key) {
                    if (pre == null) {
                        table[index] = cur.next;
                    } else {
                        pre.next = cur.next;
                    }
                    return;
                }
                pre = cur;
                cur = cur.next;
            }
        }
    }
}
相关推荐
找方案3 分钟前
hello-agents 学习笔记:解锁智能体三大经典范式,从原理到实战
javascript·笔记·学习·hello-agents
byzh_rc3 分钟前
[算法设计与分析-从入门到入土] 复杂算法
数据库·人工智能·算法·机器学习·支持向量机
Sunsets_Red9 分钟前
待修改莫队与普通莫队优化
java·c++·python·学习·算法·数学建模·c#
QT 小鲜肉14 分钟前
【Linux命令大全】001.文件管理之lsattr命令(实操篇)
linux·运维·服务器·笔记·elasticsearch
星火开发设计23 分钟前
深度优先搜索(DFS)详解及C++实现
c++·学习·算法·计算机·深度优先·大学生·期末考试
一抹轻笑动人24 分钟前
Viger笔记
笔记·golang
week_泽24 分钟前
OpenCV图像拼接原理与实践笔记
人工智能·笔记·opencv
iconball29 分钟前
个人用云计算学习笔记 --32 Docker和docker swarm
运维·笔记·学习·docker·容器·云计算
KingRumn38 分钟前
玩转DBus命令行工具之gdbus使用
linux·算法
二狗哈40 分钟前
Cesium快速入门34:3dTile高级样式设置
前端·javascript·算法·3d·webgl·cesium·地图可视化