File tree Expand file tree Collapse file tree 23 files changed +578
-1
lines changed Expand file tree Collapse file tree 23 files changed +578
-1
lines changed File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
1
+
2
+
3
+ private static int solution(int[] array) {
4
+ //从左右两边同时遍历,每次都舍弃最短边
5
+ int left = 0;
6
+ int right = array.length - 1;
7
+ int resultTemp = 0;
8
+ int result = 0;
9
+ while (left < right) {
10
+ if (array[left] <= array[right]) {
11
+ //左边柱子低于右边
12
+ //计算临时盛水容量
13
+ resultTemp = array[left] * (right - left);
14
+ //矮柱子向中间移动
15
+ left++;
16
+ } else if (array[left] >= array[right]) {
17
+ //左边柱子高于右边
18
+ //计算临时盛水容量
19
+ resultTemp = array[right] * (right - left);
20
+ //矮柱子向中间移动
21
+ right--;
22
+ }
23
+ //判断临时盛水容量
24
+ if (resultTemp > result) {
25
+ result = resultTemp;
26
+ }
27
+ }
28
+ return result;
29
+ }
Original file line number Diff line number Diff line change
1
+ private static boolean solution(String input) {
2
+ if (input == null || "".equals(input)) {
3
+ return true;
4
+ }
5
+ char[] inputCharArray = input.toCharArray();
6
+ //定义两个指针,一个头指针一个尾指针
7
+ int head = 0;
8
+ int tail = inputCharArray.length - 1;
9
+ //判断字母和数字字符的正则表达式
10
+ String regex = "^[0-9a-zA-Z]$";
11
+ while (head <= tail) {
12
+ String headStr = inputCharArray[head] + "";
13
+ String tailStr = inputCharArray[tail] + "";
14
+ if (headStr.matches(regex) && tailStr.matches(regex)) {
15
+ if (headStr.toLowerCase().equals(tailStr.toLowerCase())) {
16
+ //比较下一个字符
17
+ head++;
18
+ tail--;
19
+ } else {
20
+ //如果未匹配则直接返回
21
+ return false;
22
+ }
23
+ } else {
24
+ //如果比较的字符不为字母或数字则不比较
25
+ if (!headStr.matches(regex)) {
26
+ head++;
27
+ }
28
+ if (!tailStr.matches(regex)) {
29
+ tail--;
30
+ }
31
+ }
32
+ }
33
+ return true;
34
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isPalindrome(string s) {
4
+ int i=0;
5
+ int j=s.length()-1;
6
+
7
+ if(s.length() == 1)
8
+ return true;
9
+
10
+ while(i<j)
11
+ {
12
+ if(!isAlp(s[ i] ))
13
+ {
14
+ i++;
15
+ }
16
+ else if(!isAlp(s[ j] ))
17
+ {
18
+ j--;
19
+ }
20
+ else if(s[ i] !=s[ j] )
21
+ return false;
22
+ else
23
+ {
24
+ i++;
25
+ j--;
26
+ }
27
+ }
28
+
29
+ return true;
30
+ }
31
+
32
+ bool isAlp(char &ch)
33
+ {
34
+ if(ch >= 'A' && ch <= 'Z')
35
+ {
36
+ ch += 32;
37
+ return true;
38
+ }
39
+
40
+ if(ch >= 'a' && ch <= 'z')
41
+ return true;
42
+
43
+ if(ch >= '0' && ch <= '9')
44
+ return true;
45
+
46
+ return false;
47
+ }
48
+ };
Original file line number Diff line number Diff line change
1
+ ```
2
+ class Solution {
3
+ public:
4
+ void reverseWords(string &s) {
5
+ s.insert(0,1,' ');
6
+ reverse(s.begin(),s.end());
7
+ int len = s.size();
8
+ int now = 0;
9
+ int pos = 0;
10
+ int sum = 0;
11
+ while(now < len){
12
+
13
+ if(s[now]!=' '){
14
+ now++;
15
+ sum++;
16
+ }else{
17
+
18
+ if(sum != 0){
19
+ reverse(s.begin()+pos,s.begin()+pos+sum);
20
+ now++;
21
+ while(now<len&&s[now]==' ')s.erase(s.begin()+now);
22
+ pos=now;
23
+ sum=0;
24
+ }else{
25
+ s.erase(s.begin()+now);
26
+ }
27
+
28
+
29
+ }
30
+
31
+ }
32
+ s.pop_back();
33
+
34
+ }
35
+ };
36
+ ```
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public String reverseWords(String s) {
3
+ if(s==null || s.length()==0){
4
+ return"";
5
+ }
6
+ String[ ] array=s.split(" ");
7
+ String str="";
8
+ for(int i = array.length-1;i>=0;--i){
9
+ if(!array[ i] equals("")){
10
+ if(str.length()>0){
11
+ str+=" ";
12
+ }
13
+ str+=array[ i] ;
14
+ }
15
+ }
16
+ return str;
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ private static String solution(String input) {
2
+ if ("".equals(input) || input == null) {
3
+ return "";
4
+ }
5
+ String[] splits = input.split(" ");
6
+ //反转字符串中的单词,从尾到头遍历
7
+ //单词之间以空格分离
8
+ String result = " ";
9
+ for (int i = splits.length - 1; i >= 0; i--) {
10
+ if (!splits[i].equals("")) {
11
+ //单词不为空
12
+ result = result + splits[i];
13
+ }
14
+ }
15
+ return result;
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ void reverseWords(string &s) {
4
+ string rw;
5
+ for (int i = s.length()-1; i >= 0; )
6
+ {
7
+ while (i >= 0 && s[ i] == ' ') i--;
8
+ if (i < 0) break;
9
+ if (!rw.empty()) rw.push_back(' ');
10
+ string t;
11
+ while (i >= 0 && s[ i] != ' ') t.push_back(s[ i--] ); //开始逆向输出
12
+ reverse(t.begin(), t.end());
13
+ rw.append(t);
14
+ }
15
+ s=rw;
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ public class TestReverseWords {
2
+ public static String reverseWords(String s){
3
+ if(s==null||s.length()==0){
4
+ return "";
5
+ }
6
+ String[ ] array = s.split(" ");
7
+ String str = "";
8
+ for(int i=array.length-1;i>=0;i--){
9
+ if(!array[ i] .equals("")){
10
+ if(str.length()>0){
11
+ str+=" ";
12
+ }
13
+ str += array[ i] ;
14
+ }
15
+ }
16
+ return str;
17
+ }
18
+
19
+ public static void main(String[ ] args) {
20
+ String s = "hello world!";
21
+ System.out.println(reverseWords(s));
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments