diff --git a/com/zerroi/leetcode/ThreeThree/First.java b/com/zerroi/leetcode/ThreeThree/First.java index 3f50693..0e405cc 100644 --- a/com/zerroi/leetcode/ThreeThree/First.java +++ b/com/zerroi/leetcode/ThreeThree/First.java @@ -4,7 +4,7 @@ public class First { } -class Solution { +class SolutionFirst { public String intToRoman(int num) { String[] a1 = new String[] { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; int[] a2 = new int[]{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; diff --git a/com/zerroi/leetcode/ThreeThree/Second.java b/com/zerroi/leetcode/ThreeThree/Second.java new file mode 100644 index 0000000..578b20c --- /dev/null +++ b/com/zerroi/leetcode/ThreeThree/Second.java @@ -0,0 +1,16 @@ +package com.zerroi.leetcode.ThreeThree; + +import java.util.ArrayList; +import java.util.List; + +public class Second { +} + +class SolutionSecond { + public List generateParenthesis(int n) { + List res = new ArrayList<>(); + + + return res; + } +} diff --git a/com/zerroi/leetcode/sort/BubbleSort.java b/com/zerroi/leetcode/sort/BubbleSort.java new file mode 100644 index 0000000..08a930b --- /dev/null +++ b/com/zerroi/leetcode/sort/BubbleSort.java @@ -0,0 +1,28 @@ +package com.zerroi.leetcode.sort; + +public class BubbleSort { + public static void main(String[] args) { + Bubble bubble = new Bubble(); + int[] res = bubble.bubble(new int[]{2, 1, 9, 3, 10}); + for (int item : res) { + System.out.println("item = " + item); + } + + } +} + +class Bubble { + public int[] bubble(int[] nums) { + for (int i = 0; i < nums.length - 1; i++) { + for (int j = 0; j < nums.length - i - 1; j++) { + int temp; + if (nums[j] > nums[j + 1]) { + temp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = temp; + } + } + } + return nums; + } +} diff --git a/com/zerroi/leetcode/sort/InsertionSort.java b/com/zerroi/leetcode/sort/InsertionSort.java new file mode 100644 index 0000000..1811f77 --- /dev/null +++ b/com/zerroi/leetcode/sort/InsertionSort.java @@ -0,0 +1,30 @@ +package com.zerroi.leetcode.sort; + +public class InsertionSort { + public static void main(String[] args) { + Insertion insertion = new Insertion(); + int[] ints = insertion.insertionSort(new int[]{2, 1, 9, 3, 10}); + for (int item : ints) { + System.out.println("item = " + item); + } + + } +} + +class Insertion { + public int[] insertionSort(int[] nums) { + for (int i = 1; i < nums.length; i++) { + int temp = nums[i]; + int j = i; + while (j > 0 && temp < nums[j - 1]) { + nums[j] = nums[j - 1]; + j--; + } + if (j != i) { + nums[j] = temp; + } + } + + return nums; + } +} diff --git a/com/zerroi/leetcode/sort/SelectionSort.java b/com/zerroi/leetcode/sort/SelectionSort.java new file mode 100644 index 0000000..0af71e5 --- /dev/null +++ b/com/zerroi/leetcode/sort/SelectionSort.java @@ -0,0 +1,35 @@ +package com.zerroi.leetcode.sort; + +/** + * 选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。 + * 唯一的好处可能就是不占用额外的内存空间了吧。 + * 1. 算法步骤 + * 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。 + * 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。 + * 重复第二步,直到所有元素均排序完毕。 + */ +public class SelectionSort { + public static void main(String[] args) { + Selection selection = new Selection(); + int[] res = selection.selection(new int[]{2, 1, 9, 3, 10}); + for (int re : res) { + System.out.println(re); + } + } +} + +class Selection { + public int[] selection(int[] nums) { + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + int temp = 0; + if (nums[i] > nums[j]) { + temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + } + } + return nums; + } +}