Skip to content

Commit

Permalink
Merge pull request algorithm007-class01#70 from cxl123156/master
Browse files Browse the repository at this point in the history
0257_Week01(java)
  • Loading branch information
melody-li authored Mar 16, 2020
2 parents f02e48b + 3b2f39d commit 4c4181d
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 2 deletions.
89 changes: 89 additions & 0 deletions Week_01/G20200343040257/LeetCode_189_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

public class RotateArray {
/**
* 解法1
* @param nums 数组
* @param k 旋转次数
*/
public void rotate1(int[] nums, int k) {
int endIndex = nums.length - 1;
int prev;
int temp;
while (k>0){
prev = nums[0];
nums[0] = nums[endIndex];
for (int i = 1; i < nums.length; i++) {
temp = nums[i];
nums[i] = prev;
prev = temp;
}
k--;
}
}

/**
* 解法1 优化
* @param nums 数组
* @param k 旋转次数
*/
public void rotateOptimised1(int[] nums, int k) {
int endIndex = nums.length - 1;
int prev;
int temp;
int length = nums.length;
int rotateTimes = k;
if(k == length || k ==0){
return;
}
if(k > length){
rotateTimes = k%length;
}
while (rotateTimes>0){
prev = nums[0];
nums[0] = nums[endIndex];
for (int i = 1; i < nums.length; i++) {
temp = nums[i];
nums[i] = prev;
prev = temp;
}
rotateTimes--;
}
}

/**
* 解法2 翻转法
* @param nums 数组
* @param k 旋转次数
*/
public static void rotate2(int[] nums, int k){
int length = nums.length;
int rotateTimes = k;
if(k == length || k ==0){
return;
}
if(k > length){
rotateTimes = k%length;
}
reserve(nums,0,length-1);
reserve(nums,0,rotateTimes-1);
reserve(nums,rotateTimes,length-1);
}

/**
* 翻转数组
* @param nums 数组
* @param start 起始索引下标
* @param end 结束索引下标
*/
private static void reserve(int[] nums,int start,int end){
int temp;
while (start<end){
temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}

}
116 changes: 116 additions & 0 deletions Week_01/G20200343040257/LeetCode_1_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* LeetCode 两数之和
* https://leetcode-cn.com/problems/two-sum/
* <p>
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
* <p>
* 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
* <p>
* 示例:
* <p>
* 给定 nums = [2, 7, 11, 15], target = 9
* <p>
* 因为 nums[0] + nums[1] = 2 + 7 = 9
* 所以返回 [0, 1]
*/
public class LeetCode_1_TwoSum {

/**
* LeetCode 两数之和 暴力解法
*
* @param nums 输入数组
* @param target 目标和
* @return int[] 数组下标
*/
public int[] twoSum1(int[] nums, int target) {
int[] resultArray = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i != j) {
int result = nums[i] + nums[j];
if (result == target) {
resultArray[0] = i;
resultArray[1] = j;
return resultArray;
}
}
}
}
return resultArray;
}


/**
* Hash表缓存解法
*
* @param nums 输入数组
* @param target 目标和
* @return int[] 数组下标
*/
public int[] twoSum2(int[] nums, int target) {
Map<Integer, Integer> tempMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer tempIndex = tempMap.get(target - nums[i]);
if (tempIndex != null) {
return new int[]{tempIndex, i};
}
tempMap.put(nums[i], i);
}
return null;
}

/**
* 双指针夹逼解法
* 由于需要返回的是数组下标,再找到符合条件的数后还需再查找数对应的下标
*
* @param nums 输入数组
* @param target 目标和
* @return int[] 数组下标
*/
public static int[] twoSum3(int[] nums, int target) {
int m = 0, n = 0, tempIndex = -1;
int[] temp = new int[nums.length];
int[] result = new int[]{-1, -1};
System.arraycopy(nums, 0, temp, 0, temp.length);
Arrays.sort(nums);
int i = 0, j = nums.length - 1;
while (i < j) {
if (nums[i] > Math.abs(target)) {
break;
}
if (nums[i] + nums[j] > target) {
j--;
}
if (nums[i] + nums[j] < target) {
i++;
}
if (nums[i] + nums[j] == target) {
m = nums[i];
n = nums[j];
break;
}
}

// 根据匹配到符合条件的元素m,n 再次遍历原数组寻找下标
for (int k = 0; k < temp.length; k++) {
if (result[0] != -1 & result[1] != -1) {
break;
}
if (temp[k] == m || temp[k] == n) {
if (result[0] == -1) {
result[0] = k;
} else {
result[1] = k;
}
}
}
return result;
}


}
15 changes: 15 additions & 0 deletions Week_01/G20200343040257/LeetCode_26_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

public class RemoveDuplicates {

public int removeDuplicates(int[] nums){
int i = 0;
for(int j=1;j<nums.length;j++){
if(nums[i]!=nums[j] && i+1!=j){
i++;
nums[i] = nums[j];
}
}
return i+1;
}

}
32 changes: 32 additions & 0 deletions Week_01/G20200343040257/LeetCode_283_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


