5-2 Hash和队列算法题

栈实现队列 LeetCode232

  • 保存一个输出栈和一个输入栈,push(x) 时将值 x 压入输入栈中;调用 pop()、peek() 时若输出栈为空,则将输入栈的值依次压入输出栈,返回值为输出栈顶元素值(peek() 注意重新压入输出栈);调用 empty() 时输入栈、输出栈均为空时为真
var MyQueue = function() {
    this.in = new Array();
    this.out = new Array();
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.in.push(x);
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(this.out.length === 0) {
        while(this.in.length > 0) {
            this.out.push(this.in.pop())
        }
    }
    return this.out.pop();
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if(this.out.length === 0) {
        while(this.in.length > 0) {
            this.out.push(this.in.pop())
        }
    }
    let top = this.out.pop();
    this.out.push(top)
    return top
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return this.in.length === 0 && this.out.length === 0;
};

队列实现栈 LeetCode225

  • 保存一个栈队列和一个辅助队列,push() 时插入辅助队列并将栈队列的值依次出队并插入辅助队列中,最后交换辅助队列和栈队列;栈队列队头、队尾分别对应栈顶、栈底
var MyStack = function() {
    this.stack = new Array()
    this.help = new Array()
};

/** 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function(x) {
    this.help.push(x)
    while(this.stack.length > 0) {
        this.help.push(this.stack.shift())
    }
    let temp = this.stack
    this.stack = this.help
    this.help = temp
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function() {
    return this.stack.shift()
};

/**
 * @return {number}
 */
MyStack.prototype.top = function() {
    return this.stack[0]
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return this.stack.length === 0
};

n 数之和

  • 两数之和 LeetCode1

    • 哈希
    var twoSum = function(nums, target) {
        let map = new Map()
        let find
        for(let i = 0; i < nums.length; ++i) {
            find = target - nums[i]
            if(map.has(find)) {
                return new Array(map.get(find), i)
            }
            map.set(nums[i], i)
        }
    };
    
  • 三数之和 LeetCode15

    • 排序 + 固定首个数字 + 双指针(左右指针之后大于目标值时右指针左移,小于目标值时左指针右移,等于目标值时右指针左移 + 左指针右移),注意跳过重复值
    var threeSum = function(nums) {
        nums.sort((a, b) => a - b)
        let pre
        let res = new Array()
        let length = nums.length
        for(let i = 0; i < length - 2; ++i) {
            if(nums[i] === pre) {
                continue
            } else {
                pre = nums[i]
            }
            let left = i + 1, right = length - 1
            let target = 0 - nums[i]
            while(left < right) {
                let cur = nums[left] + nums[right]
                if(cur > target) {
                    --right
                }
                else if(cur < target) {
                    ++left
                } else {
                    res.push(new Array(nums[i], nums[left], nums[right]))
                    let prel = nums[left], prer = nums[right]
                    while(left < right && prel === nums[left]) {
                        ++left
                    }
                    while(left < right && prer === nums[right]) {
                        --right
                    }
                }
            }
        }
        return res
    };
    
  • 四数之和 LeetCode

    • 在三数之和基础上嵌套一层 for 循环
    var fourSum = function(nums, target) {
        nums.sort((a, b) => a - b)
        let pre1
        let res = new Array()
        let length = nums.length
        for(let i = 0; i < length - 3; ++i) {
            let newTarget = target - nums[i]
            if(nums[i] === pre1) {
                continue
            } else {
                pre1 = nums[i]
            }
            let pre2
            for(let j = i + 1; j < length - 2; ++j) {
                if(nums[j] === pre2) {
                    continue
                } else {
                    pre2 = nums[j]
                }
                let left = j + 1, right = length - 1
                let newNewTarget = newTarget - nums[j]
                while(left < right) {
                    let sum = nums[left] + nums[right]
                    if(sum > newNewTarget) {
                        --right
                    }
                    else if(sum < newNewTarget) {
                        ++left
                    } else {
                        res.push(new Array(nums[j], nums[i], nums[left], nums[right]))
                        let prel = nums[left], prer = nums[right]
                        while(left < right && prel === nums[left]) {
                            ++left
                        }
                        while(left < right && prer === nums[right]) {
                            --right
                        }
                    }
                }
            }
        }
        return res
    };
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在 C 语言中,可以使用类似 Python 中的哈希表实现来删除队列中的重复元素。以下是一个简单的示例: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SIZE 100 struct queue { int data[MAX_SIZE]; int front, rear; }; struct hash_table { int *keys; bool *values; int size; }; struct queue *create_queue() { struct queue *q = malloc(sizeof(struct queue)); q->front = -1; q->rear = -1; return q; } struct hash_table *create_hash_table(int size) { struct hash_table *table = malloc(sizeof(struct hash_table)); table->keys = malloc(size * sizeof(int)); table->values = malloc(size * sizeof(bool)); table->size = size; for (int i = 0; i < size; i++) { table->keys[i] = 0; table->values[i] = false; } return table; } void enqueue(struct queue *q, int value) { if (q->rear == MAX_SIZE - 1) { printf("Queue overflow\n"); return; } if (q->front == -1) { q->front = 0; } q->rear++; q->data[q->rear] = value; } int dequeue(struct queue *q) { if (q->front == -1 || q->front > q->rear) { printf("Queue underflow\n"); return -1; } int value = q->data[q->front]; q->front++; return value; } bool is_empty(struct queue *q) { return q->front == -1 || q->front > q->rear; } bool is_full(struct queue *q) { return q->rear == MAX_SIZE - 1; } void delete_queue(struct queue *q) { free(q); } int hash(int key, int size) { return key % size; } void insert(struct hash_table *table, int key) { int index = hash(key, table->size); while (table->values[index] && table->keys[index] != key) { index = hash(index + 1, table->size); } table->keys[index] = key; table->values[index] = true; } bool search(struct hash_table *table, int key) { int index = hash(key, table->size); while (table->values[index]) { if (table->keys[index] == key) { return true; } index = hash(index + 1, table->size); } return false; } void remove_duplicates(struct queue *q) { struct hash_table *table = create_hash_table(MAX_SIZE); struct queue *new_q = create_queue(); while (!is_empty(q)) { int value = dequeue(q); if (!search(table, value)) { insert(table, value); enqueue(new_q, value); } } while (!is_empty(new_q)) { int value = dequeue(new_q); enqueue(q, value); } delete_queue(new_q); free(table->keys); free(table->values); free(table); } int main() { struct queue *q = create_queue(); enqueue(q, 1); enqueue(q, 2); enqueue(q, 3); enqueue(q, 2); enqueue(q, 4); remove_duplicates(q); while (!is_empty(q)) { printf("%d ", dequeue(q)); } printf("\n"); delete_queue(q); return 0; } ``` 这个程序定义了两个数据结构:队列和哈希表。其中哈希表用于判断队列中的元素是否已经出现过。具体实现细节可以参考代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值