Skip to content

Commit 827e520

Browse files
committed
opt: change java to java
1 parent f9dd130 commit 827e520

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+52
-52
lines changed

note/001/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ return [0, 1].
2222

2323
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,最容易的当然是循环两次,复杂度为`O(n^2)`,首次提交居然是2ms,打败了100%的提交,谜一样的结果,之后再次提交就再也没跑到过2ms了。
2424

25-
``` java
25+
```java
2626
class Solution {
2727
public int[] twoSum(int[] nums, int target) {
2828
int st = 0, end = nums.length;
@@ -42,7 +42,7 @@ class Solution {
4242

4343
利用HashMap作为存储,键为目标值减去当前元素值,索引为值,比如`i = 0`时,此时首先要判断`nums[0] = 2`是否在map中,如果不存在,那么插入键值对`key = 9 - 2 = 7, value = 0`,之后当`i = 1`时,此时判断`nums[1] = 7`已存在于map中,那么取出该`value = 0`作为第一个返回值,当前`i`作为第二个返回值,具体代码如下所示。
4444

45-
``` java
45+
```java
4646
class Solution {
4747
public int[] twoSum(int[] nums, int target) {
4848
int len = nums.length;

note/002/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You may assume the two numbers do not contain any leading zero, except the numbe
1616

1717
题意我也是看了好久才看懂,就是以链表表示一个数,低位在前,高位在后,所以题中的例子就是`342 + 465 = 807`,所以我们模拟计算即可。
1818

19-
``` java
19+
```java
2020
/**
2121
* Definition for singly-linked list.
2222
* public class ListNode {

note/003/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Given `"pwwkew"`, the answer is `"wke"`, with the length of 3. Note that the ans
1919

2020
题意是计算不带重复字符的最长子字符串的长度,开辟一个hash数组来存储该字符上次出现的位置,比如`hash[a] = 3`就是代表`a`字符前一次出现的索引在3,遍历该字符串,获取到上次出现的最大索引(只能向前,不能退后),与当前的索引做差获取的就是本次所需长度,从中迭代出最大值就是最终答案。
2121

22-
``` java
22+
```java
2323
class Solution {
2424
public int lengthOfLongestSubstring(String s) {
2525
int len;

note/004/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The median is (2 + 3)/2 = 2.5
3232

3333
题意是给你两个已排序的递增数组,让你找出其中位数,乍一看这题并不是很难,其实这题难度不可小觑,要做出时间复杂度为O(log (m+n))的话需要了解从两个递增数组中找出第k大的元素,也就是我写的`helper`函数所起到的作用。下面来解释以下其原理:假设数组分别记为A,B,当前需要搜索第k大的数,于是我们可以考虑从数组A中取出前m个元素,从数组B中取出k-m个元素。由于数组A,B分别排序,则A[m]大于从数组A中取出的其他所有元素,B[k-m] 大于数组B中取出的其他所有元素。此时,尽管取出元素之间的相对大小关系不确定,但A[m]与B[k-m]的较大者一定是这k个元素中最大的。那么,较小的那个元素一定不是第k大的,它至多是第k-1大的:因为它小于其他未被取出的所有元素,并且小于取出的k个元素中最大的那个。为叙述方便,假设A[m]是较小的那个元素。那么,我们可以进一步说,A[1], A[2]…A[m-1]也一定不是第k大的元素,因为它们小于A[m],而A[m]至多是第k-1大的。因此,我们可以把较小元素所在数组中选出的所有元素统统排除,并且相应地减少k值。这样,我们就完成了一次范围缩小。特别地,我们可以选取m=k/2。而本题只是其一种情况而已,也就当总长为偶数时,找出其中间的两个数,相加除二即可;当总长为奇数时,找到中间的数即可。
3434

35-
``` java
35+
```java
3636
class Solution {
3737
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
3838
int len = nums1.length + nums2.length;

note/007/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The input is assumed to be a 32-bit signed integer. Your function should **retur
3131

3232
题意是给你一个整型数,求它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
3333

34-
``` java
34+
```java
3535
class Solution {
3636
public int reverse(int x) {
3737
long res = 0;

note/008/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ If no valid conversion could be performed, a zero value is returned. If the corr
2727

2828
题意是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含`+`,如果之后存在非数字或全为空则返回`0`,而如果合法的值超过int表示的最大范围,则根据正负号返回`INT_MAX``INT_MIN`
2929

30-
``` java
30+
```java
3131
class Solution {
3232
public int myAtoi(String str) {
3333
int i = 0, ans = 0, sign = 1, len = str.length();

note/009/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ There is a more generic way of solving this problem.
2323

2424
题意是判断一个有符号整型数是否是回文,也就是逆序过来的整数和原整数相同,首先负数肯定不是,接下来我们分析一下最普通的解法,就是直接算出他的回文数,然后和给定值比较即可。
2525

26-
``` java
26+
```java
2727
class Solution {
2828
public boolean isPalindrome(int x) {
2929
if (x < 0) return false;
@@ -41,7 +41,7 @@ class Solution {
4141

4242
好好思考下是否需要计算整个长度,比如1234321,其实不然,我们只需要计算一半的长度即可,就是在计算过程中的那个逆序数比不断除10的数大就结束计算即可,但是这也带来了另一个问题,比如10的倍数的数10010,它也会返回`true`,所以我们需要对10的倍数的数再加个判断即可,代码如下所示。
4343

44-
``` java
44+
```java
4545
public boolean isPalindrome(int x) {
4646
if (x < 0 || (x != 0 && x % 10 == 0)) return false;
4747
int halfReverseX = 0;

note/010/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ isMatch("aab", "c*a*b") → true
3131
题意是
3232

3333

34-
``` java
34+
```java
3535
class Solution {
3636
public boolean isMatch(String s, String p) {
3737
if (p.length() == 0) return s.length() == 0;
@@ -62,7 +62,7 @@ class Solution {
6262
```
6363

6464

65-
``` java
65+
```java
6666
class Solution {
6767
public boolean isMatch(String s, String p) {
6868
if (p.length() == 0) return s.length() == 0;

note/013/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Input is guaranteed to be within the range from 1 to 3999.
1919

2020
那么我们可以利用map来完成罗马数字的7个数字符号:I、V、X、L、C、D、M和整数的映射关系,然后根据上面的解释来模拟完成即可。
2121

22-
``` java
22+
```java
2323
class Solution {
2424
public int romanToInt(String s) {
2525
Map<Character, Integer> map = new HashMap<>();

note/014/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Write a function to find the longest common prefix string amongst an array of st
1111

1212
题意是让你从字符串数组中找出公共前缀,我的想法是找出最短的那个字符串的长度`minLen`,然后在`0...minLen`的范围比较所有字符串,如果比较到有不同的字符,那么直接返回当前索引长度的字符串即可,否则最后返回最短的字符串即可。
1313

14-
``` java
14+
```java
1515
class Solution {
1616
public String longestCommonPrefix(String[] strs) {
1717
int len = strs.length;

0 commit comments

Comments
 (0)