Skip to content

Commit ca69d22

Browse files
authored
Merge pull request gzc426#24 from gzc426/master
1
2 parents e82eb6c + 1a09a80 commit ca69d22

File tree

123 files changed

+4942
-0
lines changed

Some content is hidden

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

123 files changed

+4942
-0
lines changed

018429.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int firstUniqChar(char* s) {
2+
int ascii[256]={0};
3+
int i=0,len;
4+
while(s[i]!='\0')
5+
{
6+
i++;
7+
}
8+
len=i;
9+
for(i=0;i<len;i++)
10+
{
11+
ascii[s[i]]++;
12+
}
13+
i=0;
14+
while(ascii[s[i]]!=1&&i<len) i++;
15+
if(i==len)
16+
return -1;
17+
return i;
18+
}

2018.11.25-leetcode80/大魔王爱穿孖烟筒.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
```
12
class Solution {
23
public int removeDuplicates(int[] nums) {
34
int count=0;
@@ -41,3 +42,4 @@ class Solution {
4142
return count;
4243
}
4344
}
45+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
```
2+
class Solution {
3+
public int maxArea(int[] height) {
4+
int left=0;//从左边遍历
5+
int right=height.length-1;//从右边遍历
6+
int tempCha=0;
7+
int sum=0;
8+
int tempsum=0;
9+
while(left<right){//遍历到中间,两指针重合为止,求出沿途的面积。
10+
tempCha=right-left;//每次遍历记录临时长度差,也就是底边的长度
11+
if(height[right]>height[left]){
12+
tempsum=height[left]*tempCha; //短(短板)的边作为高,求出此时的面积
13+
left++;
14+
}else{
15+
tempsum=height[right]*tempCha;
16+
right--;
17+
}
18+
if(tempsum>sum){
19+
sum=tempsum;
20+
}
21+
22+
23+
}
24+
return sum;
25+
}
26+
}
27+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```
2+
class Solution {
3+
public boolean isPalindrome(String s) {
4+
s = s.toLowerCase();
5+
char [] charArray = s.toCharArray();
6+
7+
int begin = 0;
8+
int end = charArray.length - 1;
9+
while(begin < end)
10+
11+
{
12+
if (!isChar(charArray[begin])){//不是数字和小字母
13+
begin++;
14+
continue;
15+
}
16+
if (!isChar(charArray[end])){//不是数字和小字母
17+
end--;
18+
continue;
19+
}
20+
if(charArray[begin] == charArray[end])
21+
{
22+
begin++;
23+
end--;
24+
}else{
25+
return false;
26+
}
27+
}
28+
return true;
29+
}
30+
public boolean isChar(char c){//判断是否为数字或者字母
31+
if (c<48||(c>57&&c<65)||(c>90&&c<97)||c>122){
32+
return false;
33+
}
34+
return true;
35+
}
36+
}
37+
```

