Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces String concatenation with StringBuilder #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions company/adobe/MajorityElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public int majorityElement(int[] nums) {

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int current: nums) {
if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) {
return current;
} else if(map.containsKey(current)) {
map.put(current, map.get(current) + 1);
if (map.containsKey(current)) {
if (map.get(current) + 1 > nums.length / 2)
return current;
else
map.put(current, map.get(current) + 1);
} else {
map.put(current, 1);
}
Expand All @@ -21,4 +22,4 @@ public int majorityElement(int[] nums) {
//no majority element exists
return -1;
}
}
}
8 changes: 4 additions & 4 deletions company/apple/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}

8 changes: 4 additions & 4 deletions company/bloomberg/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}

8 changes: 4 additions & 4 deletions company/microsoft/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}

8 changes: 4 additions & 4 deletions company/snapchat/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}

8 changes: 4 additions & 4 deletions company/yelp/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public boolean isRotation(String s1, String s2) {
/*check that s1 and s2 are equal length and not empty */
if(len == s2.length() && len > 0) {
/* concatenate s1 and s1 within new buffer */
String s1s1 = s1 + s1;
String s1s1 = new StringBuilder().append(s1).append(s2).toString();
return isSubstring(s1s1, s2);
}

Expand Down
9 changes: 5 additions & 4 deletions leetcode/array/MajorityElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public int majorityElement(int[] nums) {

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int current: nums) {
if(map.containsKey(current) && map.get(current) + 1 > nums.length / 2) {
return current;
} else if(map.containsKey(current)) {
map.put(current, map.get(current) + 1);
if (map.containsKey(current)) {
if (map.get(current) + 1 > nums.length / 2)
return current;
else
map.put(current, map.get(current) + 1);
} else {
map.put(current, 1);
}
Expand Down
9 changes: 5 additions & 4 deletions leetcode/string/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
if(words.length==1) return s;
StringBuilder sb = new StringBuilder();
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
sb.append(words[i]).append(" ");
}

return result + words[0];
return sb.append(words[0]).toString();
}
}