题目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...
Decode Ways 题解题目来源:https://leetcode.com/problems/decode-ways/description/DescriptionA message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.ExampleGiven encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).The numbe...
难度:简单 题目描述: 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(...
今天下午做了一道题。leetcode merge intervals 属于比较难的题目。 首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end 。 从而生成新的interal,再插入到新的list 返回结果。 下面给出自己的代码:/*50 Merge Intervals https://leetcode.com/problems/merge-intervals/Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18]...
这道题目让我们求出一个线性数组中最大的连续子数组的和,题目要求如下: 这道题的题目很简单,解法也有多种,在这里总结3种解法去解决这个问题,在比较中加深对不同思路算法的理解,这三种算法是动态规划、模拟法、滑动窗口。 首先是动态规划法,正如我们之前提过的,递推形式的动态规划基本上就是三部曲: 1.确定DP数组的含义 2.递推奠基 3.基于递推方程来进行反复递推,直到最终答案 就这道题而言,DP[i]的含义是以nums[i]为结...