如何快速掌握一门编程语言🔥

2025-05-25

如何快速掌握一门编程语言🔥

对于开发者来说,精通至少一门编程语言至关重要。掌握多种编程语言会让你更具优势。

您可能认识一些可以很快学会语言的人。

当你非常了解一门语言时,你可能会认为其他语言唯一会改变的就是语法。

你大错特错了。

从 JavaScript 转到 Rust 时唯一改变的不是语法。

编程风格也会改变,关键基础也会改变。


几个月前,我正在通过文档学习 Rust 编程。(对于任何想要开始使用 Rust 的人来说,这都是一个非常好的资源。)

就我个人经验而言,Rust 语言🦀的学习曲线非常陡峭。无论多少书籍/文档都无法让你完成学习。

幸运的是,大约在这个时候,我在 LeetCode 上解决了很多竞赛编程问题。

我决定接受 30 天的挑战:解决 30 个 Rust 编程问题。

这确实起了很大作用。


我们以连续数组作为示例问题。

给定一个二进制数组,找到 0 和 1 数量相等的连续子数组的最大长度。

例 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

示例 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

注意:给定的二进制数组的长度不会超过 50,000。

用 Java 解决它非常简单:

class Solution {
    public int findMaxLength(int[] nums) {
        int sum = 0;
        int res = 0;
        // build a <sum, index> hashmap
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);
        for (int i = 0; i < nums.length; i++) {
            System.out.println(i);  
            if (nums[i] == 0) {
                sum++;
            } else {
                sum--;
            }
            if (map.containsKey(sum)) {
                res = Math.max(res, i - map.get(sum));                
            } else {
                map.put(sum, i);
            }
        }
        return res;
    }
}

此外,在 JavaScript 中也很简单:

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaxLength = function(nums) {
    let hash = {0:-1};
    let count = 0;
    let max = 0;
    for (let i=0;i<nums.length;i++) {
        if (nums[i] == 0) count--;
        else count++;

        if (hash[count]!=null) max = Math.max(max, i - hash[count]);
        else hash[count] = i 
    }
    return max;
};

但真正的挑战是用 Rust 来解决它。你需要不断搜索、研究,直到最终学会。

use std::cmp;
use std::collections::HashMap;

impl Solution {
    pub fn find_max_length(nums: Vec<i32>) -> i32 {
        let mut sum: i32 = 0;
        let mut len: i32 = 0;

        let mut map = HashMap::new();
        map.insert(0, -1);

        let size = &nums.len();

        for i in 0..*size {
            if nums[i as usize] == 0 {
                sum -= 1;
            } else {
                sum += 1;
            }
            if map.contains_key(&sum) {
                let x: i32 = match map.get(&sum) {
                    Some(&s) => s,
                    None => -1
                };  
                len = cmp::max(len, (i as i32 -x));
            } else {
                map.insert(sum, i as i32);
            }
        }
        len
    }
}

简而言之,开始用你想学的语言解决竞技编程问题吧。一个月内你就会看到明显的进步!

请在下面的讨论中让我知道您的想法。

干杯!

文章来源:https://dev.to/niharrs/how-to-master-a-programming-language-really-fast-5a6c
PREV
Web开发人员的基本色彩理论
NEXT
在开始你的下一个副业项目之前,先把这些准备好👀