行走的轮子

戒急用忍


  • 首页

  • 归档

  • 标签

  • 到此一游

  • 搜索

Next主题添加畅言评论

发表于 2017-04-16   |   分类于 博客 , 主题 , 评论   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

为什么要增加畅言评论

之前一直在用多说评论,但是多说就要关闭了,就是在2017年6月1日。所以必须在这之前改用新的评论系统,不然的话博客就要不能评论了(然而并没有什么人给我评论)。然后就是选择用什么评论系统了,也没有什么可参考,随便选了一个。

至于为什么要自己添加,因为我的Next版本很旧了,但是自己做过很多改动不想更新到最新版本。

阅读全文 »

LeetCode 514. Freedom Trail

发表于 2017-03-18   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open the door.

Given a string ring, which represents the code engraved on the outer ring and another string key, which represents the keyword needs to be spelled. You need to find the minimum number of steps in order to spell all the characters in the keyword.

Initially, the first character of the ring is aligned at 12:00 direction. You need to spell all the characters in the string key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at 12:00 direction and then by pressing the center button.
At the stage of rotating the ring to spell the key character key[i]:
You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. The final purpose of the rotation is to align one of the string ring’s characters at the 12:00 direction, where this character must equal to the character key[i].
If the character key[i] has been aligned at the 12:00 direction, you need to press the center button to spell, which also counts as 1 step. After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you’ve finished all the spelling.

Example:

Input: ring = “godding”, key = “gd”
Output: 4
Explanation:
For the first key character ‘g’, since it is already in place, we just need 1 step to spell this character.
For the second key character ‘d’, we need to rotate the ring “godding” anticlockwise by two steps to make it become >”ddinggo”.
Also, we need 1 more step for spelling.
So the final output is 4.

Note:

  1. Length of both ring and key will be in range 1 to 100.
  2. There are only lowercase letters in both strings and might be some duplcate characters in both strings.
  3. It’s guaranteed that string key could always be spelled by rotating the string ring.

题意解析

题目来自于辐射4这个游戏。有一个圆盘,圆盘上有几个英文字母,圆盘12点方向代表当前选中的字母,通过这个圆盘拼出一个和题目一样的字符串,求出最小的步数。

圆盘可以顺时针或者逆时针旋转,每旋转一个字母代表1步,当12点方向是需要的英文字母时,按确定按钮,这个按也算一步。

阅读全文 »

LeetCode 524. Longest Word in Dictionary through Deleting

发表于 2017-03-18   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.

Example 1:

Input:
s = “abpcplea”, d = [“ale”,”apple”,”monkey”,”plea”]

Output:
“apple”

Example 2:

Input:
s = “abpcplea”, d = [“a”,”b”,”c”]

Output:
“a”

Note:

  1. All the strings in the input will only contain lower-case letters.
  2. The size of the dictionary won’t exceed 1,000.
  3. The length of all the strings in the input won’t exceed 1,000.

题意解析

给出一个String A和一个String数组,在String数组中找到最长的String B,这个String可以满足从A中删除一些字母得到。如果满足的字符串有多个,则选择字母序最小的字符串。

阅读全文 »

LeetCode 539. Minimum Time Difference

发表于 2017-03-18   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimum minutes difference between any two time points in the list.

Example 1:

Input: [“23:59”,”00:00”]
Output: 1
Note:

  1. The number of time points in the given list is at least 2 and won’t exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

题意解析

计算最短的时间间隔,注意24小时制值得变化。

阅读全文 »

LeetCode 535. Encode and Decode TinyURL

发表于 2017-03-08   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Note: This is a companion problem to the System Design problem: Design TinyURL.

TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

Design the encode and decode methods for the TinyURL service. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.

题意解析

完成短链接的编解码。

阅读全文 »

LeetCode 529. Minesweeper

发表于 2017-03-03   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Let’s play the minesweeper game (Wikipedia), online game)!

You are given a 2D char matrix representing the game board. ‘M’ represents an unrevealed mine, ‘E’ represents an unrevealed empty square, ‘B’ represents a revealed blank square that has no adjacent (above, below, left, right, and all 4 diagonals) mines, digit (‘1’ to ‘8’) represents how many mines are adjacent to this revealed square, and finally ‘X’ represents a revealed mine.

Now given the next click position (row and column indices) among all the unrevealed squares (‘M’ or ‘E’), return the board after revealing this position according to the following rules:

If a mine (‘M’) is revealed, then the game is over - change it to ‘X’.
If an empty square (‘E’) with no adjacent mines is revealed, then change it to revealed blank (‘B’) and all of its adjacent unrevealed squares should be revealed recursively.
If an empty square (‘E’) with at least one adjacent mine is revealed, then change it to a digit (‘1’ to ‘8’) representing the number of adjacent mines.
Return the board when no more squares will be revealed.

