LeetCode 33. Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

题目大意:假设按照升序排列的数组在事先未知的某个关键点上旋转。给一个目标数target,找这个数是否存在于这个序列中,如果存在返回下标,不存在返回-1

分析:二分搜索法,如果当前nums[mid] < nums[l],说明mid在旋转节点的右边,那么如果target也在mid和r之间就将l = mid + 1表示在mid + 1到r的区域找,否则将r = mid – 1在l到mid – 1的区域寻找;如果当前nums[mid] > nums[r],说明mid在旋转节点的左边,那么如果target也在l和mid之间就将r = mid – 1,在l~mid-1的区域内寻找,否则在mid+1~r的区域内寻找;否则说明当前区间的顺序是正确的,就判断target和mid的大小关系,如果比mid所指数字大则在右边找,否则在左边找

 

LeetCode 671. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example 1:
Input:
2
/ \
2 5
/ \
5 7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.
Example 2:
Input:
2
/ \
2 2

Output: -1
Explanation: The smallest value is 2, but there is not any second smallest value.

题目大意:给一个非空的树,这个树所有值非负,而且每个结点只有2个孩子或者0个孩子,如果当前结点有两个孩子,则这个结点的值比它的两个孩子的值都小,要求输出这个树第二小的值。

分析:遍历树,将所有的值放入集合中,输出集合的第二个数;如果集合的大小小于等于1,则输出-1

 

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后新数组中的个数~