当前位置:网站首页>Question skimming record - hash table
Question skimming record - hash table
2022-07-28 06:46:00 【HandsomeDog_ L】
Catalog
Effective alphabetic words ( Power button 242)
Intersection of two arrays (349)
** Generally, hash table is used to quickly determine whether an element appears in the set **
Effective alphabetic words ( Power button 242)
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
```java
class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];
for (char c : s.toCharArray()) {
record[c - 'a'] += 1;
}
for (char c : t.toCharArray()) {
record[c - 'a'] -= 1;
}
for (int i : record) {
if (i != 0) {
return false;
}
}
return true;
}
}
```
Intersection of two arrays (349)
```java
import java.util.HashSet;
import java.util.Set;
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
return new int[0];
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> resSet = new HashSet<>();
// Traversal array 1
for (int i : nums1) {
set1.add(i);
}
// Traversal array 2 To determine whether the element exists in the hash table
for (int i : nums2) {
if (set1.contains(i)) {
resSet.add(i);
}
}
int[] resArr = new int[resSet.size()];
int index = 0;
// Turn the resulting geometry into an array
for (int i : resSet) {
resArr[index++] = i;
}
return resArr;
}
}
```
Happy number (202)
Write an algorithm to judge a number n Is it a happy number .
「 Happy number 」 Defined as : For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions , Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1. If It can be 1, So this number is the happy number .
```java
// Speed pointer
class Solution {
public int getNext(int n) {
int totalSum = 0;
while (n > 0) {
int d = n % 10;
n = n / 10;
totalSum += d * d;
}
return totalSum;
}
public boolean isHappy(int n) {
int slowRunner = n;
int fastRunner = getNext(n);
while (fastRunner != 1 && slowRunner != fastRunner) {
slowRunner = getNext(slowRunner);
fastRunner = getNext(getNext(fastRunner));
}
return fastRunner == 1;
}
}
//
class Solution {
public boolean isHappy(int n) {
Set<Integer> record = new HashSet<>();
while (n != 1 && !record.contains(n)) {
record.add(n);
n = getNextNumber(n);
}
return n == 1;
}
private int getNextNumber(int n) {
int res = 0;
while (n > 0) {
int temp = n % 10;
res += temp * temp;
n = n / 10;
}
return res;
}
}
```
Sum of two numbers
```java
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
if(nums == null || nums.length == 0){
return res;
}
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = target - nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
}
map.put(nums[i], i);
}
return res;
}
```
Add four numbers ||(454)
Given a list of four arrays containing integers A , B , C , D , Calculate how many tuples there are (i, j, k, l) , bring A[i] + B[j] + C[k] + D[l] = 0.
```java
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int temp;
int res = 0;
// Count the sum of the elements in two arrays , At the same time, count the number of occurrences , Put in map
for (int i : nums1) {
for (int j : nums2) {
temp = i + j;
if (map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
}
}
// Count the sum of the remaining two elements , stay map Find out if there is an addition to 0 The situation of , Record the number of times at the same time
for (int i : nums3) {
for (int j : nums4) {
temp = i + j;
if (map.containsKey(0 - temp)) {
res += map.get(0 - temp);
}
}
}
return res;
}
}
```
Ransom letter (383)
Give a ransom (ransom) String and a magazine (magazine) character string , Judge the first string ransom Can I have a second string magazines The characters inside make up . If it can be constructed , return true ; Otherwise return to false.
```java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// Define a hash mapping array
int[] record = new int[26];
// Traverse
for(char c : magazine.toCharArray()){
record[c - 'a'] += 1;
}
for(char c : ransomNote.toCharArray()){
record[c - 'a'] -= 1;
}
// If there are negative numbers in the array , explain ransomNote Strings always exist magazine Characters not in
for(int i : record){
if(i < 0){
return false;
}
}
return true;
}
}
```
Sum of three numbers (15)
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all the triples that meet the conditions and do not repeat .
```java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
return result;
}
}
```
Sum of four numbers (18)
The question : Given an inclusion n Array of integers nums And a target value target, Judge nums Are there four elements in a,b,c and d , bring a + b + c + d The value of is equal to target equal ? Find all quadruples that meet the conditions and are not repeated
```java
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
left++;
right--;
}
}
}
}
return result;
}
}
```
边栏推荐
- [C note] data type and storage
- 【C语言】自定义结构体类型
- What is hash? (development of Quantitative Trading Robot System)
- 用c语言实现三子棋小游戏
- Source code analysis of countdownlatch of AQS
- 江中ACM新生10月26日习题题解
- Treasure plan TPC system development DAPP construction
- Using C language to realize three piece chess games
- 战疫杯--奇奇怪怪的形状
- Valgrind tool
猜你喜欢
随机推荐
Bug experience related to IAP jump of stm32
ZOJ Problem 1005 jugs
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
Dynamic programming -- simple question type of climbing stairs
NFT data storage blind box + mode system development
代码整洁之道(一)
刷题记录----哈希表
2021-11-10
About the collation of shader keyword
Leetcode 刷题日记 剑指 Offer II 053. 二叉搜索树中的中序后继
关于Shader KeyWord的整理
Brief analysis of order transaction
[pta ---- traversal of tree]
mongo ssl 配置实战
AQS之CyclicBarrier源码解析
[PTA----输出全排列]
Two dimensional array practice: spiral matrix
explain详解
Pyppeteer is recognized to bypass detection
[dynamic planning -- the best period series for buying and selling stocks]
![[哈希表基础知识]](/img/8f/54a4780a02f81e5de3d92c25248e1e.png)

![[队列,栈的简单应用----包装机]](/img/bc/617b1eb35558c4f948018f593a1de5.jpg)






