1.25编程练习题
import java.util.Scanner;
public class Demo82{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] arr = new int[101];
System.out.print("Enter numbers:");
while(true){
int num = input.nextInt();
if(num == 0){
break;
}else{
arr[num]++;
}
}
for(int i = 1; i< arr.length;i++){
if(arr[i] != 0){
if(arr[i] == 1){
System.out.println(i +" 出现"+arr[i]+ " time");
}else{
System.out.println(i + " 出现"+arr[i] + " times");
}
}
}
}
}
import java.util.*;
public class Demo83{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] arr = new int[0];
for(int i = 0; i < arr.length;i++){
int num = input.nextInt();
if(!arr.contains(num)){
arr = add(arr,num);
}
}
System.out.println(Arrays.toString(arr));
}
public static int[] add(int[] arr,int num){
int newArr[] = new int[arr.length+1];
for(int i = 0;i < arr.length;i++){
newArr[i] = arr[i];
}
newArr[newArr.length-1] = num;
return newArr;
}
}
public class Demo85{
public static void main(String[] args){
System.out.println(gcd(35,15,30,45,90));
}
public static int gcd(int...numbers){
int min = getMin(numbers);
for(int i = min;i>=1;i--){
boolean flag = true;
for(int j = 0;j < numbers.length;j++){
if(numbers[j] % i != 0){
flag = false;
break;
}
}
if(flag){
return i;
}
}
return 1;
}
public static int getMin(int[] numbers){
int min =numbers[0];
for(int i = 1;i <numbers.length;i++){
if(numbers[i] < min){
min = numbers[i];
}
}
return min;
}
}
import java.util.*;
public class Demo88{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.print("请输入球的个数:");
int balls = input.nextInt();
System.out.print("请输入槽子的个数:");
int slots = input.nextInt();
int[] temp = new int[slots];//槽子数;
String[] roads = new String[balls];
for(int i = 0;i < roads.length;i++){//遍历每个路径;
String str = "";
int count = 0;
for(int j = 0;j < slots -1;j++){//每个路径对应的每层的路径;
if(random.nextInt(2) % 2 == 0){
str = str + "L";
}else
{
str = str +"R";
count++;
}
}
roads[i] = str;//拼接字符串成一个球的路径;
System.out.println(roads[i]);
temp[count]++;
}
System.out.println(Arrays.toString(temp));
}
}
import java.util.Scanner;
public class Demo90{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入数字的个数:");
int nums = input.nextInt();
int[] arr = new int[nums];//提示输入;
for(int i = 0;i < arr.length;i++){
arr[i] = input.nextInt();
}//连续4个相等,随意i只能到达倒数第4个;
for(int i = 0;i<arr.length-3;i++){
int count = 1;
for(int j = i+1;j<arr.length;j++){
if(arr[i] == arr[j]){
count++;
}else{
i = j;//不等,则将从不等的数开始;
}
}
if(count >= 4){
System.out.println("有");
return;
}
}
System.out.println("没有");
}
}
1.26
力扣题练习:
(283)双指针:
class Solution {
public void moveZeroes(int[] nums) {
int k = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] != 0){
int temp = nums[i];
nums[i] = nums[k];
nums[k] = temp;
k++;
}
}
}
}
选择排序思路:
class Solution {
public void moveZeroes(int[] nums) {
for(int i = 0;i < nums.length-1;i++){
for(int j = i + 1;j<nums.length;j++){
if(nums[i] == 0 && nums[j] != 0){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
}
}
}
(169)
class Solution {
public int majorityElement(int[] nums) {
int n = nums[0];
int count = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] == n){
count++;
}else{
count--;
if(count == 0){
n = nums[i];
count = 1;
}
}
}
return n;
}
}
(905)插入排序思路:
class Solution {
public int[] sortArrayByParity(int[] A) {
for(int i = 1; i<A.length;i++){
int e = A[i];
int j = 0;
for(j = i; j>0&&(A[j-1] % 2 == 1);j--){
A[j] = A[j-1];
}
A[j] = e;
}
return A;
}
}
双指针:
class Solution {
public int[] sortArrayByParity(int[] A) {
int k = 0;
for(int i = 0; i < A.length;i++){
if(A[i] % 2 == 0){
int temp = A[k];
A[k] = A[i];
A[i] = temp;
k++;
}
}
return A;
}
}
第三种解法;
class Solution {
public int[] sortArrayByParity(int[] A) {
int l = 0;
int r = A.length-1;
while(l < r){
if(A[l] % 2 == 1 && A[r] % 2 == 0){
int temp = A[l];
A[l] = A[r];
A[r] = temp;
}else if(A[l] % 2 == 0 && A[r] % 2 == 1){
l++;
r--;
}else if(A[l] % 2 == 1 && A[r] % 2 == 1){
r--;
}else{
l++;
}
}
return A;
}
}
(1013)
class Solution {
public boolean canThreePartsEqualSum(int[] arr) {
int sum = 0;
for(int num : arr){
sum += num;
}
int key = sum / 3; //算出总值,求平均数;
int group = 0; //定义计数组;
for(int i = 0;i<arr.length;i++){
key -= arr[i]; //计算值为0则重新给key赋值,组数加加;
if(key == 0){
group++;
key = sum / 3;
}
}
return group == 3 || sum == 0 && group >= 3;//当总和为0,且分的组大于等于3,则也满足,例如4个,可以将2个0看成一个;构成3个组;
}
}
(724)
class Solution {
public int pivotIndex(int[] nums) {
int sum = 0;
for(int num : nums){
sum += num;
}
int lSum = 0;
int rSum = 0;
for(int i = 0;i <nums.length;i++){
if(i == 0){
lSum = 0;
}else{
lSum += nums[i-1];
}
rSum = sum - nums[i] - lSum;
if(rSum == lSum){
return i;
}
}
return -1;
}
}
(209)
class Solution {
public int minSubArrayLen(int s, int[] nums) {
int len = 0;
int i = 0;
int sum = 0;
for(int j = 0;j < nums.length;j++){
sum += nums[j];
while(sum >= s){
len = len == 0 ? j-i+1 : Math.min(j-i+1,len);
sum -= nums[i];
i++;
}
}
return len;
}
}