add
This commit is contained in:
parent
8ff7654633
commit
ed167e9058
40
5.leetcode/src/com/fanxb/common/Q169.java
Normal file
40
5.leetcode/src/com/fanxb/common/Q169.java
Normal file
@ -0,0 +1,40 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Q169 {
|
||||
|
||||
public int majorityElement(int[] nums) {
|
||||
/**
|
||||
* Moore voting
|
||||
* Suppose the first num is target num x,and set count as 1
|
||||
* And then iterate through the array from second num
|
||||
* if( count == 0 ) x = current num
|
||||
* continue
|
||||
* if x == current num count++ else count--
|
||||
*/
|
||||
|
||||
|
||||
int x = nums[0], count = 1;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (count == 0) {
|
||||
x = nums[i];
|
||||
count = 1;
|
||||
} else if (x == nums[i]) {
|
||||
count++;
|
||||
} else {
|
||||
count--;
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = {10, 9, 9, 9, 10};
|
||||
System.out.println(new Q169().majorityElement(numbers));
|
||||
}
|
||||
}
|
79
5.leetcode/src/com/fanxb/common/Q189.java
Normal file
79
5.leetcode/src/com/fanxb/common/Q189.java
Normal file
@ -0,0 +1,79 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Q189 {
|
||||
|
||||
/**
|
||||
* set n = nums.length
|
||||
* first solution:
|
||||
* create a new array
|
||||
* copy origin array from nums[n-k] to nums[n-1]
|
||||
* copy origin array from nums[0] to nums[n-k-1]
|
||||
* second solution:
|
||||
* move every num to right k
|
||||
* third solution:
|
||||
*
|
||||
* @param nums
|
||||
* @param k
|
||||
*/
|
||||
public void rotate1(int[] nums, int k) {
|
||||
int n = nums.length;
|
||||
k = k % n;
|
||||
int[] res = new int[nums.length];
|
||||
int j = 0;
|
||||
for (int i = n - k; i < n; i++) {
|
||||
res[j++] = nums[i];
|
||||
}
|
||||
for (int i = 0; i < n - k; i++) {
|
||||
res[j++] = nums[i];
|
||||
}
|
||||
System.arraycopy(res, 0, nums, 0, n);
|
||||
}
|
||||
|
||||
/**
|
||||
* don't create a new array.
|
||||
* move num group
|
||||
* group 0: index: 0 0+k 0+2k 0+3k ...
|
||||
* group 1: index: 1 1+k 1+2k 1+3k ...
|
||||
* ...
|
||||
* group i: index: i i+k i+2k i+3k ...
|
||||
* ...
|
||||
* group k-1: index: k-1 k-1+k k-1 +2k k-1+3k ...
|
||||
*
|
||||
* @param nums
|
||||
* @param k
|
||||
*/
|
||||
public void rotate2(int[] nums, int k) {
|
||||
int n = nums.length;
|
||||
k = k % n;
|
||||
if (n < 2 || k == 0) {
|
||||
return;
|
||||
}
|
||||
int count = 0, start = 0;
|
||||
int curVal = nums[0], nextVal, curIndex = 0, nextIndex;
|
||||
while (count < n) {
|
||||
do {
|
||||
nextIndex = (curIndex + k) % n;
|
||||
nextVal = nums[nextIndex];
|
||||
nums[nextIndex] = curVal;
|
||||
curIndex = nextIndex;
|
||||
curVal = nextVal;
|
||||
count++;
|
||||
} while (curIndex != start);
|
||||
start++;
|
||||
curVal = nums[start];
|
||||
curIndex = start;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = {1, 2};
|
||||
new Q189().rotate2(numbers, 2);
|
||||
System.out.println(Arrays.toString(numbers));
|
||||
}
|
||||
}
|
57
5.leetcode/src/com/fanxb/common/Q80.java
Normal file
57
5.leetcode/src/com/fanxb/common/Q80.java
Normal file
@ -0,0 +1,57 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q80 {
|
||||
|
||||
public int removeDuplicates(int[] nums) {
|
||||
if (nums.length <= 2) {
|
||||
return nums.length;
|
||||
}
|
||||
//当前元素重复出现的次数
|
||||
int count = 0;
|
||||
//保留的元素下标
|
||||
int index = 0;
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (nums[i] == nums[i - 1]) {
|
||||
count++;
|
||||
if (count < 2) {
|
||||
nums[++index] = nums[i];
|
||||
}
|
||||
} else {
|
||||
count = 0;
|
||||
nums[++index] = nums[i];
|
||||
}
|
||||
}
|
||||
return index + 1;
|
||||
}
|
||||
|
||||
public int better(int[] nums) {
|
||||
if (nums.length <= 2) {
|
||||
return nums.length;
|
||||
}
|
||||
int slow = 2, fast = 2;
|
||||
int n = nums.length;
|
||||
while (fast < n) {
|
||||
if (nums[slow - 2] != nums[fast]) {
|
||||
nums[slow++] = nums[fast];
|
||||
}
|
||||
fast++;
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3};
|
||||
System.out.println(new Q80().better(nums));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user