ARTS-第13周-190617-算法:堆、合并排序;php 时间函数;Mysql忘记root密码;rpm包删除

博客主要分享了算法题的解题思路,包括判断括号字符串是否有效的栈解法,以及合并两个排序链表的遍历合并法。此外,还提及了php时间函数、MySQL修改root密码的多种方法,以及卸载rpm包的操作和相关参考网站。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Algotithm
Valid Parentheses

Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

给一个字符串,只有这些字符 ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ , ‘]’,判断字符串是否可用。这个可用的定义是:括号要成对并按照顺序出现,和数学表达式中的判断类似。

Example 1: Input: “()” Output: true
Example 2: Input: “()[]{}” Output: true
Example 3: Input: “(]” Output: false
Example 4: Input: “([)]” Output: false
Example 5: Input: “{[]}” Output: true

解题思路和解析表达式的过程有点类似,栈。

static public bool IsValid(string s)
{
    bool bResult = true;
    Stack<char> stack_cache = new Stack<char>();
    for (int i = 0; i < s.Length; i++)
    {
        char cur_char = s[i];
        switch(cur_char)
        {
            case '(':
            case '{':
            case '[':
                stack_cache.Push(cur_char);
                break;
            case ')':
                if (stack_cache.Count>0 && stack_cache.Peek() == '(')
                {
                    stack_cache.Pop();
                }
                else
                {
                    bResult = false;
                    break;
                }
                break;
            case '}':
                if (stack_cache.Count > 0 && stack_cache.Peek() == '{')
                {
                    stack_cache.Pop();
                }
                else
                {
                    bResult = false;
                    break;
                }
                break;
            case ']':
                if (stack_cache.Count > 0 && stack_cache.Peek() == '[')
                {
                    stack_cache.Pop();
                }
                else
                {
                    bResult = false;
                    break;
                }
                break;
            default:
                break;
        }
    }
    if (bResult && stack_cache.Count > 0)
    {
        bResult = false;
    }
    return bResult;
}

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

将两个排序的列表合并,这个没什么太多要说的,同时遍历两个列表然后进行合并。
参考了讨论中的思路,创建一个头节点,这样在取第一个节点的时候就不需要特殊判断。

static public ListNode MergeTwoLists(ListNode l1, ListNode l2)
{
    if (l1 == null && l2 == null) return null;
    if (l1 == null) return l2;
    if (l2 == null) return l1;
    ListNode result = new ListNode(0);
    ListNode head = result;
    while (l1!=null && l2!=null)
    {
        if (l1.val < l2.val)
        {
            result.next = l1;
            result = result.next;
            l1 = l1.next;
        }
        else
        {
            result.next = l2;
            result = result.next;
            l2 = l2.next;
        }
    }
    if (l1 != null)
    {
        result.next = l1;
    }
    if (l2 != null)
    {
        result.next = l2;
    }
    return head.next;
}

Review

Tips&Share

  • php 时间函数
//前一个小时:
date("Y-m-d H:i:s", strtotime("-1 hour"));
//前一天:
date("Y-m-d H:i:s", strtotime("-1 day"));
//前一个月:
date("Y-m-d H:i:s", strtotime("-1 month"));
//前一年:
date("Y-m-d H:i:s", strtotime("-1 year"));
//没有设置root密码,可以直接无密码登录mysql
mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

//在丢失root密码时
//修改文件 my.cnf ,在 [mysqld] 字段增加 skip-grant-tables
use mysql;
update user set authentication_string=password('root') where user='123' and Host = 'localhost';
update user set password=password('123') where user='root' and Host = 'localhost';
flush privileges;
exit;
 rpm -q <关键字> 可以查询到rpm包的名字
 rpm -e <包的名字> 删除特定rpm包
 //我使用的
 yum erase zabbix-server-mysql

在路径:/etc/yum.repos.d 中可以看到以安装的 rpm 包。也可以直接在这个文件夹中删除对应文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值