1.
//回溯法
public int maxProduct(int[] nums) {
max = nums[0];
backtracking(nums,0);
return max;
}
public void backtracking(int[] nums , int startIndex){
if (startIndex == nums.length){
return;
}
int tmp = 1;
for (int i = startIndex; i < nums.length; i++) {
tmp = tmp * nums[i];
if (max < tmp){
max = tmp;
}
}
backtracking(nums,startIndex+1);
}
官方解法:
int maxF = nums[0], minF = nums[0], ans = nums[0];
int length = nums.length;
for (int i = 1; i < length; ++i) {
int mx = maxF, mn = minF;
maxF = Math.max(mx * nums[i], Math.max(nums[i], mn * nums[i]));
minF = Math.min(mn * nums[i], Math.min(nums[i], mx * nums[i]));
ans = Math.max(maxF, ans);
}
return ans;
2.
public int majorityElement(int[] nums) {
int count = 1;
int majority = nums[0];
for(int i = 1; i < nums.length;i++){
count = majority == nums[i]?count+1:count - 1;
if(count == 0){
majority = nums[i];
count = 1;
}
}
return majority;
}
3.
public void rotate(int[] nums, int k) {
if (k % nums.length == 0){
return;
}
//第一种
int n = nums.length;
int[] newArr = new int[n];
for (int i = 0; i < n; i++) {
newArr[(i + k) % n] = nums[i];
}
System.arraycopy(newArr,0,nums,0,n);
//第二种
int n = nums.length;
int count = gcd(k,n);
for (int start = 0; start < count; start++) {
int current = start;
int prev = nums[start];
do {
int next = (current + k) % n;
int tmp = nums[next];
nums[next] = prev;
current = next;
prev = tmp;
}while (start != current);
}
//第三种
int n = nums.length;
reverse(nums,0,n-1);
reverse(nums,0,k%n-1);
reverse(nums,k%n,n-1);
}
public int gcd(int x ,int y){
return y > 0 ? gcd(y,x%y):x;
}
public void reverse(int[] nums , int start , int end){
while (start < end){
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
4.
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])){
return true;
}
}
return false;
}
5.
public void moveZeroes(int[] nums) {
int count = 0;
int startIndex = 0;
int endIndex = 0;
while (startIndex < nums.length && endIndex < nums.length){
if (nums[endIndex] == 0){
endIndex++;
count++;
}else {
if (startIndex != endIndex){
nums[startIndex] = nums[endIndex];
}
startIndex++;
endIndex++;
}
}
for (int i = nums.length - 1; i >= nums.length - count; i--) {
nums[i] = 0;
}
}
6.
class Solution {
private int[] original;
private int[] array;
Random random = new Random();
public Solution(int[] nums) {
array = nums;
original = nums.clone();
}
/** Resets the array to its original configuration and return it. */
public int[] reset() {
array = original;
original = original.clone();
return original;
}
/** Returns a random shuffling of the array. */
public int[] shuffle() {
for (int i = 0; i < array.length; i++) {
swap(i,random.nextInt(array.length - i) + i);
}
return array;
}
private void swap(int i , int j){
int tmp;
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
7.
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer , Integer> map1 = new HashMap<>();
HashMap<Integer , Integer> map2 = new HashMap<>();
HashMap<Integer , Integer> map3 = new HashMap<>();
for (int i : nums1) {
if (map1.containsKey(i)){
map1.put(i,map1.get(i).intValue()+1);
}else {
map1.put(i,1);
}
}
for (int i : nums2) {
if (map2.containsKey(i)){
map2.put(i,map2.get(i).intValue()+1);
}else {
map2.put(i,1);
}
}
for (Integer integer : map1.keySet()) {
if (map2.containsKey(integer)){
if (map1.get(integer).intValue() > map2.get(integer).intValue()){
map3.put(integer,map2.get(integer).intValue());
}else {
map3.put(integer,map1.get(integer).intValue());
}
}
}
ArrayList<Integer> list = new ArrayList<>();
for (Integer integer : map3.keySet()) {
for (int i = 0; i < map3.get(integer).intValue(); i++) {
list.add(integer);
}
}
int[] max = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
max[i] = list.get(i);
}
return max;
}
或者
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
return intersect(nums2, nums1);
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int num : nums1) {
int count = map.getOrDefault(num, 0) + 1;
map.put(num, count);
}
int[] intersection = new int[nums1.length];
int index = 0;
for (int num : nums2) {
int count = map.getOrDefault(num, 0);
if (count > 0) {
intersection[index++] = num;
count--;
if (count > 0) {
map.put(num, count);
} else {
map.remove(num);
}
}
}
return Arrays.copyOfRange(intersection, 0, index);
}
8.
public boolean increasingTriplet(int[] nums) {
int one = Integer.MAX_VALUE;
int two = Integer.MAX_VALUE;
for (int three : nums) {
if (three > two){
return true;
}else if (three <= one){
one = three;
}else {
two = three;
}
}
return false;
}
9.
public boolean searchMatrix(int[][] matrix, int target) {
int row = matrix.length - 1;
int col = 0;
while(row >= 0 && col <= matrix[0].length - 1){
if(matrix[row][col] > target){
row--;
}else if(matrix[row][col] < target){
col++;
}else{
return true;
}
}
return false;
}
10.
public int[] productExceptSelf(int[] nums) {
int length = nums.length;
int[] answer = new int[length];
answer[0] = 1;
//右边乘积
for (int i = 1; i < length; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}
int r = 1;
//左边乘积
for (int i = length - 1; i >= 0; i--) {
answer[i] = answer[i] * r;
r = r * nums[i];
}
return answer;
}