From d4754ba141cfce87e60b214f245df182919ef0c9 Mon Sep 17 00:00:00 2001 From: "xuemin.zhu" Date: Sat, 8 Jun 2019 10:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_01/id_35/LeetCode_189_35.java | 47 ++++++++++++++++++++++++++++++ Week_01/id_35/LeetCode_26_35.java | 33 +++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Week_01/id_35/LeetCode_189_35.java create mode 100644 Week_01/id_35/LeetCode_26_35.java diff --git a/Week_01/id_35/LeetCode_189_35.java b/Week_01/id_35/LeetCode_189_35.java new file mode 100644 index 00000000..fb696710 --- /dev/null +++ b/Week_01/id_35/LeetCode_189_35.java @@ -0,0 +1,47 @@ +package com.leecode; + +import java.util.Arrays; + +/** + * https://leetcode.com/problems/rotate-array/ + * + * Input: [1,2,3,4,5,6,7] and k = 3 + * Output: [5,6,7,1,2,3,4] + * Explanation: + * rotate 1 steps to the right: [7,1,2,3,4,5,6] + * rotate 2 steps to the right: [6,7,1,2,3,4,5] + * rotate 3 steps to the right: [5,6,7,1,2,3,4] + * + */ +public class LeetCode_189_35 { + public static void main(String[] args) { + + //考虑先移动一次的情况 + int nums[]={1,2,3,4,5}; + + LeetCode_189_35 p189=new LeetCode_189_35(); + p189.rotate(nums,3); + //p189.moveOnece(nums); + + System.out.println("最后结果:\n"+ Arrays.toString(nums)); + } + + public void rotate(int[] nums, int k) { + k%=nums.length; + for(int i=0;i0;i--){ + nums[i]=nums[i-1]; + } + nums[0]=lastNum; + } +} diff --git a/Week_01/id_35/LeetCode_26_35.java b/Week_01/id_35/LeetCode_26_35.java new file mode 100644 index 00000000..9685e889 --- /dev/null +++ b/Week_01/id_35/LeetCode_26_35.java @@ -0,0 +1,33 @@ +package com.leecode; + +import java.util.Arrays; + +/** + * https://leetcode.com/problems/remove-duplicates-from-sorted-array/ + */ +public class LeetCode_26_35 { + + public static void main(String[] args) { + //int[] nums={0,0,1,1,1,2,2,3,3,4}; + //int[] nums={1,1}; + int[] nums={1,2,2}; + System.out.println("原始数据:\n"+Arrays.toString(nums)); + System.out.println(removeDuplicates(nums)); + System.out.println("最后结果:\n"+Arrays.toString(nums)); + } + + public static int removeDuplicates(int[] nums) { + if(nums.length==0){ + return 0; + } + int index=0; + for(int i=1;i