ARTS-01

Algorithm

leetcode 112: Path Sum

    public boolean hasPathSum(TreeNode node, int sum) {
        if(node == null) return false;
        if(node.val == sum && node.left == null && node.right == null) return true;
        return hasPathSum(node.left, sum - node.val) ||
            hasPathSum(node.right, sum - node.val);
    }

leetcode 113: Path Sum II

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        pathSum(root, sum, result, list);
        return result;
    }
    
    private void pathSum(TreeNode node, int sum, List<List<Integer>> result, List<Integer> list) {
        if (node == null) return ;
        list.add(node.val);
        if (node.val == sum && node.left == null && node.right == null) {
            result.add(new ArrayList(list));
            list.remove(list.size() - 1);
            return ;
        }
        pathSum(node.left, sum - node.val, result, list);
        pathSum(node.right, sum - node.val, result, list);
        list.remove(list.size() - 1);
    }

leetcode 437 Path Sum III

    public int pathSum(TreeNode node, int sum) {
        if(node == null) return 0;
        return pathSum(node.left, sum) + pathSum(node.right, sum) + pathSumFrom(node, sum);
    }
    
    private int pathSumFrom(TreeNode node, int sum) {
        if(node == null) return 0;
        return (node.val == sum ? 1 : 0) + pathSumFrom(node.left, sum - node.val) + pathSumFrom(node.right, sum - node.val);
    }

Review

This is a post on Quora the What are some of MOST at The Basic Things Every Programmer Should know? . Which discusses some common sense programmer should know, I have some excerpts below.

  • If it's not tested, it doesn't work.
  • Don't reinvent the wheel, library code is there to help.
  • Code that's hard to understand is hard to maintain.
  • Code that's hard to maintain is next to useless.
  • Magic numbers are bad.
  • If there is a bug, the user will find it.
  • A code review is not a criticism.
  • Eat your own dog food — fixing bugs in your own code helps you code better and improves your understanding.
  • Fewer features for better code is always the right answer in the end.
  • There’s always a better way.

Tip

To delete a repository on github.

Into the Repository you want to delete -> Settings -> Danger Zone -> Delete this repository.

Share

And the computer is very similar.

Computer equipment good or bad, people, too. So we need to exercise.

The composition of the core computer has three parts: input, calculates the output. People, too, so we need to study, think and output.

Data input is not necessarily correct, there may be dirty, it needs to be cleaned. Reading the same book good or bad, be sure to read good books. Bad books useless, but harmful, just as dirty as the data will affect the final result.

To solve the same problem, the efficiency of different algorithms can be vastly different. Corresponds to a person, that is a different knowledge structure, efficiency between different modes of thinking will be a great difference. And in the algorithm, we can often use space for time. So, your stock of knowledge will be a great influence on the efficiency of thinking.

The computer needs to show the results of their calculations to humans, or to inform an answer, or facilitate human revised program. People, too, need to show the world the results of their own thinking. Or tell the world the answer, or exchange ideas with others.

Guess you like

Origin www.cnblogs.com/thomas-hzp/p/11409724.html