mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -223,7 +223,7 @@ class Solution {
|
|||||||
// 解法2
|
// 解法2
|
||||||
// 1、首先,在 nums 数组中二分查找 target;
|
// 1、首先,在 nums 数组中二分查找 target;
|
||||||
// 2、如果二分查找失败,则 binarySearch 返回 -1,表明 nums 中没有 target。此时,searchRange 直接返回 {-1, -1};
|
// 2、如果二分查找失败,则 binarySearch 返回 -1,表明 nums 中没有 target。此时,searchRange 直接返回 {-1, -1};
|
||||||
// 3、如果二分查找失败,则 binarySearch 返回 nums 中 为 target 的一个下标。然后,通过左右滑动指针,来找到符合题意的区间
|
// 3、如果二分查找成功,则 binarySearch 返回 nums 中值为 target 的一个下标。然后,通过左右滑动指针,来找到符合题意的区间
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public int[] searchRange(int[] nums, int target) {
|
public int[] searchRange(int[] nums, int target) {
|
||||||
|
@ -384,7 +384,7 @@ func (this *MyQueue) Peek() int {
|
|||||||
func (this *MyQueue) Empty() bool {
|
func (this *MyQueue) Empty() bool {
|
||||||
return len(this.stack) == 0 && len(this.back) == 0
|
return len(this.stack) == 0 && len(this.back) == 0
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
javaScript:
|
javaScript:
|
||||||
|
|
||||||
@ -442,8 +442,6 @@ MyQueue.prototype.empty = function() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
@ -71,21 +71,84 @@ public:
|
|||||||
## Java
|
## Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
// 时间复杂度:O(n)
|
||||||
|
// 空间复杂度:如果采用 toCharArray,则是 O(n);如果使用 charAt,则是 O(1)
|
||||||
|
class Solution {
|
||||||
|
public boolean judgeCircle(String moves) {
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
for (char c : moves.toCharArray()) {
|
||||||
|
if (c == 'U') y++;
|
||||||
|
if (c == 'D') y--;
|
||||||
|
if (c == 'L') x++;
|
||||||
|
if (c == 'R') x--;
|
||||||
|
}
|
||||||
|
return x == 0 && y == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
# 时间复杂度:O(n)
|
||||||
|
# 空间复杂度:O(1)
|
||||||
|
class Solution:
|
||||||
|
def judgeCircle(self, moves: str) -> bool:
|
||||||
|
x = 0 # 记录当前位置
|
||||||
|
y = 0
|
||||||
|
for i in range(len(moves)):
|
||||||
|
if (moves[i] == 'U'):
|
||||||
|
y += 1
|
||||||
|
if (moves[i] == 'D'):
|
||||||
|
y -= 1
|
||||||
|
if (moves[i] == 'L'):
|
||||||
|
x += 1
|
||||||
|
if (moves[i] == 'R'):
|
||||||
|
x -= 1
|
||||||
|
return x == 0 and y == 0
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
func judgeCircle(moves string) bool {
|
||||||
|
x := 0
|
||||||
|
y := 0
|
||||||
|
for i := 0; i < len(moves); i++ {
|
||||||
|
if moves[i] == 'U' {
|
||||||
|
y++
|
||||||
|
}
|
||||||
|
if moves[i] == 'D' {
|
||||||
|
y--
|
||||||
|
}
|
||||||
|
if moves[i] == 'L' {
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
if moves[i] == 'R' {
|
||||||
|
x--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return x == 0 && y == 0;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## JavaScript
|
## JavaScript
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
// 时间复杂度:O(n)
|
||||||
|
// 空间复杂度:O(1)
|
||||||
|
var judgeCircle = function(moves) {
|
||||||
|
var x = 0; // 记录当前位置
|
||||||
|
var y = 0;
|
||||||
|
for (var i = 0; i < moves.length; i++) {
|
||||||
|
if (moves[i] == 'U') y++;
|
||||||
|
if (moves[i] == 'D') y--;
|
||||||
|
if (moves[i] == 'L') x++;
|
||||||
|
if (moves[i] == 'R') x--;
|
||||||
|
}
|
||||||
|
return x == 0 && y == 0;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。
|
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。
|
||||||
|
|
||||||
以数组形式返回答案。
|
以数组形式返回答案。
|
||||||
|
|
||||||
|
|
||||||
示例 1:
|
示例 1:
|
||||||
输入:nums = [8,1,2,2,3]
|
输入:nums = [8,1,2,2,3]
|
||||||
@ -35,7 +35,7 @@
|
|||||||
示例 3:
|
示例 3:
|
||||||
输入:nums = [7,7,7,7]
|
输入:nums = [7,7,7,7]
|
||||||
输出:[0,0,0,0]
|
输出:[0,0,0,0]
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
* 2 <= nums.length <= 500
|
* 2 <= nums.length <= 500
|
||||||
* 0 <= nums[i] <= 100
|
* 0 <= nums[i] <= 100
|
||||||
@ -120,8 +120,51 @@ public:
|
|||||||
## Java
|
## Java
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
/**
|
||||||
|
* 解法一:暴力
|
||||||
|
* 时间复杂度:O(n^2)
|
||||||
|
* 空间复杂度:O(n)
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public int[] smallerNumbersThanCurrent(int[] nums) {
|
||||||
|
int[] res = new int[nums.length];
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
for (int j = 0; j < nums.length; j++) {
|
||||||
|
if (nums[j] < nums[i] && j != i) { // 注意 j 不能和 i 重合
|
||||||
|
res[i]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
/**
|
||||||
|
* 优化:排序 + 哈希表
|
||||||
|
* 时间复杂度:O(nlogn)
|
||||||
|
* 空间复杂度:O(n)
|
||||||
|
*/
|
||||||
|
class Solution {
|
||||||
|
public int[] smallerNumbersThanCurrent(int[] nums) {
|
||||||
|
int[] res = Arrays.copyOf(nums, nums.length);
|
||||||
|
Arrays.sort(res); // 是对 res 排序,nums 中顺序还要保持
|
||||||
|
int[] hash = new int[101]; // 使用哈希表,记录比当前元素小的元素个数
|
||||||
|
for (int i = res.length - 1; i >= 0; i--) { // 注意:从后向前
|
||||||
|
hash[res[i]] = i; // 排序后,当前下标即表示比当前元素小的元素个数
|
||||||
|
}
|
||||||
|
// 此时 hash中保存的每一个元素数值 便是 小于这个数值的个数
|
||||||
|
for (int i = 0; i < res.length; i++) {
|
||||||
|
res[i] = hash[nums[i]];
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -143,4 +186,3 @@ public:
|
|||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user