力扣---2020.2.25

169. 多数元素

//学习一下别人的,太巧妙了
class Solution {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }
}
//HashMap
public int majorityElement(int[] nums) {
    Map<Integer,Integer> map = new HashMap<>() ;
    for(int i=0;i<nums.length;i++){
        if(map.containsKey(nums[i])){
            map.put(nums[i],map.get(nums[i])+1) ;
        }else{
            map.put(nums[i],1);
        }
    }
    Set<Integer> set = map.keySet() ;
    int result = 0 ;
    for(int item:set){
        if(map.get(item)>nums.length/2){
            result = item ;
        }
    }
    return result ;
}
class Solution {
    public int majorityElement(int[] nums) {
        Map<Integer, Integer> counter = new HashMap<>();
        // 遍历每个数统计次数
        for (int num: nums) {
            counter.put(num, counter.getOrDefault(num, 0) + 1);
            // 如果某个数次数超过了n/2就返回
            if (counter.get(num) > nums.length / 2) {
                return num;
            }
        }
        return -1;
    }
}
//摩尔投票法
class Solution {
    public int majorityElement(int[] nums) {
        int count = 0;
        Integer candidate = null;

        for (int num : nums) {
            if (count == 0) {
                candidate = num;
            }
            count += (num == candidate) ? 1 : -1;
        }

        return candidate;
    }
}

49. 字母异位词分组

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
         //返回值是List List里面是List 装的String  定义一个HashMap 值为List
        Map<String, List> map = new HashMap<String, List>();
        for (String i : strs) {
            //将字符串转换成数组
            char[] arr = i.toCharArray();
            //将字符数组排序 eg:"tea" -> "aet"
            Arrays.sort(arr);
            //再次将arr化为String
            String str = String.valueOf(arr);
            if (!map.containsKey(str)) {
                //若不存在建立映射关系 排序后的字符串—>新的List集合(装未排序的异位词)
                map.put(str, new ArrayList());
            }
            //建立映射关系户后添加 以及存在映射关系后添加单词
            map.get(str).add(i);
        }
        //返回值是List集合 通过构造器 构造一个包含指定 collection 的元素的列表
        return new ArrayList(map.values());
    }
}
//按计数分类
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) return new ArrayList();
        Map<String, List> ans = new HashMap<String, List>();
        int[] count = new int[26];
        for (String s : strs) {
            Arrays.fill(count, 0);
            for (char c : s.toCharArray()) count[c - 'a']++;

            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < 26; i++) {
                sb.append('#');
                sb.append(count[i]);
            }
            String key = sb.toString();
            if (!ans.containsKey(key)) ans.put(key, new ArrayList());
            ans.get(key).add(s);
        }
        return new ArrayList(ans.values());
    }
}

283. 移动零

class Solution {
    public void moveZeroes(int[] nums) {
        if(nums==null)  return;
        int j = 0;
        for(int i=0;i < nums.length;i++){
            if(nums[i] != 0){
                nums[j++] = nums[i];
            }
        }

        for (int i = j; i < nums.length; i++) {
            nums[i] = 0;
        }
    }
}
//最优解法
class Solution {
    public void moveZeroes(int[] nums) {
        for(int i=0,count=0;i<nums.length;i++){
            if(nums[i] != 0){
                //执行替换操作
                if(count != i){
                    nums[count] = nums[i];
                    nums[i] = 0;
                }
                count++;
            }
        }
    }
}
//统计0的个数,如果count大于0,将非0元素直接移到当前位置减去0元素个数的位置上,将当前元素用0填充。
class Solution {
    public void moveZeroes(int[] nums) {
        int count = 0;
        for(int i = 0 ; i < nums.length ; i++){
            if(nums[i] == 0)
                count++;
            else if(count >0){
                nums[i-count] = nums[i];
                nums[i] = 0;
            }
        }
    }
}

你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步

你可能感兴趣的:(数据结构与算法)