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...
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 element always exist in the array.Credits:Special thanks to @ts for adding this problem and creating all test cases. 题目解析:我用了Hashmap的方法, 感觉不是最好的, 但是是最好想到的. 如果有更好方法的童鞋们求评论我~~...
题目描述:Count the number of prime numbers less than a non-negative number, n. 要完成的函数:int countPrimes(int n) 说明:1、题目看上去非常简单和熟悉。给定一个非负数n,要求返回小于n的所有素数的个数。2、处理一下边界条件,n<=2时返回0,n=3时返回1,n=4时返回2。3、传统方法:对于小于n的每个数i,判断i是不是素数。判断方法是对于每个大于等于2且小于等于i/2的数,确定i能否整除这个数。双重循环,暴力解法。然后...
Longest Palindromic SubstringGiven a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 同Palindrome Partitioning II 几乎一致,二维数组动态规划问题。需要注意两点:1、使用数组比使用vector of vector节省时间;2、每次发现当前最优回文串就使用substr取出会耗时间,改成记录起始位置。class Sol...