Skip to content

Instantly share code, notes, and snippets.

public class Solution {
public String convert(String s, int numRows) {
//String res = "";
if(s == null || s == "" || numRows == 1) return s;
//String[] eachRow = new String[numRows];
StringBuilder[] eachRow = new StringBuilder[numRows];
for (int i = 0; i < eachRow.length; i++) eachRow[i] = new StringBuilder();// Initialization!!!!
int direct = -1;
int j = 0;
for(int i = 0; i < s.length(); i++){
class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1) return 0;
int res = 0, i = 0, len = str.length(), sign = 1;
while (str.charAt(i) == ' ') i++;
if (str.charAt(i) == '-') {
sign = -1;
i++;
} else if (str.charAt(i) == '+') {
i++;
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) return "";
StringBuilder res = new StringBuilder();
Arrays.sort(strs);
char[] a = strs[0].toCharArray();
char[] b = strs[strs.length - 1].toCharArray();
for (int i = 0; i < a.length && i < b.length; i++) {
if (a[i] == b[i]) {
res.append(a[i]);
class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
if (source == null || target == null) {
return -1;
public class Solution {
public String countAndSay(int n) {
String baseStr = "1";
List<String> res = new ArrayList<String>();
res.add(baseStr);
while(n > 1){
int count = 1;
StringBuilder temp = new StringBuilder();
for(int i = 1; i < baseStr.length(); i++){
if(baseStr.charAt(i) == baseStr.charAt(i - 1)){
public class Solution {
public boolean isPowerOfTwo(int n) {
return (n > 0) && ( (n & (n - 1)) == 0);
}
}
class Solution {
public:
bool isPowerOfThree(int n) {
return fmod(log10(n)/log10(3), 1)==0;
}
};
public class Solution {
public String multiply(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
int[] res = new int[ len1 + len2];
for(int i = len1 - 1; i >= 0; i-- ){
for(int j = len2 - 1; j >=0; j-- ){
int temp = (num1.charAt(i) - '0') * (num2.charAt(j) - '0') + res[i + j + 1];
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {