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 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"));
- MySQL修改root密码的多种方法
- 参考:https://www.cnblogs.com/cuikang/p/5977800.html https://www.linuxidc.com/Linux/2018-05/152586.htm
//没有设置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包
参考网站:https://www.cnblogs.com/ziziwu/archive/2012/02/01/2335102.html https://blog.youkuaiyun.com/fantaxy025025/article/details/84918729
有时候需要删掉之前的rpm源,安装其他版本的rpm源,就涉及到删除rpm包。
rpm -q <关键字> 可以查询到rpm包的名字
rpm -e <包的名字> 删除特定rpm包
//我使用的
yum erase zabbix-server-mysql
在路径:/etc/yum.repos.d 中可以看到以安装的 rpm 包。也可以直接在这个文件夹中删除对应文件。