Skip to content

Instantly share code, notes, and snippets.

// {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}
// pointer i sent to first 0
// pointer j from the end set to first non zero
//{1, 0, 1,0,1, 3, 0}
// i = 0 j = 3 --> i = 1, j = 5
// a[i] == 0, swap i, j i++ {1, 3, 1, 0, 1, 0, 0} j--(if it's zero --) i++
// non zero i++
public void (ArrayList<Integer> list) {
if (list.size() == 0|| list == null) {
return;
// 1.5 Implement a method to perform basic string compression using the counts
// of repeated characters. For example, the string aabcccccaaa would become
// a2blc5a3. If the "compressed" string would not become smaller than the original
// string, your method should return the original string.
// time complexity: O(n)
// space complexity: O(n)
public class Solution {
public String compressed(String s) {
int len = s.length();
StringBuffer buff = new StringBuffer();
@jocelynzz
jocelynzz / CC 1.4
Last active August 29, 2015 14:18
CC
// 1.4 Write a method to replace all spaces in a string with'%20'. You may assume that
// the string has sufficient space at the end of the string to hold the additional
// characters, and that you are given the "true" length of the string. (Note: if implementing
// in Java, please use a character array so that you can perform this operation
// in place.)
//note: string is immutable
// EXAMPLE
// Input: "Mr John Smith"
// Output: "Mr%20Dohn%20Smith"
//time complexity O(n)
public class Solution1 {
public boolean isPermuataion(String string1, String string2) {
//abcde
//edcba
//time complexity: O(n)
//space complexity: O(n)
//note: you can also use char[] content = string1.toCharArray();
//then sort(content)
int len1 = string1.length();
int len2 = string2.length();
public class solution {
public void reverse(String s) {
int len = s.length(); //len = 5
//abcde
//ebcda
//edcba
if (s > 0) {
for (int i = 0; i < len/2; i++ ) {
char temp = s.charAt(i); //i = 0 temp = a
int pos = len - i - 1; //pos = 4
public class Solution {
public boolean UniqueString(String s) {
//if char has appeared before
//abcdedfe
//time complexity n^2
int len = s.length();
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (s.charAt(i) == s.charAt(j) {
return false;