599. 两个列表的最小索引总和

  1. public class Solution {
  2. public String[] findRestaurant(String[] list1, String[] list2) {
  3. //
  4. HashMap < Integer, List < String >> map = new HashMap < > ();
  5. for (int i = 0; i < list1.length; i++) {
  6. for (int j = 0; j < list2.length; j++) {
  7. if (list1[i].equals(list2[j])) {
  8. if (!map.containsKey(i + j))
  9. map.put(i + j, new ArrayList < String > ());
  10. map.get(i + j).add(list1[i]);
  11. }
  12. }
  13. }
  14. int min_index_sum = Integer.MAX_VALUE;
  15. for (int key: map.keySet())
  16. min_index_sum = Math.min(min_index_sum, key);
  17. String[] res = new String[map.get(min_index_sum).size()];
  18. return map.get(min_index_sum).toArray(res);
  19. }
  20. }

627. 变更性别

# Write your MySQL query statement below
update salary set sex = 
case sex
when 'm' then 'f'
else 'm' end;

628. 三个数的最大乘积

class Solution {
    public int maximumProduct(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        // 结合是否有负数的情况分析
        return Math.max(nums[0] * nums[1] * nums[len - 1], nums[len - 1] * nums[len - 2] * nums[len - 3]);
    }
}

643. 子数组最大平均数 I

// 时间超限
class Solution {
    public double findMaxAverage(int[] nums, int k) {

        double result = Integer.MIN_VALUE;

        for (int i = 0; i <= nums.length - k; i++) {
            double sum = 0;
            for (int j = i + k - 1; j >= i; j--) {
                sum += nums[j];
            }
            if (result < (sum / k)){
                result = sum / k;
            }
        }

        return result;
    }
}

class Solution {
    public double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        int n = nums.length;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int maxSum = sum;
        for (int i = k; i < n; i++) {
            sum = sum - nums[i - k] + nums[i];
            maxSum = Math.max(maxSum, sum);
        }
        return 1.0 * maxSum / k;
    }
}

645. 错误的集合

public class Solution {
    public int[] findErrorNums(int[] nums) {
        int dup = -1, missing = -1;
        for (int i = 1; i <= nums.length; i++) {
            int count = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] == i)
                    count++;
            }
            // 为2说明找到了出错的数据
            if (count == 2)
                dup = i;
            // 为0就说明找到了丢失的数据  正常情况下count应该为1
            else if (count == 0)
                missing = i;
        }
        return new int[] {dup, missing};
    }
}

657. 机器人能否返回原点

class Solution {
    public boolean judgeCircle(String moves) {
        int u = countSteps(moves, 'U');
        int d = countSteps(moves, 'D');
        int l = countSteps(moves, 'L');
        int r = countSteps(moves, 'R');
        return u == d && l == r;
    }

    public int countSteps(String moves,char step){
        int count = 0;
        char[] chars = moves.toCharArray();
        for (char aChar : chars) {
            if (aChar == step){
                count++;
            }
        }
        return count;
    }
}

661. 图片平滑器

class Solution {
    public int[][] imageSmoother(int[][] M) {
        int R = M.length, C = M[0].length;
        int[][] ans = new int[R][C];

        for (int r = 0; r < R; ++r)
            for (int c = 0; c < C; ++c) {
                int count = 0;
                for (int nr = r-1; nr <= r+1; ++nr)
                    for (int nc = c-1; nc <= c+1; ++nc) {
                        if (0 <= nr && nr < R && 0 <= nc && nc < C) {
                            ans[r][c] += M[nr][nc];
                            count++;
                        }
                    }
                ans[r][c] /= count;
            }
        return ans;
    }
}

674. 最长连续递增序列

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int result = 1;
        int count = 1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i + 1] > nums[i]){
                count++;
            }else {
                if (count > result){
                    result = count;
                }
                count = 1;
            }
            if (count > result){
                    result = count;
            }
        }
        return result;
    }
}

682. 棒球比赛

class Solution {
    public int calPoints(String[] ops) {
        List<Integer> arr = new ArrayList<>();
        for (int i = 0; i < ops.length; i++) {
            switch (ops[i]){
                case "D":
                    arr.add(arr.get(arr.size() - 1) * 2);
                    break;
                case "C":
                    arr.remove(arr.size() - 1);
                    break;
                case "+":
                    arr.add(arr.get(arr.size() - 1) + arr.get(arr.size() - 2));
                    break;
                default:
                    arr.add(Integer.parseInt(ops[i]));
                    break;
            }
        }
        int result = 0;
        for (int i = 0; i < arr.size(); i++) {
            result += arr.get(i);
        }
        return result;
    }
}

690. 员工的重要性

class Solution {
    Map<Integer, Employee> emap;
    public int getImportance(List<Employee> employees, int queryid) {
        emap = new HashMap();
        for (Employee e: employees) emap.put(e.id, e);
        return dfs(queryid);
    }
    public int dfs(int eid) {
        Employee employee = emap.get(eid);
        int ans = employee.importance;
        for (Integer subid: employee.subordinates)
            ans += dfs(subid);
        return ans;
    }
}

693. 交替位二进制数

class Solution {
    public boolean hasAlternatingBits(int n) {
        String s = Integer.toBinaryString(n);
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length - 1; i++) {
            if (chars[i] == chars[i + 1]){
                return false;
            }
        }
        return true;
    }
}

696. 计数二进制子串

class Solution {
    public int countBinarySubstrings(String s) {
        List<Integer> counts = new ArrayList<Integer>();
        int ptr = 0, n = s.length();
        while (ptr < n) {
            char c = s.charAt(ptr);
            int count = 0;
            while (ptr < n && s.charAt(ptr) == c) {
                ++ptr;
                ++count;
            }
            counts.add(count);
        }

        int result = 0;
        for (int i = 0; i < counts.size() - 1; i++) {
            result += Math.min(counts.get(i),counts.get(i + 1));
        }
        return result;
    }
}

697. 数组的度

class Solution {
    public int findShortestSubArray(int[] nums) {
        // 为防止原数组被破坏,copy一个数组进行map处理
        int [] copy = Arrays.copyOf(nums,nums.length);
        Map<Integer, Integer> du = findDu(copy);
        int result = Integer.MAX_VALUE;
        int first = 0;
        int last = 0;
        Set<Map.Entry<Integer, Integer>> entries = du.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            // 找到开始位置的下标
            for (int i = 0; i < nums.length; i++) {
                if (entry.getKey() == nums[i]){
                    first = i;
                    break;
                }
            }
            // 找到结束位置的下标
            for (int i = nums.length - 1; i > 0; i--) {
                if (entry.getKey() == nums[i]){
                    last = i;
                    break;
                }
            }
            result = Math.min((last - first + 1), result);
        }
        return result;
    }

    public Map<Integer,Integer> findDu(int[] nums){
        // map保存  key为数字的值,value为度
        Map<Integer,Integer> map = new HashMap<>();
        int count = 1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == Integer.MIN_VALUE){
                continue;
            }
            int key = nums[i];
            int value = 1;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] == nums[j]){
                    // 避免重复
                    nums[j] = Integer.MIN_VALUE;
                    value++;
                }
            }
            // 如果value的值比count大,就说明已经找到了最大的度,清空map存入最大值
            if (value > count){
                count = value;
                map = new HashMap<>();
                map.put(key,value);
                // 如果value == count,说明有两个最大值存在,直接加入到map中
            }else if (value == count){
                map.put(key,value);
            }
        }
        return map;
    }
}