判定是否互为字符重排
题目
给定两个字符串 s1
和 s2
,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
1 2
| 输入: s1 = "abc", s2 = "bca" 输出: true
|
1 2
| 输入: s1 = "abc", s2 = "bad" 输出: false
|
1 2
| 0 <= len(s1) <= 100 0 <= len(s2) <= 100
|
解题思路一
我们要处理的是字符串,那我们对字符串取出char,对char进行排序再
代码实现
1 2 3 4 5 6 7 8 9 10 11
| public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<nums.length;i++){ int complement = target - nums[i]; if(map.containsKey(complement)){ return new int[]{map.get(complement),i}; } map.put(nums[i],i); } return null; }
|
性能分析
一、执行用时分布图表
二、执行消耗内存分布图表