Acy夕拾算法 Week1_day4

LeetCode 20. 有效的括号

/*
整理思路:
·左括号必须以正确顺序闭合,用栈,先进后出,确保顺序闭合
·放入栈时,不放左括号本身,而是放对应的右括号–方便查询和弹出
·遍历,如果是({[,放入]});如果等于栈顶,弹出栈顶;都不对应,无效括号false

·错误:没考虑到 “){” ----栈顶为空时,return false;
·剪枝:字符串为奇数,一定不是有效字符串,括号两两对应
*/

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;

        if(s.size() % 2 != 0)   return false;//剪枝

        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == '(')     st.push(')');
            else if(s[i] == '{')    st.push('}');
            else if(s[i] == '[')    st.push(']');
            else if(st.empty() == 1)  return false;//有前后顺序
            else if(s[i] == st.top())   st.pop();//有前后顺序--top是st必须不为空
            else//不对应
                return false;
        }
        return st.empty();
    }
};

LeetCode 232. 用栈实现队列

/*
·实际pop无返回值
我的思路:
·两个栈,temp_stack中转栈,queue_stack队列先进先出栈
··eg: abc先放入temp_stack:栈顶-cba-栈底;
······这些做完,top&push到queue_stack:栈顶-abc-栈底;
·先push放入temp_stack中转栈; 把temp_stack的top,push到queue_stack里,同时temp_stack pop掉
·(需优化)得到pop/peek后再把queue_stack全部放回temp_stack---------下面改进

优化点:
·不必重新放回,当queue_stack为空时,再导入temp_stack即可----因为出队列,只有在前面的全部出完,才会轮到后面;
··这样就相当于:队尾(temp栈顶) ==|== 队头(queue栈顶),65|4321
··eg:push 1234; pop3:1、2、3; push 56; pop3:4、移动、5、6
··eg: 4321|_ -> |4321 -> 65|4 -> 65| -> _|65 -> |

·peek时复用pop,不要重复写
*/

class MyQueue {
    stack<int> temp_stack;
    stack<int> queue_stack;
public:
    MyQueue() {
        
    }
    
    void push(int x) {
        temp_stack.push(x);
    }
    
    int pop() {
        int temp, res;
        if(queue_stack.empty())
        {
            while( !temp_stack.empty() )
            {
                temp = temp_stack.top();
                queue_stack.push(temp);
                temp_stack.pop();
            }
        }
        res = queue_stack.top();
        queue_stack.pop();
        
        return res;
    }
    
    int peek() {
        int res = this->pop();  //this->使用已有函数
        queue_stack.push(res);

        return res;
    }
    
    bool empty() {

        return queue_stack.empty()&&temp_stack.empty();
    }
};

LeetCode 225. 用队列实现栈

方法一:两个队列

/*
栈、队列有.size()
我的思路:
两个队列,需要弹出时,把所有前面的都放进temp队列,size()-1次放入,最后一个直接pop不push
*/

class MyStack {
    queue<int> stack_que;
    queue<int> temp_que;
public:
    MyStack() {
        
    }
    
    void push(int x) {
        stack_que.push(x);
    }
    
    int pop() {
        int res, size = stack_que.size() - 1;//最后一个不push----
        while(size--)
            {
                res = stack_que.front();
                temp_que.push(res);
                stack_que.pop();
            }
        res = stack_que.front();//最后一个不push------------------
        stack_que.pop();

        stack_que = temp_que;//直接就能放 //再放回来

        while(!temp_que.empty())
        {//清空
            temp_que.pop();
        }
        return res;
    }
    
    int top() {
        int res = this->pop();
        stack_que.push(res);
        return res;
    }
    
    bool empty() {
        return stack_que.empty();
    }
};

方法二:一个队列

/*
优化:
一个队列;从队头到队尾重排,只改pop就好
*/

class MyStack {
    queue<int> stack_que;
public:
    MyStack() {
        
    }
    
    void push(int x) {
        stack_que.push(x);
    }
    
    int pop() {
        int res, size = stack_que.size() - 1;//最后一个pop掉----
        while(size--)
            {
                res = stack_que.front();
                stack_que.push(res);//重新入队
                stack_que.pop();
            }
        res = stack_que.front();//最后一个pop掉------------------
        stack_que.pop();

        return res;
    }
    
    int top() {
        int res = this->pop();
        stack_que.push(res);
        return res;
    }
    
    bool empty() {
        return stack_que.empty();
    }
};

队列有.back()返回队尾的方法

int top() {
        return stack_que.back();//C++:back()可以直接返回队尾
    }
