//实现 pow(x, n) ,即计算 x 的 n 次幂函数。
//
// 示例 1:
//
// 输入: 2.00000, 10
//输出: 1024.00000
//
//
// 示例 2:
//
// 输入: 2.10000, 3
//输出: 9.26100
//
//
// 示例 3:
//
// 输入: 2.00000, -2
//输出: 0.25000
//解释: 2-2 = 1/22 = 1/4 = 0.25
//
// 说明:
//
//
// -100.0 < x < 100.0
// n 是 32 位有符号整数,其数值范围是 [−231, 231 − 1] 。
//
// Related Topics 数学 二分查找
// 👍 567 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public double myPow(double x, int n) {
// 2的10次方,是10个2 相乘,
if (n == 0) { return 1; }
if (n == 1) { return x; }
if (n == -1) { return 1 / x; }
double half = myPow(x, n / 2);
// 如果遇到奇数,则额外乘以一个数,偶数则是1
double rest = myPow(x, n % 2);
return rest * half * half;
}
}
class Solution {
public double myPow(double x, int n) {
return myPowValue(x, n);
}
public double myPowValue(double x, int n) {
//
if (n == 1) return x;
if (n == 0) return 1;
if (n == -1) return 1 / x;
double v = myPowValue(x, n / 2);
return v * v *(n % 2 == 1 ? x : 1);
}
}
//给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
//
// 示例:
//
// 输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
//输出:
//[
// ["ate","eat","tea"],
// ["nat","tan"],
// ["bat"]
//]
//
// 说明:
//
//
// 所有输入均为小写字母。
// 不考虑答案输出的顺序。
//
// Related Topics 哈希表 字符串
// 👍 624 👎 0
import java.util.List;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<Integer, List<String>> map = new HashMap();
for (String str : strs) {
char[] chars = str.toCharArray();
int i = -1;
for (char aChar : chars) {
i+=aChar;
}
List<String> list = map.computeIfAbsent(i, k -> new ArrayList<String>());
list.add(str);
}
return new ArrayList<>(map.values());
}
}
//leetcode submit region end(Prohibit modification and deletion)