算法day05
242. 有效的字母异位词
题目链接:https://leetcode.cn/problems/valid-anagram/description/ 第一眼思路:字母异位词:即所含字母相同,字母个数相同,但位置可以不同。所以直接暴力:统计两个字符串的字符个数并记录,最后对比。 第三四眼思路:第一眼的思路就蛮好的。 统计字母个数并记录比对的暴力解法:
class Solution {
public boolean isAnagram ( String s, String t) {
char [ ] sarr = new char [ 26 ] ;
char [ ] tarr = new char [ 26 ] ;
for ( int i = 0 ; i< s. length ( ) ; i++ ) {
char c = s. charAt ( i) ;
sarr[ c- 97 ] += 1 ;
}
for ( int i = 0 ; i< t. length ( ) ; i++ ) {
char c = t. charAt ( i) ;
tarr[ c- 97 ] += 1 ;
}
for ( int i = 0 ; i< sarr. length; i++ ) {
if ( sarr[ i] != tarr[ i] ) {
return false ;
}
}
return true ;
}
}
卡哥的思路比我的写法少了一个数组,直接对一个数组进行操作就好。第一个字符串的字母就++,其他字符串的字母就–
349. 两个数组的交集
题目链接:https://leetcode.cn/problems/intersection-of-two-arrays/description/ 第一眼思路:所谓两个数组的交集,也就是,A有且B有的元素。用HashSet对A去重,再判断B的元素是否在A中,在A中的话就要放到要输出的数组中。 HashSet代码实现:
class Solution {
public int [ ] intersection ( int [ ] nums1, int [ ] nums2) {
HashSet < Integer > hashSet = new HashSet < > ( ) ;
HashSet < Integer > hashSet2 = new HashSet < > ( ) ;
for ( int i : nums1) {
hashSet. add ( i) ;
}
for ( int i : nums2) {
hashSet2. add ( i) ;
}
int temp = nums1. length> nums2. length? nums1. length: nums2. length;
int [ ] arr = new int [ temp] ;
int len = 0 ;
for ( int i : hashSet) {
if ( hashSet2. contains ( i) ) {
arr[ len++ ] = i;
}
}
int [ ] res = new int [ len] ;
for ( int i = 0 ; i< len; i++ ) {
res[ i] = arr[ i] ;
}
return res;
}
}
数组方式实现的代码:
class Solution {
public int [ ] intersection ( int [ ] nums1, int [ ] nums2) {
int [ ] arr = new int [ 1001 ] ;
for ( int i = 0 ; i< nums1. length; i++ ) {
arr[ nums1[ i] ] = 1 ;
}
HashSet < Integer > hashSet = new HashSet < > ( ) ;
for ( int i : nums2) {
if ( arr[ i] == 1 ) {
hashSet. add ( i) ;
}
}
return hashSet. stream ( ) . mapToInt ( Integer :: intValue ) . toArray ( ) ;
}
}
202. 快乐数
题目链接: 自己思路:无 看文章的思路:本质上还是要找到规律:终止条件是当之前的数字再次出现了,表示即将进入死循环,就可以停止了。 代码
class Solution {
public boolean isHappy ( int n) {
HashSet < Integer > set = new HashSet < > ( ) ;
int nextNum = n;
set. add ( nextNum) ;
while ( true ) {
nextNum = calcNextNum ( nextNum) ;
if ( nextNum== 1 ) {
return true ;
} else if ( set. contains ( nextNum) ) {
return false ;
} else {
set. add ( nextNum) ;
}
}
}
public int calcNextNum ( int n) {
int res = 0 ;
String nstr = "" + n;
char [ ] arr = nstr. toCharArray ( ) ;
for ( int i = 0 ; i< arr. length; i++ ) {
int val = Integer . valueOf ( arr[ i] ) - 48 ;
res += val* val;
}
return res;
}
}
1. 两数之和
题目链接:https://leetcode.cn/problems/two-sum/description/ 第一眼的想法:两层for循环 第二眼的想法:无 暴力代码:
class Solution {
public int [ ] twoSum ( int [ ] nums, int target) {
int [ ] arr = new int [ 2 ] ;
for ( int i = 0 ; i< nums. length- 1 ; i++ ) {
for ( int j = i+ 1 ; j< nums. length; j++ ) {
if ( nums[ i] + nums[ j] == target) {
arr[ 0 ] = i;
arr[ 1 ] = j;
return arr;
}
}
}
return null ;
}
}
哈希法,将遍历过的数字都保存下来,如果后面遇到需要的数字去map里找,找到就直接返回下标数组。 哈希法代码实现:
class Solution {
public int [ ] twoSum ( int [ ] nums, int target) {
HashMap < Integer , Integer > map = new HashMap < > ( ) ;
for ( int i = 0 ; i< nums. length; i++ ) {
int val = target- nums[ i] ;
if ( map. containsKey ( val) ) {
int [ ] arr = new int [ 2 ] ;
arr[ 0 ] = map. get ( val) ;
arr[ 1 ] = i;
return arr;
} else {
map. put ( nums[ i] , i) ;
}
}
return null ;
}
}