Example 1:

Input:

[[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘M’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’],
[‘E’, ‘E’, ‘E’, ‘E’, ‘E’]]

Click : [3,0]

Output:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Explanation:

Example 2:

Input:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘M’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Click : [1,2]

Output:

[[‘B’, ‘1’, ‘E’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘X’, ‘1’, ‘B’],
[‘B’, ‘1’, ‘1’, ‘1’, ‘B’],
[‘B’, ‘B’, ‘B’, ‘B’, ‘B’]]

Explanation:

Note:

  1. The range of the input matrix’s height and width is [1,50].
  2. The click position will only be an unrevealed square (‘M’ or ‘E’), which also means the input board contains at least one clickable square.
  3. The input board won’t be a stage when game is over (some mines have been revealed).
  4. For simplicity, not mentioned rules should be ignored in this problem. For example, you don’t need to reveal all the unrevealed mines when the game is over, consider any cases that you will win the game or flag any squares.

题意解析

这个题指的是扫雷游戏。在扫雷游戏中,用二维矩阵标识当前的扫雷状态。没有被发掘的空块标记为'E',没有被发掘的雷为'M',发掘之后的空块标记为'N',发掘之后的块标记为'X',另外数字1-8表示该块周围有几个雷。

给出一个二维矩阵标识当天扫雷游戏的状态,然后给出下一步,求出走完下一步之后的游戏状态。

阅读全文 »

LeetCode 526. Beautiful Arrangement

发表于 2017-02-28   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:

  1. The number at the ith position is divisible by i.
  2. i is divisible by the number at the ith position.
    Now given N, how many beautiful arrangements can you construct?

Example 1:

Input: 2
Output: 2
Explanation:

The first beautiful arrangement is [1, 2]:

Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).

Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).

The second beautiful arrangement is [2, 1]:

Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).

Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

Note:

  1. N is a positive integer and will not exceed 15.

题意解析

所谓美丽的排布是指,在i位置上的数字能够整除i或者被i整除,i从1开始。给定一个数字N,计算有多少个这样的排布。

阅读全文 »

LeetCode 516. Longest Palindromic Subsequence

发表于 2017-02-27   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.

Example 1:
Input:

“bbbab”
Output:
4

One possible longest palindromic subsequence is “bbbb”.

Example 2:
Input:

“cbbd”
Output:
2

One possible longest palindromic subsequence is “bb”.

题意解析

查找一个字符串中包含的最长回文长度。

阅读全文 »

LeetCode 488. Zuma Game

发表于 2017-02-08   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.

Examples:

Input: “WRRBBW”, “RB”
Output: -1
Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW

Input: “WWRRBBWW”, “WRBRW”
Output: 2
Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

Input:”G”, “GGGGG”
Output: 2
Explanation: G -> G[G] -> GG[G] -> empty

Input: “RBYYBBRRB”, “YRBGB”
Output: 3
Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

Note:

  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. The number of balls on the table won’t exceed 20, and the string represents these balls is called “board” in the input.
  3. The number of balls in your hand won’t exceed 5, and the string represents these balls is called “hand” in the input.
  4. Both input strings will be non-empty and only contain characters ‘R’,’Y’,’B’,’G’,’W’.

题意解析

祖玛游戏,不知道你们有没有玩儿过,我也是很久很久之前玩儿了,久到实在想不出来是什么时候的事情了。在桌上有一行球,球有5种颜色,红、黄、蓝、绿、白。你的手里也有一些这些颜色的球,每次可以选择一个球插入到桌上一行球中的任意位置,如果有3个或者3个以上相同颜色的球出现则将它们消除。然后再持续把手里的球插入到桌面一行球中,直到桌面上的这行秀全部消除。

求出能把桌面上的球全部消除的最小步数。

阅读全文 »

LeetCode 508. Most Frequent Subtree Sum

发表于 2017-02-08   |   分类于 LeetCode , Java   |   ❤

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

题目要求

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.

Examples 1
Input:

5
/ \
2 -3
return [2, -3, 4], since all the values happen only once, return all of them in any order.

Examples 2
Input:

5
/ \
2 -5
return [2], since 2 happens twice, however -5 only occur once.

题意解析

子树和的含义是指树中的一个节点所有子节点值得和(包含该节点)。一棵树每个节点都有子树和,找到存在数目最多的子树和。

阅读全文 »
1234…37
Jerky Lu

Jerky Lu

戒急用忍

364 日志
45 分类
75 标签
GitHub Weibo
友情链接
  • 李琼写文章的地方
  • 落影流年
  • Koihik's Blog
© 2020 Jerky Lu
京ICP备15007413号-1
主题 - NexT.Mist