LeetCode 538. Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:
5
/ \
2 13

Output: The root of a Greater Tree like this:
18
/ \
20 13

题目大意:给定一个二叉搜索树(BST),将其转换为一个Greater Tree,使得原始BST的每个结点的键值被改变为原始键加上所有比BST中的原始键大的键的总和。

分析:因为BST的中序遍历是从小到大排列,那么BST的右根左遍历方式得到的就是从大到小的排列,遍历过程中对当前结点累计到sum中,并将sum的值赋值给当前结点,最后返回这棵树即可~

LeetCode 521. Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:
Input: “aba”, “cdc”
Output: 3
Explanation: The longest uncommon subsequence is “aba” (or “cdc”),
because “aba” is a subsequence of “aba”,
but not a subsequence of any other strings in the group of two strings.
Note:

Both strings’ lengths will not exceed 100.
Only letters from a ~ z will appear in input strings.

题目大意:给定两个字符串,找这两个字符串中最长非公共子序列。 最长的非公共子序列是指这些字符串之一的最长的子序列,并且这个子序列不是其他字符串的任何子序列。

分析:如果a和b相等,那么a的任意一个子串都是b的子串,反之同理,所以a和b相等时返回-1;如果a和b不相等,返回a和b长度中较长的数字,因为只要取a和b中长度较长的那个字符串,必然是另一方的最长非公共子串~

 

LeetCode 507. Perfect Number

We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.

Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)

题目大意:完美数字是指它的所有可以整除的正数中除了它本身,其他数字之和等于这个数字的数。给一个正整数n,写一个函数,当它是一个完美数字的时候返回true否则false。

分析:从2~sqrt(num),累加所有i和num/i【因为如果从1~num一个个试是否可以整除的话会超时,而且也没必要,因为知道了除数a必然就知道了num/a这个数字也是它的除数】因为最后还有一个1没有加,所以sum一开始为1,然后返回num == sum,注意如果num本身为1,则要return false,因为1的唯一一个除数1是它本身不能累加,所以1不满足条件~

 

LeetCode 443. String Compression

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

Follow up:
Could you solve it using only O(1) extra space?

Example 1:
Input:
[“a”,”a”,”b”,”b”,”c”,”c”,”c”]

Output:
Return 6, and the first 6 characters of the input array should be: [“a”,”2″,”b”,”2″,”c”,”3″]

Explanation:
“aa” is replaced by “a2”. “bb” is replaced by “b2”. “ccc” is replaced by “c3”.
Example 2:
Input:
[“a”]

Output:
Return 1, and the first 1 characters of the input array should be: [“a”]

Explanation:
Nothing is replaced.
Example 3:
Input:
[“a”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”,”b”]

Output:
Return 4, and the first 4 characters of the input array should be: [“a”,”b”,”1″,”2″].

Explanation:
Since the character “a” does not repeat, it is not compressed. “bbbbbbbbbbbb” is replaced by “b12”.
Notice each digit has its own entry in the array.
Note:
All characters have an ASCII value in [35, 126].
1 <= len(chars) <= 1000.

分析:指针i指向修改内容的位置,指针j遍历整个数组chars,当下一个字符与当前字符不相同时,直接将该字符赋值到i处,然后i++,j++;否则若相同,k指向j所在位置,j继续向前出发遍历所有与k处相同的字符,则相同的个数为j-k,将j-k转化为字符串s,将s的每一个字符都赋值在i所在位置开始的chars中~最后直到j>=n时退出循环,此时i的值即为in-place后新数组中的个数~

 

LeetCode 557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:
Input: “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Note: In the string, each word is separated by single space and there will not be any extra space in the string.

题目大意:将一个字符串的每个单词反转~
分析:将每个单词放入栈中,当遇到空格或者最后一个字符的时候,说明当前栈内为一个完整的单词,那么就将栈内的单词按字符一个个出栈加入result字符串中,根据flag的值判断是否是第一个单词,如果不是第一个单词就要在result的后面加一个空格~

 

LeetCode 524. Longest Word in Dictionary through Deleting

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:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won’t exceed 1,000.
The length of all the strings in the input won’t exceed 1,000.

题目大意:给一个string s和一个string字典d,找字典中的某个string,寻找s的子串(满足可以通过删除s中某些元素后得到该string),寻找满足条件的字符串中最长的一个,如果有多个长度相等的就返回字典序中最小的那个,如果一个都没有满足条件的string,就返回一个空字符串~
分析:遍历字典中的某一个字符串,设当前字符串的下标为index,对于当前字符串d[index],使用两个指针i和j分别从头到尾遍历s和d[index],随着i指针的增加,如果j指针所指元素和i指针所指元素相同就向后移动一位,当i指针都指完的时候,j如果也指完了说明满足条件,当前d[index]是s的子串,如果当前d[index]的长度比保存的result字符串长度长,就更新result,或者一样长但是字典序排列中d[index]比result小,也要更新result,最后返回result~