最近的LeetCode挑战之旅

💻 Leetcode · 07-15 · 79 人浏览
最近的LeetCode挑战之旅
最近,我注意到许多编程大牛都在积极地刷LeetCode上的算法题目。这激发了我的兴趣,于是我也决定加入这场挑战。

初探算法题

我尝试了两道题目,虽然最终找到了解决方案,但当我对比官方的解答时,发现自己的代码实现方式与官方的思路大相径庭。这让我意识到,还有很大的提升空间。
2024-07-15T07:09:48.png

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        Integer num = 0;
        try {
            byte[] bytes = s.getBytes("UTF-8");
            LinkedHashMap<Byte,Byte> map = new LinkedHashMap<>();
            List<Integer> intList = new ArrayList<>();
            for (byte b:bytes) {
                if(map.containsKey(b)){
                    intList.add(map.size());
                    Iterator<Map.Entry<Byte, Byte>> iterator = map.entrySet().iterator();
                    boolean found = false;
                    while (iterator.hasNext()) {
                        Map.Entry<Byte, Byte> entry = iterator.next();
                        if (entry.getKey().equals(b)) {
                              iterator.remove();
                              break;
                        }else{
                              iterator.remove();
                        }  
                    }
                    map.put(b, b);
                }else{
                    map.put(b, b);
                    intList.add(map.size());
                }
            }
            for (Integer i:intList) {
                if(i>num){
                    num=i;
                }
            }
        }catch(Exception e){
              System.out.print(e.getMessage());  
        }
            return num;
    }
}

2024-07-15T07:10:04.png

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
               int c = nums[i]+nums[j];
               if(c==target){
                   return new int[]{i,j};
               }
            }
        }
        return null;
    }
}

学习与进步

通过阅读官方解答,我学到了许多优化代码的方法。这不仅仅是关于解决问题,更是一个学习和成长的过程。我认识到,编写简洁、高效、优雅的代码同样重要。

每日挑战

我决定每天都刷几道LeetCode上的题目。这不仅能锻炼我的编程技能,还能帮助我更好地理解算法和数据结构。我相信,通过持续的努力和学习,我的编程水平会得到显著提升。

加油,每天进步一点点!


让我们在代码的海洋中乘风破浪,不断探索和发现新大陆。

leetcode
  1. 厉害厉害,我是做不来的

    1. 执迷 (作者)  07-24
      @三毛笔记

      过奖啦 好多我也不会 只能写一些简单的

Under CC BY NC-SA License.
Powered by Typecho | Theme by Jasmine
您是第 5401 位访客