diff --git a/README.md b/README.md index 561c902..fd2f009 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,12 @@ Java练习算法代码(排序,数据结构,小算法练习题) ## 三、basic文件夹是基础相关 -Java简单的算法题,递归知识~ +Java简单的算法题,目前有20道 + +递归知识~ + +## 四、linkList文件夹是链表 + +## 五、queue文件夹是队列 + +## 六、statck文件夹是栈 diff --git a/src/LinkList/LinkListText.java b/src/LinkList/LinkListText.java index 14f1eff..02b2eff 100644 --- a/src/LinkList/LinkListText.java +++ b/src/LinkList/LinkListText.java @@ -25,7 +25,9 @@ public static void main(String[] args) { addData(32); addData(4); addData(3); + addData(5); addData(3); + addData(5); addData(2); traverse(head); @@ -60,7 +62,7 @@ public static void main(String[] args) { System.out.println("公众号:Java3y--------删除重复节点---------"); - deleteDuplecate(head); + delete_v2(head); traverse(head); System.out.println("公众号:Java3y--------删除重复节点---------"); @@ -162,6 +164,7 @@ public static void printListReversely(Node head) { * * @param head 头节点 */ +/* public static void deleteDuplecate(Node head) { //临时节点,(从首节点开始-->真正有数据的节点) @@ -170,7 +173,7 @@ public static void deleteDuplecate(Node head) { //当前节点(首节点)的下一个节点 Node nextNode = temp.next; - while (temp.next != null) { + while (temp != null) { while (nextNode.next != null) { @@ -192,7 +195,26 @@ public static void deleteDuplecate(Node head) { } +*/ + /** + * 参考博文:https://blog.csdn.net/ifollowrivers/article/details/70161447 + * 有三种解决方案 + * @param head + */ + public static void delete_v2(Node head){ + Node p=head; + while(p!=null){ + Node q=p; + while(q.next!=null){ + if(q.next.data==p.data){ + q.next=q.next.next; + }else + q=q.next; + } + p=p.next; + } + } /** * 遍历链表 * diff --git a/src/basic/ArithmeticOne.java b/src/basic/ArithmeticOne.java index 53aa9cc..5ae13ce 100644 --- a/src/basic/ArithmeticOne.java +++ b/src/basic/ArithmeticOne.java @@ -16,6 +16,8 @@ public static void main(String[] args) { //Factorial(3); + Factorial(7); + diff --git a/src/basic/ArithmeticTwo.java b/src/basic/ArithmeticTwo.java index 548f6f4..a31c6be 100644 --- a/src/basic/ArithmeticTwo.java +++ b/src/basic/ArithmeticTwo.java @@ -7,12 +7,218 @@ public class ArithmeticTwo { public static void main(String[] args) { + /* String roman = "XVIII"; - int[] arrays = {5, 5, 6, 4, 4, 6, 3}; + int num = romanToNumber(roman, 0, roman.length() - 1); - //singleNumber(arrays); + System.out.println("关注公众号:Java3y--------------->" + num);*/ - drawStar(); + + // beerAndDrink(); + +/* String s = "HELLO WORLD"; + char[] ch = new char[s.length()]; + + for (int i = 0; i < s.length(); i++) { + ch[i] = (char) encode(s.charAt(i), 3); + }*/ + + int gcd = gcd(35, 5); + + System.out.println("关注公众号:Java3y------------>" + gcd); + + + + + } + + + /** + * 求最大公约数 + * + * @param num1 + * @param num2 + */ + public static int gcd(int num1, int num2) { + + + // 求余数 + int r = num1 % num2; + + // 如果余数为0,那么除数就是最大公约数 + if (r == 0) { + return num2; + } else { + + // 否则,则用除数和余数来进行运算 + return gcd(num2, r); + } + + } + + /** + * 右移 + */ + public static int rotateRight(int ch) { + if (ch >= 'A' && ch <= 'Y') { + return ch + 1; + } else if (ch >= 'a' && ch <= 'y') { + return ch + 1; + } else if (ch == 'Z') { + return 'A'; + } else if (ch == 'z') { + return 'a'; + } else { + return ch; + } + } + + /** + * 左移 + */ + public static int rotateLeft(int ch) { + if (ch >= 'B' && ch <= 'Z') { + return ch - 1; + } else if (ch >= 'b' && ch <= 'z') { + return ch - 1; + } else if (ch == 'A') { + return 'Z'; + } else if (ch == 'a') { + return 'z'; + } else { + return ch; + } + } + + /** + * 加密 + * @param ch + * @param shift + * @return + */ + public static int encode(int ch, int shift) { + + // 如果没有移动,则直接返回 + if (shift == 0) { + return ch; + } else if (shift > 0) { + + // 如果shift移动的是正数,那么就向右移动 + for (int i = 0; i < shift; i++) { + ch = rotateRight(ch); + } + return ch; + } else { + + // 如果shift移动的是负数,那么就向左移动 + for (int i = 0; i < -shift; i++) { + ch = rotateLeft(ch); + } + return ch; + } + } + + + /** + * 啤酒与饮料题目 + */ + public static void beerAndDrink() { + + // 啤酒 + for (int i = 0; i < 36; i++) { + + // 饮料 + for (int j = 0; j < 44; j++) { + + // 钱刚好花光了,并且啤酒比饮料少 + if (2.3 * i + j * 1.9 == 82.3 && i < j) { + System.out.println("关注公众号:Java3y--------------->啤酒买了" + i); + } + } + } + } + + /** + * 将罗马数字转成阿拉伯数字,实际上就是一个查表的过程 + * - + * + * @param roman + * @return + */ + public static int digitsToValues(char roman) { + + // 定义罗马数字 + char digits[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'}; + + // 罗马数字对应的阿拉伯数字 + int values[] = {1, 5, 10, 50, 100, 500, 1000}; + + + for (int i = 0; i < digits.length; i++) { + if (digits[i] == roman) { + return values[i]; + } + } + + return 0; + + } + + /** + * 找到当前罗马数字最大值的角标 + * + * @param digits + * @return + */ + public static int findMaxIndex(String digits, int L, int R) { + + // 假设第一个是最大的 + int max = digitsToValues(digits.charAt(L)); + int maxIndex = L; + + for (int i = L; i < R; i++) { + // 将罗马数字转成是阿拉伯数字 + int num = digitsToValues(digits.charAt(i)); + if (max < num) { + max = num; + maxIndex = i; + } + } + + return maxIndex; + } + + + /** + * 将罗马数字转成阿拉伯数字 + * + * @param romanNumber + * @param L + * @param R + */ + public static int romanToNumber(String romanNumber, int L, int R) { + + // 如果只有一个罗马数字,那么可以直接返回了(递归出口) + if (L == R) { + return digitsToValues(romanNumber.charAt(L)); + } else if (L > R) { // 如果L和R已经越界了,那么说明没有值了 + return 0; + } else { + + // 找到当前罗马数字最大值的角标 + int maxIndex = findMaxIndex(romanNumber, L, R); + + // 得到最大值 + int max = digitsToValues(romanNumber.charAt(maxIndex)); + + // 在最大值左边的,则用最大值减去左边的 + int left = romanToNumber(romanNumber, L, maxIndex - 1); + + // 在最大值右边的,则用最大值加上右边的 + int right = romanToNumber(romanNumber, maxIndex + 1, R); + + return max - left + right; + } } diff --git a/src/sort/QuickSort.java b/src/sort/QuickSort.java index 1045d3e..f54fd60 100644 --- a/src/sort/QuickSort.java +++ b/src/sort/QuickSort.java @@ -10,9 +10,10 @@ public class QuickSort { public static void main(String[] args) { - int[] arr = {1, 4, 5, 67, 2, 7, 8, 6, 9, 44}; + //int[] arr = {1, 4, 5, 67, 2, 7, 8, 6, 9, 44, 34, 5, 5, 2, 34, 5, 62, 42, 1, 1324, 2346}; + int[] arr={23,34,33,56,45}; + quickSort(arr, 0, arr.length - 1); - quickSort(arr, 0, 9); System.out.println("Java3y " + arr);