Ruizhi Ma's Blog

Keep Calm and Carry On

Problem 112

Sum Path

问题描述 见链接:https://leetcode.com/problems/path-sum/ 解决思路 其实就是深度优先(DFS),不断递归左右子树。 代码 package leetcode; public class PathSum { public class TreeNode { int val; TreeNode left; ...

Problem 102

Binary Tree Level Order Traversal

问题描述 题目看链接:https://leetcode.com/problems/binary-tree-level-order-traversal/ 解决思路 同Pro107 代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode le...

Problem 171

Excel Sheet Column Number

问题描述 Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... Example 1: ...

Problem 169

Majority Element

问题描述 Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority elem...

Problem 136

Single Number

问题描述 Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it wi...

Problem 119

Pascal's Triangle II

问题描述 题目看链接:https://leetcode.com/problems/pascals-triangle-ii/ 解决思路 这题除了题目和118有略微不同,只需要返回最后一行,还有就是需要在O(n)空间完成,虽然118虽然改一下就能出结果,但那个是O(n^2)。其实杨辉三角有一个公式,即第n行的m个数可表示为 C(n-1,m-1).这样就很简单了,算出第n行第一个数,然后遍历该行...

Problem 118

Pascal's Triangle

问题描述 其实就是杨辉三角,题目看网址:https://leetcode.com/problems/pascals-triangle/ 解决思路 主要有几个地方需要处理: 通过遍历上一行元素,将当前元素由其左上角和右上角元素之和获得 每行最后一个元素1,要单独加入 要使用双层List(List<List<>>),内层List是每一行所有元素的集合,外层...

HashMap

手动实现HaspMap的常用方法

Node3类 package cn.sxt.myCollection; /* * to be used in SxtHashMap */ public class Node3<K,V> { int hash; K key; V value; Node3 next; } SxtHashMap04类 package cn.sxt.myCollection;...

StringBuilder和StringBuffer

可变序列,常用方法,效率探究

TestStringBuilder类 package commonClass; /** * Test StringBuilder, StringBuffer variable character sequence * @author Sober * */ public class TestStringBuilder { public static void main(Stri...

Problem 67

Add Binary

问题描述 Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = “11”, b = “1” Output: “100...