leetcode-数组相关的问题1

面试题 08.03. 魔术索引

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

870. 优势洗牌(田忌赛马的问题)

    public static int[] advantageCount1(int[] a, int[] b) {
        //定义一个list
        ArrayList list = new ArrayList<>();
        for (int i = 0; i < a.length; i++) {
            list.add(a[i]);
        }
        //排序
        Collections.sort(list);
        int[] result = new int[b.length];
        for (int i = 0; i < b.length; i++) {
            int flag = 0;
            //找到list中的比b[i]中大的
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j) > b[i]) {
                    flag = 1;
                    result[i] = list.get(j);
                    list.remove(j);
                    break;
                }
            }
            if (flag == 0) {
                result[i] = Integer.MAX_VALUE;
            }
        }
        //将剩下的元素的插入里面
        for (int i = 0; i < result.length; i++) {
            if (result[i] == Integer.MAX_VALUE) {
                result[i] = list.get(0);
                list.remove(0);
            }
        }
        return result;
    }

565. 数组嵌套

/**
 * Copyright (C), 2018-2020
 * FileName: arrayNesting565
 * Author:   xjl
 * Date:     2020/7/31 20:29
 * Description: 数组的嵌套
 */
package Array;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class arrayNesting565 {
    /**
     * 可以求解,但是这是暴力的方法
     *
     * @param nums
     * @return
     */
    public static int arrayNesting(int[] nums) {
        //创建新的Hashmap中
        //存放结果
        Map map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            Set set = new HashSet<>();
            int index = i;
            while (true) {
                if (!set.contains(map.get(index))) {
                    set.add(map.get(index));
                    index = map.get(index);
                } else {
                    break;
                }
            }
            if (result < set.size()) {
                result = set.size();
            }
        }
        return result;
    }

    /**
     * 时间复杂度为n
     * @param nums
     * @return
     */
    public static int arrayNesting2(int[] nums) {
        boolean[] visited = new boolean[nums.length];
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (!visited[i]) {
                int start = nums[i], count = 0;
                do {
                    start = nums[start];
                    count++;
                    visited[start] = true;
                }
                while (start != nums[i]);
                res = Math.max(res, count);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] A = {0, 2, 1};
        int i = arrayNesting(A);
        System.out.println(i);
    }
}
v

954. 二倍数对数组

 

1488. 避免洪水泛滥

 

667. 优美的排列 II

 

1375. 灯泡开关 III

 

1177. 构建回文串检测

 

1437. 是否所有 1 都至少相隔 k 个元素

 

1386. 安排电影院座位

 

 

 

 

你可能感兴趣的:(Leetcode,算法)