242.有效的字母异位词
题目链接:LeetCode 文章讲解:代码随想录 视频讲解:哔哩哔哩 编写语言:Java 完成状态:已完成
解题思路
将26个字母存放到数组中,两个数组进行遍历计数,再进行比较或者用一个数组也可,只需进行相应的减法 使用双重循环进行逐一清除判断,最后字符串长度为0则匹配成功
代码
数组
class Solution {
public boolean isAnagram ( String s, String t) {
if ( s. length ( ) != t. length ( ) ) {
return false ;
}
int [ ] a = new int [ 200 ] ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char ch = s. charAt ( i) ;
a[ ch] ++ ;
}
for ( int i = 0 ; i < t. length ( ) ; i++ ) {
char ch = t. charAt ( i) ;
a[ ch] -- ;
}
for ( int i = 97 ; i <= 122 ; i++ ) {
if ( a[ i] != 0 ) {
return false ;
}
}
return true ;
}
}
List集合
class Solution {
public boolean isAnagram ( String s, String t) {
if ( s. length ( ) != t. length ( ) ) {
return false ;
}
List < Character > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char ch = s. charAt ( i) ;
list. add ( ch) ;
}
for ( int i = 0 ; i < t. length ( ) ; i++ ) {
char ch = t. charAt ( i) ;
boolean flag = list. remove ( Character . valueOf ( ch) ) ;
if ( ! flag) {
return false ;
}
}
return true ;
}
}
Map集合
class Solution {
public boolean isAnagram ( String s, String t) {
if ( s. length ( ) != t. length ( ) ) {
return false ;
}
Map < Character , Integer > map = new HashMap < > ( ) ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char c = s. charAt ( i) ;
if ( map. containsKey ( c) ) {
map. put ( c, map. get ( c) + 1 ) ;
} else {
map. put ( c, 1 ) ;
}
}
for ( int i = 0 ; i < t. length ( ) ; i++ ) {
char c = t. charAt ( i) ;
if ( map. containsKey ( c) ) {
map. put ( c, map. get ( c) - 1 ) ;
if ( map. get ( c) == 0 ) {
map. remove ( c) ;
}
} else {
return false ;
}
}
return true ;
}
}
349. 两个数组的交集
题目链接:LeetCode 文章讲解:代码随想录 视频讲解:哔哩哔哩 编写语言:Java 完成状态:已完成
解题思路
设置哈希表用set直接去重 当数据比较小时(此题为1000),可以考虑数组的方式
代码
数组
class Solution {
public int [ ] intersection ( int [ ] nums1, int [ ] nums2) {
Set < Integer > set_result = new HashSet < > ( ) ;
int [ ] num_result = new int [ 1010 ] ;
for ( int i = 0 ; i < nums1. length; i++ ) {
num_result[ nums1[ i] ] = 1 ;
}
for ( int i = 0 ; i < nums2. length; i++ ) {
if ( num_result[ nums2[ i] ] == 1 ) {
set_result. add ( nums2[ i] ) ;
}
}
Integer [ ] num_result1 = set_result. toArray ( new Integer [ set_result. size ( ) ] ) ;
int [ ] num_result2 = new int [ num_result1. length] ;
for ( int i = 0 ; i < num_result2. length; i++ ) {
num_result2[ i] = num_result1[ i] ;
}
return num_result2;
}
}
Set集合
class Solution {
public int [ ] intersection ( int [ ] nums1, int [ ] nums2) {
Set < Integer > set_result = new HashSet < > ( ) ;
Set < Integer > set1 = new HashSet < > ( ) ;
for ( int i = 0 ; i < nums1. length; i++ ) {
set1. add ( nums1[ i] ) ;
}
for ( int i = 0 ; i < nums2. length; i++ ) {
if ( set1. contains ( nums2[ i] ) ) {
set_result. add ( nums2[ i] ) ;
}
}
Integer [ ] num_result1 = set_result. toArray ( new Integer [ 0 ] ) ;
int [ ] num_result2 = new int [ num_result1. length] ;
for ( int i = 0 ; i < num_result2. length; i++ ) {
num_result2[ i] = num_result1[ i] ;
}
return num_result2;
}
}
202. 快乐数
题目链接:LeetCode 文章讲解:代码随想录 编写语言:Java 完成状态:已完成
解题思路
题目中说了会 无限循环,那么也就是说求和的过程中,sum会重复出现,这对解题很重要! 所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
代码
Set集合
class Solution {
public boolean isHappy ( int n) {
Set < Integer > set = new HashSet < > ( ) ;
while ( true ) {
int sum = getSum ( n) ;
if ( sum == 1 ) {
return true ;
}
if ( set. contains ( sum) ) {
return false ;
} else {
set. add ( sum) ;
}
n = sum;
}
}
public int getSum ( int n) {
String str = String . valueOf ( n) ;
int sum = 0 ;
for ( int i = 0 ; i < str. length ( ) ; i++ ) {
int num = Integer . valueOf ( str. charAt ( i) + "" ) ;
sum += num * num;
}
return sum;
}
}
1. 两数之和
题目链接:LeetCode 文章讲解:代码随想录 视频讲解:哔哩哔哩 编写语言:Java 完成状态:已完成
解题思路
既有数值又有下标,优先想到哈希表中的map,而set只有key,不适用key是值,value是下标,进行遍历,查询key,返回value集合 可以使用暴力法双重循环枚举查找,nums[i] + nums[j] = target时,返回对应的{i,j}即可
代码
暴力法
class Solution {
public int [ ] twoSum ( int [ ] nums, int target) {
for ( int i = 0 ; i < nums. length; i++ ) {
for ( int j = i + 1 ; j < nums. length; j++ ) {
if ( nums[ i] + nums[ j] == target) {
return new int [ ] { i, j} ;
}
}
}
return new int [ ] { } ;
}
}
Map集合
class Solution {
public int [ ] twoSum ( int [ ] nums, int target) {
Map < Integer , Integer > map = new HashMap < > ( ) ;
for ( int i = 0 ; i < nums. length; i++ ) {
int value = target - nums[ i] ;
boolean flag = map. containsKey ( value) ;
if ( flag == true ) {
return new int [ ] { map. get ( value) , i} ;
} else {
map. put ( nums[ i] , i) ;
}
}
return new int [ ] { } ;
}
}