File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int majorityElement (int [] nums ) {
3+ // 初始放入第一个元素并开始计数
4+ int maj = nums [0 ];
5+ int count = 1 ;
6+ // 第二个元素开始比较
7+ for (int i = 1 ; i < nums .length ; i ++) {
8+ if (maj == nums [i ])
9+ count ++;
10+ else {
11+ count --;
12+ if (count == 0 ) {
13+ maj = nums [i + 1 ];
14+ }
15+ }
16+ }
17+ return maj ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public String longestWord (String [] words ) {
3+ // 对字符串数组进行排序
4+ Arrays .sort (words );
5+
6+ Set <String > set = new HashSet <>();
7+ String result = new String ();
8+ for (String str : words ) {
9+ if (set .contains (str .substring (0 , str .length () - 1 )) || str .length () == 1 ) {
10+ // 保存更长的哪一个字符串
11+ result = str .length () > result .length () ? str : result ;
12+ set .add (str );
13+ }
14+ }
15+ return result ;
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments