View Leetcode 527: word abbreviation
http://www.cnblogs.com/grandyang/p/6818742.html
Leetcode 527: Word Abbreviation
这道题让我们求单词的缩写形式,就是首尾字母加上中间字符的个数组成的新字符串,但是要求是不能有重复的缩写字符串,而且说明如果缩写字符串
的长度并没有减小的话就保留原来的字符串,比如god,缩写成g1d也没啥用,所以仍是god。博主刚开始在研究题目中给的例子的时候有些疑惑,虽然
知道internal和interval的缩写形式都是i6l,会冲突,博主刚开始不明白的是,为什么不能一个是i6l,一个是in5l,这样不就不冲突了么,而题目
中的缩写形式居然都是原字符串。后来才搞清楚题目原来是说只要有冲突的都不能用,而internal和interval是典型的死杠上的一对,i6l,in5l,
int4l,inte3l,inter2l,统统冲突,而再往后的缩写长度就和原字符串一样了,所以二者就都保留了原样。理解了题意就好办了,由于每个单词的
View Leetcode517: Super washing machine
Leetcode 517 - super washing machine
http://www.cnblogs.com/grandyang/p/6648557.html
这道题题给了我们一堆工藤新一,噢不,是滚筒洗衣机。我们有许多洗衣机,每个洗衣机里的衣服数不同,每个洗衣机每次只允许
向相邻的洗衣机转移一件衣服,问要多少次才能使所有洗衣机的衣服数相等。注意这里的一次移动是说所有洗衣机都可以移动一件
衣服到其相邻的洗衣机。这道题的代码量其实不多,难点是在于解题思路,难的是对问题的等价转换等。博主也没有做出这道题,
博主想到了要先验证衣服总数是否能整除洗衣机的数量,然后计算出每个洗衣机最终应该放的衣服数,返回跟初始状态衣服数之差的
最大值,但这种解法是不对的,无法通过这个test case [0, 0, 11, 5],最终每个洗衣机会留4件衣服,我想的那方法会返回7,
然后正确答案是8。想想也是,如果这么是这么简单的思路,这题怎么会标记为Hard呢,还是图样图森破啊。这里直接参照大神Chidong
的帖子来做,我们就用上面那个例子,有四个洗衣机,装的衣服数为[0, 0, 11, 5],最终的状态会变为[4, 4, 4, 4],那么我们将
View gist:0d5b1bf85b7fd7383e57252eecca8119
Leetcode 514
Discussion from the blog:
http://www.cnblogs.com/grandyang/p/6675879.html
这道题是关于辐射4这款游戏出的,博主虽然没有玩过这款游戏,但是知道确实有些游戏中需要破解一些谜题才能继续通关,像博主很早以前
玩过的恐龙危机啊,生化危机啊啥的,都有一些机关需要破解,博主大部分都要靠看攻略来通关哈哈。
这道题讲的是一种叫做自由之路的机关,我们需要将密码字符串都转出来,让我们求最短的转动步数。博主最先尝试的用贪婪算法来做,就是
每一步都选最短的转法,但是OJ中总有些test case会引诱贪婪算法得出错误的结果,因为全局最优解不一定都是局部最优解,而贪婪算法一直
View infixExpressionToBinaryExpressionTree.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace infixExpressionToBinaryExpressionTree
{
class Program
View gist:84d9964edd7fd64bcd010fe26976e839
using System;
class Solution
{
public static string Decrypt(string word)
{
if(word == null || word.Length == 0)
return "";
var length = word.Length;
View Find a path with minimum maximum value
First question:
you are given a matrix. Size m rows, n columns. start point and end point.
try to find a path from start point to end point, in the path, the next value is bigger then the previous value.
[[1,4,5,3]
[2,4,0,1]
[3,6,8,-1]]
start = [0,0] end [2,2]
==> [0,0], [1,0], [2,0],[2,1], [2,2]
View Search a path with increasing value given two nodes in matrix
Problem statement:
you are given a matrix. Size m rows, n columns. start point and end point.
try to find a path from start point to end point, in the path, the next value is bigger then the previous value.
Every time you can go four directions, left, top, right and bottom.
For example,
[[1,4,5,3]
[2,4,0,1]
[3,6,8,-1]]
View clone graph algorithm
Clone a graph, given a class Node and then ask to implement the function cloneGraph.
Here is the function:
public Node cloneGraph(Node root)
{
}
given class Node,
class Node{
Node p1;
View use infix expression to construct a binary express tree
use infix expresssion to construct binary expression tree
My feedback after the performance of 30 minutes of the peer:
Leet us to work on a simple infix expression and see how we can solve the problem together.
Given infix expression (1+2), we like to use stack to construct the binary expression tree.
First all elements in the expression are pushed to the stack until close bracket is met.
Here are the steps:
infix expression: (1+2)
View using infix Expression to construct binary expression tree
convert infix expression to binary expression tree
for example, ((2 + 2)+3)
The solution written by the peer:
+
+ 3
1 2
stack (