Skip to content

Instantly share code, notes, and snippets.

public class Solution {
public boolean isPalindrome(String str) {
// Start typing your Java solution below
// DO NOT write main() function
str=str.toLowerCase();
int start=0, end=str.length()-1;
while(start<=end)
{
if(start<=end && outOfRange(str.charAt(start)))
@guolinaileen
guolinaileen / Binary Tree Maximum Path Sum.java
Last active December 11, 2015 14:49
Java doesn't allow to modify passed-in parameters. For this problem, we need to keep track two things during recursions: the max subpath sum and the current max sum for a given node. One way is to let the helper method return an array of two items; Another way is to pass in the current max as an array of one item.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
#include <algorithm>
class Solution {
public:
class Pair
{
public:
int index;
int val;
@guolinaileen
guolinaileen / Add Two Numbers.cpp
Last active May 22, 2024 07:53
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
@guolinaileen
guolinaileen / ZigZag Conversion.cpp
Last active December 11, 2015 17:09
careful about the divisor
class Solution {
public:
string convert(string s, int nRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(nRows<=1) return s;
int length=s.length();
int numberItems=2*(nRows-1);
int zones=length/numberItems+(length%numberItems==0? 0: 1);
@guolinaileen
guolinaileen / gist:4640399
Last active December 11, 2015 18:19
bugs still exist. need to figure out a better method.
public class Solution {
public boolean isMatch(String s, String p) {
if(s!="" && p=="") return false;
char [] sArray=s.toCharArray();
int sLength=sArray.length;
char [] pArray=p.toCharArray();
int pLength=pArray.length;
int i=0, j=0;
while(i<sLength && j<pLength){
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string result="";
if(strs.size()==0) return result;
result=strs[0];
for(int i=1; i<strs.size(); i++)
{
class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string result="";
if(num>=1000) result+=string(num/1000, 'M');
num=num%1000; result+=stringHelper(num/100, "C", "D", "M");
num=num%100; result+=stringHelper(num/10, "X", "L", "C");
num=num%10; result+=stringHelper(num, "I", "V", "X");
@guolinaileen
guolinaileen / Roman to Integer.cpp
Last active December 11, 2015 18:59
'M', 1000; 'D', 500; 'C', 100 ; 'L', 50; 'X',10; 'V', 5; 'I', 1 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV 但是,左减时不可跨越一个位数。比如,99不可以用IC()表示,而是用XCIX()表示。(等同于阿拉伯数字每位数字分别表示。) 此外,左减数字必须为一位,比如8写成VIII,而非IIX。 同理,右加数字不可超过三位,比如14写成XIV,而非XIIII。(见下方“数码限制”一项。) 加线乘千: 在罗马数字的上方加上一条横线或者加上下标的Ⅿ,表示将这个数…
class Solution {
public:
int romanToInt(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s.length()==0) return 0;
map<char, int> mp;
mp['M']=1000;
mp['D']=500;
mp['C']=100;
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector< int > > result;
if(num.size()<3) return result;
sort(num.begin(), num.end());
for(int i=0; i<=num.size()-3; i++)
{