ARTS是什么? Algorithm:每周至少做一个LeetCode的算法题 Review:阅读并点评至少一篇英文文章 Tip:学习至少一个技术技巧 Share:分享一篇有观点和思考的技术文章

Algorithm

  1. package leetcode;
  2. /**
  3. * @className: LeetCode03
  4. * @description:
  5. * 编写一个函数来查找字符串数组中的最长公共前缀。
  6. * 如果不存在公共前缀,返回空字符串 ""。
  7. *
  8. * 示例 1:
  9. * 输入:strs = ["flower","flow","flight"]
  10. * 输出:"fl"
  11. *
  12. * 示例 2:
  13. * 输入:strs = ["dog","racecar","car"]
  14. * 输出:""
  15. * 解释:输入不存在公共前缀。
  16. * @author: Miluo
  17. * @date: 2021/3/8
  18. **/
  19. public class LeetCode03 {
  20. public String longestCommonPrefix(String[] strs) {
  21. int min = 9999;
  22. char c = '0';
  23. String prefix = "";
  24. if (strs.length == 0){
  25. return prefix;
  26. }
  27. //计算数组内 位数最少是多少
  28. for (int i = 0; i < strs.length; i++) {
  29. if (strs[i].length() == 0){
  30. return prefix;
  31. }
  32. if (strs[i].length() < min){
  33. min = strs[i].length();
  34. }
  35. }
  36. //根据最小位数min,遍历数组前min位
  37. for (int j = 0; j < min; j++) {
  38. for (int i = 0; i < strs.length; i++) {
  39. if (i == 0){
  40. c = strs[i].charAt(j);
  41. }else {
  42. if (strs[i].charAt(j) != c){
  43. return prefix;
  44. }
  45. }
  46. }
  47. prefix = prefix.concat(String.valueOf(c));
  48. }
  49. return prefix;
  50. }
  51. //参考题解
  52. //依次遍历字符串数组中的每个字符串,对于每个遍历到的字符串,更新最长公共前缀,
  53. //当遍历完所有的字符串以后,即可得到字符串数组中的最长公共前缀。
  54. public String longestCommonPrefix(String[] strs) {
  55. if (strs == null || strs.length == 0) {
  56. return "";
  57. }
  58. String prefix = strs[0];
  59. int count = strs.length;
  60. for (int i = 1; i < count; i++) {
  61. prefix = longestCommonPrefix(prefix, strs[i]);
  62. if (prefix.length() == 0) {
  63. break;
  64. }
  65. }
  66. return prefix;
  67. }
  68. public String longestCommonPrefix(String str1, String str2) {
  69. int length = Math.min(str1.length(), str2.length());
  70. int index = 0;
  71. while (index < length && str1.charAt(index) == str2.charAt(index)) {
  72. index++;
  73. }
  74. return str1.substring(0, index);
  75. }
  76. }

Review

20 controversial programming opinions

Tip

可使用 try-with-resources 语句安全地释放资源

  1. 这里所谓的资源(resource)是指在程序完成后,必须关闭的对象, try-with-resources 语句确保了每个资源在语句结束时关闭;
  2. 使用 Java 7 新增的 try-with-resources 语句 代替 try-finally 语句进行资源关闭,不仅代码更精简而且更安全;
  3. 支持 try-with-resources 语句 的类必须都实现 AutoCloseable接口,同样的,我们自定义的类也可以实现这个接口来帮助我们进行一些安全的自动化释放资源;
  4. Java 9 对 try-with-resources 语句进行了改进,如果你有一个资源是 final 或等效于 final 变量, 则可以在 try-with-resources 语句中使用该变量,无需在 try-with-resources 语句中再声明一个新的变量。
  1. public static void demo() {
  2. File file = new File("try-with-resources-demo.txt");
  3. try (
  4. FileWriter fileWriter = new FileWriter(file);
  5. BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  6. FileReader fileReader = new FileReader(file);
  7. BufferedReader bufferedReader = new BufferedReader(fileReader);
  8. ) {
  9. bufferedWriter.write("now is:" + LocalDateTime.now() + "\n\r");
  10. bufferedWriter.write("availableProcessors are : " + Runtime.getRuntime().availableProcessors() + "\n\r");
  11. bufferedWriter.write("totalMemory is : " + Runtime.getRuntime().totalMemory() + "\n\r");
  12. bufferedWriter.write("maxMemory is : " + Runtime.getRuntime().maxMemory() + "\n\r");
  13. bufferedWriter.write("freeMemory is : " + Runtime.getRuntime().freeMemory() + "\n\r");
  14. bufferedWriter.flush();
  15. StringBuffer readResult = new StringBuffer("");
  16. String oneLine = null;
  17. while (null != (oneLine = bufferedReader.readLine())) {
  18. readResult.append(oneLine + "\n\r");
  19. }
  20. System.out.println(readResult.toString());
  21. } catch (IOException ioe) {
  22. ioe.printStackTrace();
  23. }
  24. }

Share

如何用正确的姿势打开TDD