2018.11.28-leetcode151/018429.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
int top=0;
2+
void push(char c,char* stack)
3+
{
4+
stack[top++]=c;
5+
}
6+
char pop(char* stack)
7+
{
8+
return stack[--top];//写入s1
9+
}
10+
void reverseWords(char *s) {
11+
int i=0,j=0,flag=0;
12+
while(s[i]!='\0') i++;
13+
char* stack = (char*)malloc(i);
14+
char* s1 = (char*)malloc(i+1);
15+
i--;
16+
while(i>=0)
17+
{
18+
if(s[i]==' ')
19+
{
20+
while(top!=0)//如果栈不为空
21+
{
22+
flag=1;
23+
s1[j++]=pop(stack);
24+
}
25+
if(flag==1)
26+
{
27+
s1[j++]=' ';
28+
flag=0;//重置flag
29+
}
30+
i--;
31+
}
32+
else
33+
{
34+
push(s[i--],stack);
35+
}
36+
}
37+
if(s[++i]!=' ')
38+
{
39+
while(top!=0)//如果栈不为空
40+
s1[j++]=pop(stack);
41+
s[j]=' ';
42+
}
43+
else
44+
j--;
45+
s1[j]='\0';
46+
s = strcpy(s,s1);
47+
free(s1);
48+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```
2+
public class Solution {
3+
public String reverseWords(String s) {
4+
if (s == null || s.length() == 0) {
5+
return "";
6+
}
7+
String[] array = s.split(" ");//按照空格划分字符串
8+
String str = "";
9+
for (int i = array.length - 1; i >= 0; i--) {
10+
if (!array[i].equals("")) {
11+
if (str.length() > 0) {
12+
str += " ";
13+
}
14+
str += array[i];//如果不是空字符串,就把截断的旧字符串添加到新字符串中。
15+
}
16+
}
17+
return str;
18+
}
19+
}
20+
```

2018.11.29-leetcode443/kiritocly.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
private static int solution(char[] charArray) {
2+
if (charArray.length == 0) {
3+
return 0;
4+
}
5+
//遍历整个字符数组
6+
//压缩后的字符长度
7+
int resultCount = 0;
8+
//当前重复字符计数
9+
int count = 1;
10+
for (int i = 0; i < charArray.length; i++) {
11+
//如果遇到某个字符与当前字符不相等或者已经遍历到最后一个字符
12+
if (i + 1 == charArray.length || charArray[i] != charArray[i + 1]) {
13+
//记录一下当前字符,并且resultCount加1,比如a,a,b,b,b,扫面到第二个a时
14+
//a!=b,如果a的个数大于1,先将a后面所有的a替换为数字,如a,a替换为a,2
15+
charArray[resultCount++] = charArray[i];
16+
if (count > 1) {
17+
String temp = String.valueOf(count);
18+
for (int k = 0; k < temp.length(); k++) {
19+
charArray[resultCount++] = temp.charAt(k);
20+
}
21+
}
22+
//重新统计下一个字符
23+
count = 1;
24+
} else {
25+
//统计重复的字符个数
26+
count++;
27+
}
28+
29+
}
30+
return resultCount;
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```
2+
class Solution {
3+
public int compress(char[] chars) {//思路有一点,写的时候就缺胳膊少腿,又来模仿群主代码
4+
int count = 1;
5+
int index = 0;
6+
for (int i = 0; i < chars.length; i++) {
7+
if (i + 1 == chars.length || chars[i] != chars[i+1]) {
8+
chars[index++] = chars[i];//前后不同直接添加
9+
if (count > 1) {
10+
String temp = String.valueOf(count);//转换成字符串,也可以变成字符数组,然后遍历。
11+
for(int k=0;k<temp.length();k++)
12+
chars[index++] = temp.charAt(k);
13+
}
14+
count = 1;//重置
15+
}
16+
else {
17+
count++;//统计重复字符的个数
18+
}
19+
}
20+
return index;
21+
}
22+
}
23+
```

2018.11.29-leetcode443/妮可.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
```java
2+
package sy181203;
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
7+
public class leetcode_443压缩字符串
8+
{
9+
10+
public static void main(String[] args)
11+
{
12+
char[] chars = {'a','b','b','b','c','c','b','b','b','b','b','b','b','b','b','b','b','b'};
13+
System.out.println(Arrays.toString(chars));
14+
15+
System.out.println(compress(chars));
16+
17+
}
18+
19+
public static int compress(char[] chars)
20+
{
21+
if(chars.length==0)
22+
return 0;
23+
int m=0,i=0,j;
24+
while(i<chars.length)//外层循环控制总次数
25+
{
26+
j=i;
27+
chars[m++]=chars[i];
28+
int k=0;//统计重复字母数
29+
while(j<chars.length && chars[i]==chars[j])//内层循环判断是不是同一个字母
30+
{
31+
k++;
32+
j++;
33+
}
34+
StringBuilder sb=new StringBuilder();
35+
if(k!=1)//k=1不用再加数字
36+
{
37+
while(k!=0)//除来减小,到0出循环
38+
{
39+
System.out.println("k%10 "+(k%10));
40+
sb.append((char)(k%10+48));
41+
k/=10;
42+
}
43+
}
44+
System.out.println("sb ==="+sb.toString());
45+
sb.reverse();//
46+
System.out.println("sbre ==="+sb.toString());
47+
for(char c:sb.toString().toCharArray())
48+
49+
{
50+
System.out.println("c="+c);
51+
chars[m++]=c;
52+
}
53+
i=j;//置位
54+
}
55+
System.out.println(Arrays.toString(chars));
56+
return m;
57+
}
58+
59+
}
60+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
```
2+
class Solution {
3+
public List<String> findAndReplacePattern(String[] words, String pattern) {
4+
List<String> list=new ArrayList<>();
5+
char [] pat=pattern.toCharArray();
6+
for(int i=0;i<words.length;i++){
7+
8+
char [] sArray=words[i].toCharArray();
9+
boolean flag=true;
10+
if(sArray.length==pat.length) {
11+
HashMap<Character,Character> hash=new HashMap<>();
12+
HashMap<Character,Character> hash2=new HashMap<>();
13+
for(int j=0;j<pat.length;j++){
14+
15+
if(hash.containsKey(pat[j])==false){//一个模式字符不存在的话,添加key和value
16+
hash.put(pat[j],sArray[j]);
17+
18+
if(hash2.containsKey(sArray[j])==false){//考虑存在key和value重复的情况,使用另外一个map反向添加key和value。
19+
hash2.put(sArray[j],pat[j]);
20+
}else{//如果已经存在了
21+
if(hash2.get(sArray[j])!= pat[j]){
22+
flag=false;
23+
break;
24+
}
25+
}
26+
}else{//已经存在相应的key
27+
if(hash.get(pat[j])!= sArray[j]){
28+
flag=false;
29+
break;
30+
}
31+
}
32+
}
33+
34+
}
35+
36+
if(flag){//如果模式对应,添加到list集合中
37+
list.add(words[i]);
38+
}
39+
40+
}
41+
42+
return list;
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)