17. Letter Combinations of a Phone Number Problem‘s Link ----------------------------------------------------------------------------
Mean: 给你一个数字串,输出其在手机九宫格键盘上的所有可能组合.analyse:使用进位思想来枚举所有组合即可.Time complexity: O(N) view code/** * ----------------------------------------------------------------- * Copyright (c) 2016 crazyacking.All rights reserved. * ------...
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A = [1,1,2],Your function should return length = 2, and A is now [1,2].解法很简单,边界条件A为null或者长度为0,返回0;否则,用variable size来维护题目要求的数...
827. 最大人工岛
难度困难83
在二维地图上, 0代表海洋, 1代表陆地,我们最多只能将一格 0 海洋变成 1变成陆地。
进行填海之后,地图上最大的岛屿面积是多少?(上、下、左、右四个方向相连的 1 可形成岛屿)
示例 1:
输入: [[1, 0], [0, 1]]
输出: 3
解释: 将一格0变成1,最终连通两个小岛得到面积为 3 的岛屿。示例 2:
输入: [[1, 1], [1, 0]]
输出: 4
解释: 将一格0变成1,岛屿的面积扩大为 4。示例 3:
输入: [[1, 1], [1, 1]]...
https://oj.leetcode.com/problems/path-sum/http://blog.csdn.net/linhuanmars/article/details/23654413/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null)
return false; ...
Greedy先生成abbreviation,如果有重复,对所有重复的prefix再添加一个字符,直到没有重复为止。class Solution {
public:vector<string> wordsAbbreviation(vector<string>& dict) {int n=dict.size();vector<string> res(n);vector<int> prefix(n,0); //prefix[i] = to what index is the prefixfor (int i=0;i<n;++i){res[i] = abbrev(dict[i],prefix[i]);}for (int i=0;i<n;++i){ while (true){vector<int> dupes;for (int j=i...
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.You can use each character in text at most once. Return the maximum number of instances that can be formed.Example 1:Input: text = "nlaebolko"
Output: 1
Example 2:Input: text = "loonbalxballpoon"
Output: 2
Example 3:Input: text = "leetcode"
Output: 0Constraints:1 <= text.length <...
原题链接在这里:https://leetcode.com/problems/minimum-size-subarray-sum/ 维护一个window, 当sum<s时一直移动window 的 right index, 同时更新sum.当sum >= s后一直移动window left index, 同时更新sum 和 res.最后返回时看res 是否 被更新过,若被更新过就返回 res, 没有更新过说明没有符合要求的window, 返回0.Note: right的初始值是0, 所以每次更新sum 后 right 是指向了下个没有加进sum里的数值. 外循环while中的第二部分检...
题目Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321简单题,需要考虑两个问题,1)如果输入的末尾是0怎么办?2)如果倒过来的数字溢出了怎么办?class Solution {
public:int reverse(int x) {int flag = 1;unsigned res = 0;;if (x < 0){flag = -1;x = flag * x;}while (x > 0){if (res > (INT_MAX - x%10)/10)return0;res = res*10 + x%10;x = x/10;}return flag * (int)res;}
}; ...
链接:LeetCode[Leetcode]5380. 数组中的字符串匹配给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。
如果你可以删除\(words[j]\)最左侧和/或最右侧的若干字符得到\(word[i]\),那么字符串\(words[i]\) 就是\(words[j]\)的一个子字符串。直接暴力就可以了。class Solution:def stringMatching(self, words: List[str]) -> List[str]:res = []f...
https://leetcode.com/problems/friend-circlespublicclass Solution {int[] id;int[] weight;publicint findCircleNum(int[][] M) {if (M == null || M.length == 0 || M[0].length == 0) {return 0;}initUnionFind(M.length);Set<Integer> set = new HashSet<>();for (int i = 0; i < M.length; i++) {for (int j = 0; j < M[0].length; j++) {if (i == j) {continue;}if (M[i][j] == 1) {union(i, j);}}}for (int i = 0; i < id...
Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3/ 9 20/ 15 7
return its level order traversal as:[[3],[9,20],[15,7]
] 1class Solution {2public:3 vector<vector<int>> levelOrder(TreeNode* root) {4 queue<TreeNode*>q, p;5 vector<vector<int>>ans;...
Given n non-negative
integers a1, a2,
..., an, where each represents a point
at coordinate
(i, ai). n vertical
lines are drawn such that the two endpoints of
line i is at
(i, ai) and
(i, 0). Find two lines, which together with x-axis forms a
container, such that the container contains the most water. 1class Solution {2public:3int maxArea(vector<int> &height) {4int i = 0, j = height.size()...
1 题目描述
题目链接:https://leetcode-cn.com/problems/decode-xored-array/未知 整数数组 arr 由 n 个非负整数组成。
经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。
给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。
请解码返回原数组 arr 。可以证明答案存在并且是唯一的。示例 1:
输入:...
Given a linked list, determine if it has a cycle in it.Follow up:
Can you solve it without using extra space?Analysis: typical Runner Technique. 一次过 1/** 2 * Definition for singly-linked list.3 * class ListNode {4 * int val;5 * ListNode next;6 * ListNode(int x) {7 * val = x;8 * next = null;9 * }
10 * }
11*/12publicclass Solution {
13publicboolean hasCycle(ListNod...
题目
Implement int sqrt(int x).
Compute and return the square root of x.
分析
两种方法:1. 牛顿法(解法1)2. 二分查找(解法2)解法1public class Sqrt {public int sqrt(int x) {double ret = 1, temp = 0;while ((int) ret - (int) temp != 0) {temp = ret;ret = ret / 2 + (double) x / (2 * ret);}return (int) ret;}
}解法2
public class Sqrt {public int sqrt(int x) {if (x < 2) {return x;}int left = 1;int right...