/**
* 283. 移动零
* https://leetcode-cn.com/problems/move-zeroes/
*/
public class LeetCode_283_MoveZeroes {

public static void moveZeroes(int[] nums) {
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[index] = nums[i];
index++;
}
}
while (index < nums.length){
nums[index] = 0;
index++;
}
}

public static void main(String[] args) {
int[] arrays = {0,1,0,3,12};
moveZeroes(arrays);
for (int array : arrays) {
System.out.print(array);
System.out.print(" ");
}
}

}
49 changes: 49 additions & 0 deletions Week_01/G20200343040257/LeetCode_66_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@


/**
* 66. 加一
* https://leetcode-cn.com/problems/plus-one/
*/
public class LeetCode_66_PlusOne {

/**
* 解法 递归+1
*/
public int[] plusOne1(int[] digits) {
return addOne(digits,digits.length-1);
}

/**
* 如果当前数字小于9则加一返回,如果等于9则继续循环
* 对于99,999这种情况只需返回一个第一个元素为1其他元素均为0的新数组即可
*/
public int[] plusOne2(int[] digits){
for(int i=digits.length-1;i>=0;i--){
if(digits[i]<9){
digits[i]++;
return digits;
}
digits[i]=0;
}
int[] result = new int[digits.length+1];
result[0] = 1;
return result;
}

private int[] addOne(int[] nums,int index){
int result = nums[index] +1;
if (result == 10) {
if(index == 0){
int[] newNumber = new int[nums.length + 1];
newNumber[0] = 1;
return newNumber;
}
nums[index] = 0;
return addOne(nums,index-1);
}else {
nums[index] = result;
}
return nums;
}

}
22 changes: 22 additions & 0 deletions Week_01/G20200343040257/LeetCode_88_0257.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import java.util.Arrays;

/**
* 合并两个有序数组
* https://leetcode-cn.com/problems/merge-sorted-array/
*/
public class LeetCode_88_MergeSortedArray {

/**
* 先append到数组后面再对整个数组进行排序
*
*/
public void merge(int[] nums1, int m, int[] nums2, int n) {
for (int i : nums2) {
nums1[m] = i;
m++;
}
Arrays.sort(nums1);
}

}
35 changes: 34 additions & 1 deletion Week_01/G20200343040257/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
学习笔记
###学习笔记
数组:
插入O(n)
查找O(1)
删除O(n)

链表:
插入O(1)
查找O(n)
删除O(1)


跳表:
1. 对链表的元素查询进行优化;
2. 只能用于元素是有序的情况下;
3. 对标平衡树和二分查找 插入 删除 搜索 操作时间复杂度均为O(log n);
4. 应用于 redis LevelDB;

数组题目的解法思路:
1. 双指针夹逼
LeetCode题目:
[11.盛最多水的容器](https://leetcode-cn.com/problems/container-with-most-water/ "11.盛最多水的容器")
2. 快慢指针
LeetCode题目:
[283.移动零](https://leetcode-cn.com/problems/move-zeroes/ "283.移动零")
[26.删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ "26.删除排序数组中的重复项")


算法优化的方法:
1. 升维度 (一维升二维) 例如跳表
2. 空间换时间 例如解题中通过Map开辟新的内存空间缓存计算结果的方法来优化时间复杂度

存在的问题:
链表操作的代码写的不是很熟练,没有掌握到一个比较好的方法,还需多写代码练习。
26 changes: 25 additions & 1 deletion Week_预习/G20200343040257/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
学习笔记
##学习笔记

刷题步骤:
1. 5-10分钟最多15分钟的读题和思考时间。
2. 有思路的话自己开始做题写代码,没有思路马上看题解没必要死磕。
3. 通过背诵、默写熟练题目解法。
4. 闭卷自己写

刷题方法:五毒神掌
第一遍 按照刷题步骤做题
第二遍 尝试思考多种解法和优化方法
第三遍 24小时候重做该题
第四遍 一周后重做该题
第五遍 面试前过一遍

递归的复杂度分析:将递归的执行顺序按照树型结构画出来

主定理
![avatar](https://raw.githubusercontent.com/cxl123156/algorithm007-class01/master/Week_%E9%A2%84%E4%B9%A0/G20200343040257/%E4%B8%BB%E5%AE%9A%E7%90%86.PNG)

通过这次的预习课程学到了一些很有用的学习方法和道理比如五毒神掌,主定理,画脑图。之前每次都是看别人画好的脑图,这次自己画了一遍后体会到了脑图的强大之处,在画的过程中进行
通过查资料并结合自己之前的积累梳理相关知识点,起到了一个查漏补缺的作用,之前看别人的脑图看完基本和没看一样体会不到自己的痛点,自己画一遍的确加深了自己的印象同时也了解到自己对这些知识点的盲区,以便日后的针对练习。

脑图画的看起来不如别人全,因为在画图过程中发现自己有很多知识点不了解甚至第一次听说的情况比如布隆过滤器、动态规划,随着后面的学习再慢慢完善丰富吧。

Binary file added Week_预习/G20200343040257/主定理.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week_预习/G20200343040257/数据结构.xmind
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week_预习/G20200343040257/算法.xmind
Binary file not shown.
Binary file added Week_预习/G20200343040257/算法脑图.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c4181d

Please sign in to comment.