112. Path Sum
判断能否从根节点到某个叶子节点的路径和等于指定值。
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
if(root.left == null && root.right == null && sum - root.val == 0) return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
695. Max Area of Island
题意:找出一个01矩阵中最大的一片1的面积。
典型dfs。
public int maxAreaOfIsland(int[][] grid) {
int max_area = 0;
for(int i = 0; i < grid.length; i++)
for(int j = 0; j < grid[0].length; j++)
if(grid[i][j] == 1) max_area = Math.max(max_area, AreaOfIsland(grid, i, j));
return max_area;
}
public int AreaOfIsland(int[][] grid, int i, int j){
if( i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] == 1){
grid[i][j] = -1; // visited is -1
return 1 + AreaOfIsland(grid, i+1, j) + AreaOfIsland(grid, i-1, j) + AreaOfIsland(grid, i, j-1) + AreaOfIsland(grid, i, j+1);
}
return 0;
}
168. Excel Sheet Column Title
excel中列的数字字母转换。大神的代码。。简洁得无可挑剔。
1 -> A, 2 -> B, 3 -> C… 26 -> Z, 27 -> AA, 28 -> AB
return n == 0 ? "" : convertToTitle((n - 1) / 26) + (char) ((n - 1) % 26 + 'A');
500. Keyboard Row
判断一个单词是否能够只用键盘上的一行字母构成。
public class Solution {
public String[] findWords(String[] words) {
String[] strs = {"QWERTYUIOP","ASDFGHJKL","ZXCVBNM"};
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i<strs.length; i++){
for(char c: strs[i].toCharArray()){
map.put(c, i);//put <char, rowIndex> pair into the map
}
}
List<String> res = new LinkedList<>();
for(String w: words){
if(w.equals("")) continue;
int index = map.get(w.toUpperCase().charAt(0));
for(char c: w.toUpperCase().toCharArray()){
if(map.get(c)!=index){
index = -1; //don't need a boolean flag.
break;
}
}
if(index!=-1) res.add(w);//if index != -1, this is a valid string
}
return res.toArray(new String[0]);
}
}
617. Merge Two Binary Trees
题意:从根部合并两个树。要牢记实现的细节!PS:这不是自己的代码
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return null;
int val = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val);
TreeNode newNode = new TreeNode(val);
newNode.left = mergeTrees(t1 == null ? null : t1.left, t2 == null ? null : t2.left);
newNode.right = mergeTrees(t1 == null ? null : t1.right, t2 == null ? null : t2.right);
return newNode;
}
669. Trim a Binary Search Tree
题意:给定一个BST,要求去除值在[L,R]外的节点(R >= L).有可能会改变树根。PS:这不是自己的代码
class Solution {
public TreeNode trimBST(TreeNode root, int L, int R) {
if (root == null) return null;
if (root.val < L) return trimBST(root.right, L, R);
if (root.val > R) return trimBST(root.left, L, R);
//
root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);
return root;
}
}
448. Find All Numbers Disappeared in an Array
一个长为n的数组,元素都是1<=a[i]<=n。有些数出现了两次有些出现了一次。找到那些没出现的数。
我只能说,如果不准新开数组,这题真的挺不easy的。。如果把所有值减一,然后把a[i]=j视为从i到j的有向边,那么要找的就是没有入边的节点。
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> ret = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
int val = Math.abs(nums[i]) - 1;
if(nums[val] > 0) {
nums[val] = -nums[val];
}
}
for(int i = 0; i < nums.length; i++) {
if(nums[i] > 0) {
ret.add(i+1);
}
}
return ret;
}
442. Find All Duplicates in an Array
题意:一个长为n的数组由1-n中的数字组成。找出所有出现两次的数字。
解法:从448的思路稍微改改就行。看看日后你能想到否?
283. Move Zeroes
题意:把一个数组中的所有0都移到末尾,同时保持其他元素的相对顺序。不能copy数组,只能在原数组上操作。
这个思路好棒啊,O(n)就行了。
双下标法,一个慢一个快。其中的精髓是要逆向思考,也就是说并不是对题目特定数操作,而是在非特定数才操作。比如这题就不是对0元素操作而是非0元素。
public void moveZeroes(int[] nums) {
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
j++;
}
}
}
27. Remove Element
题意,在原数组上移除所有指定值元素。
又一个双下标法。
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
26. Remove Duplicates from Sorted Array
双下标试试看。
53. Maximum Subarray
找到子序列的最大和。
解法:实际上是挺简单的DP,但你还是不会!看代码吧。
private int[] sum;
public int maxSubArray(int[] nums) {
if(nums.length==0) return 0;
int [] dp = new int[nums.length+1];
dp[0]=0;
int max_=nums[0];
for(int i=1;i<=nums.length;i++){
dp[i]=nums[i-1]+(dp[i-1]>0 ? dp[i-1] : 0);
max_=Math.max(dp[i], max_);
}
return max_;
}
697. Degree of an Array
题意:一个数组的度定义为出现次数最多的元素的出现次数。求最短子序列的长度,且子序列有相同度。
扫描,记录每个元素第一次出现和最后一次出现的下标,记为left和right,然后取度数最大且最短的就行。
以下是别人家的代码。。思路不难,主要是熟悉java语法。
class Solution {
public int findShortestSubArray(int[] nums) {
Map<Integer, Integer> left = new HashMap(),
right = new HashMap(), count = new HashMap();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (left.get(x) == null) left.put(x, i);
right.put(x, i);
count.put(x, count.getOrDefault(x, 0) + 1);
}
int ans = nums.length;
int degree = Collections.max(count.values());
for (int x: count.keySet()) {
if (count.get(x) == degree) {
ans = Math.min(ans, right.get(x) - left.get(x) + 1);
}
}
return ans;
}
}
796. Rotate String
题意:判断一个字符串能不能通过字母平移得到另一个串。
简单的return (A+A).contains(B),但复杂度是N2。其实有N的,要用到大数哈希了。solution
本文精选了多个经典算法问题及其解决方案,包括路径总和、岛屿最大面积、Excel列标题转换、键盘行单词验证、二叉树合并等。每道题都附带了简洁高效的代码实现。
352

被折叠的 条评论
为什么被折叠?



