很简单,哈希一下
进阶
javapublic class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
Map<Character, Integer> charCount = new HashMap<>();
// 遍历第一个字符串,统计每个字符出现的次数
for (int i = 0; i < s.length(); i++) {
charCount.put(s.charAt(i), charCount.getOrDefault(s.charAt(i), 0) + 1);
}
// 遍历第二个字符串,减去对应字符的计数
for (int i = 0; i < t.length(); i++) {
charCount.put(t.charAt(i), charCount.getOrDefault(t.charAt(i), 0) - 1);
}
// 检查HashMap中的所有值是否都为0
for (int count : charCount.values()) {
if (count != 0) {
return false;
}
}
return true;
}
}
也是哈希,很简单的思路
javapublic class Solution {
public int longestPalindrome(String s) {
int[] count = new int[54]; // 26个小写字母 + 26个大写字母 + 2个额外空间
int result = 0;
boolean hasOdd = false;
// 统计每个字母出现的次数
for (char c : s.toCharArray()) {
int index = c >= 'a' ? c - 'a' : c - 'A' + 26;
count[index]++;
}
// 计算最长回文串的长度
for (int i = 0; i < count.length; i++) {
if (count[i] % 2 == 0) {
result += count[i];
} else {
result += count[i] - 1; // 出现奇数次的字母,取偶数次
hasOdd = true;
}
}
// 如果有出现奇数次的字母,结果加1
if (hasOdd) {
result++;
}
return result;
}
}
javapublic class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> duplicates = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
while (nums[i] != nums[nums[i] - 1]) {
// 交换 nums[i] 和 nums[nums[i] - 1]
int temp = nums[i];
nums[i] = nums[temp - 1];
nums[temp - 1] = temp;
}
}
// 找出出现两次的整数
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i + 1) {
duplicates.add(nums[i]);
}
}
return duplicates;
}
}
javaimport java.util.Random;
public class Solution {
private int[] nums;
private Random rand;
public Solution(int[] nums) {
this.nums = nums;
this.rand = new Random();
}
public int pick(int target) {
int count = 0;
int result = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
count++; // 遇到目标值的次数
// 以1/count的概率选择当前索引
if (rand.nextInt(count) == 0) {
result = i;
}
}
}
return result;
}
}
/////
class Solution {
Map<Integer, List<Integer>> pos;
Random random;
public Solution(int[] nums) {
pos = new HashMap<Integer, List<Integer>>();
random = new Random();
for (int i = 0; i < nums.length; ++i) {
pos.putIfAbsent(nums[i], new ArrayList<Integer>());
pos.get(nums[i]).add(i);
}
}
public int pick(int target) {
List<Integer> indices = pos.get(target);
return indices.get(random.nextInt(indices.size()));
}
}
javapublic class Solution {
public String[] findWords(String[] words) {
// 创建三个集合,分别代表键盘上的三行
Set<Character> row1 = new HashSet<>();
Set<Character> row2 = new HashSet<>();
Set<Character> row3 = new HashSet<>();
// 初始化三行的字符集合
String row1Chars = "qwertyuiop";
String row2Chars = "asdfghjkl";
String row3Chars = "zxcvbnm";
for (char c : row1Chars.toCharArray()) {
row1.add(c);
}
for (char c : row2Chars.toCharArray()) {
row2.add(c);
}
for (char c : row3Chars.toCharArray()) {
row3.add(c);
}
// 创建一个列表来存储结果
List<String> result = new ArrayList<>();
// 遍历每个单词
for (String word : words) {
// 将单词转换为小写,以便比较
String lowerCaseWord = word.toLowerCase();
// 检查单词中的每个字符是否都在同一行
boolean isOnSameRow1 = true;
boolean isOnSameRow2 = true;
boolean isOnSameRow3 = true;
for (char c : lowerCaseWord.toCharArray()) {
if (!row1.contains(c)) {
isOnSameRow1 = false;
}
if (!row2.contains(c)) {
isOnSameRow2 = false;
}
if (!row3.contains(c)) {
isOnSameRow3 = false;
}
}
// 如果单词中的所有字符都在同一行,则添加到结果列表中
if (isOnSameRow1 || isOnSameRow2 || isOnSameRow3) {
result.add(word);
}
}
// 将结果列表转换为数组并返回
return result.toArray(new String[0]);
}
}
本文作者:yowayimono
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!