### Java实现Delaunay三角剖分算法 Delaunay三角剖分是一种在二维空间中对点集进行三角化的方法,其核心特性是:任意两个点之间的边不会与其他边相交,并且每个三角形的外接圆内不包含其他点。这种特性使得Delaunay三角剖分广泛应用于计算机图形学、地理信息系统(GIS)等领域。 在Java中实现Delaunay三角剖分,通常可以采用**增量插入法**(Bowyer-Watson算法),该算法通过逐步插入点并删除与新点冲突的三角形来维护Delaunay性质。以下是一个基于该算法的Java实现示例。 --- ### Java代码实现 ```java import java.util.*; class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Point)) return false; Point other = (Point) obj; return Double.compare(other.x, x) == 0 && Double.compare(other.y, y) == 0; } @Override public int hashCode() { return Objects.hash(x, y); } } class Triangle { Point a, b, c; Set<Edge> edges = new HashSet<>(); Triangle(Point a, Point b, Point c) { this.a = a; this.b = b; this.c = c; edges.add(new Edge(a, b)); edges.add(new Edge(b, c)); edges.add(new Edge(c, a)); } boolean circumcircleContains(Point p) { double abx = a.x - b.x; double aby = a.y - b.y; double acx = a.x - c.x; double acy = a.y - c.y; double d = 2 * (abx * (a.y - c.y) - acx * aby); double cx_ = ( (a.x * a.x - b.x * b.x) * (a.y - c.y) - (a.x * a.x - c.x * c.x) * aby ) / d; double cy_ = ( (a.y * a.y - b.y * b.y) * (abx) - (a.y * a.y - c.y * c.y) * acx ) / d; double r = Math.sqrt((a.x - cx_) * (a.x - cx_) + (a.y - cy_) * (a.y - cy_)); double px = p.x - cx_; double py = p.y - cy_; return (px * px + py * py) <= r * r + 1e-8; } } class Edge { Point p1, p2; Edge(Point p1, Point p2) { if (p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y)) { this.p1 = p1; this.p2 = p2; } else { this.p1 = p2; this.p2 = p1; } } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Edge)) return false; Edge other = (Edge) obj; return p1.equals(other.p1) && p2.equals(other.p2); } @Override public int hashCode() { return Objects.hash(p1, p2); } } public class DelaunayTriangulation { private static List<Triangle> triangulate(List<Point> points) { List<Triangle> triangles = new ArrayList<>(); Point p1 = new Point(-1000, -1000); Point p2 = new Point(1000, -1000); Point p3 = new Point(0, 1000); Triangle superTriangle = new Triangle(p1, p2, p3); triangles.add(superTriangle); for (Point p : points) { Set<Edge> polygon = new HashSet<>(); List<Triangle> badTriangles = new ArrayList<>(); for (Triangle t : triangles) { if (t.circumcircleContains(p)) { badTriangles.add(t); for (Edge e : t.edges) { boolean shared = false; for (Triangle other : triangles) { if (other != t && other.edges.contains(e)) { shared = true; break; } } if (!shared) { polygon.add(e); } } } } for (Edge e : polygon) { triangles.add(new Triangle(e.p1, e.p2, p)); } triangles.removeAll(badTriangles); } // 移除包含超级三角形顶点的三角形 triangles.removeIf(t -> t.a == p1 || t.a == p2 || t.a == p3 || t.b == p1 || t.b == p2 || t.b == p3 || t.c == p1 || t.c == p2 || t.c == p3); return triangles; } public static void main(String[] args) { List<Point> points = new ArrayList<>(); points.add(new Point(0.0, 0.0)); points.add(new Point(1.0, 0.0)); points.add(new Point(0.5, 1.0)); points.add(new Point(0.5, 0.5)); List<Triangle> result = triangulate(points); for (Triangle t : result) { System.out.println("Triangle: (" + t.a.x + "," + t.a.y + ") - (" + t.b.x + "," + t.b.y + ") - (" + t.c.x + "," + t.c.y + ")"); } } } ``` --- ### 代码说明 - **Point类**:表示二维空间中的点。 - **Edge类**:表示三角形的边,用于边去重。 - **Triangle类**:表示一个三角形,并包含判断点是否在三角形外接圆内的方法。 - **triangulate方法**:使用Bowyer-Watson算法进行增量插入,构建Delaunay三角剖分。 - **main方法**:提供测试点集并输出三角剖分结果。 该实现能够处理任意二维点集,并输出符合Delaunay条件的三角形集合。算法的时间复杂度为O(n²),适用于中等规模的数据集。 --- ### 性能优化与扩展 对于大规模点集,可以引入**分治法**或**平面扫描法**来提升性能。此外,该算法也可以扩展到三维空间,形成**三维Delaunay三角剖分**(Tetrahedralization),但实现复杂度显著增加[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值