Skip to content

Instantly share code, notes, and snippets.

@luoxiaoxun
luoxiaoxun / 剑指offer-从尾到头打印链表
Created July 11, 2013 10:42
题目描述: 输入一个链表,从尾到头打印链表每个节点的值。 输入: 每个输入文件仅包含一组测试样例。 每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点。第一行是链表第一个节点的值,依次类推。当输入到-1时代表链表输入完毕。-1本身不属于链表。 输出: 对应每个测试案例,以从尾到头的顺序输出链表每个节点的值,每个值占一行。 样例输入: 1 2 3 4 5 -1 样例输出: 5 4 3 2 1
#include<cstdio>
#include<stack>
using namespace std;
struct ListNode{
int val;
ListNode *next;
};
void addToTail(ListNode **head,ListNode **cur,int value){
ListNode *temp=new ListNode();
@luoxiaoxun
luoxiaoxun / 剑指offer-替换空格
Created July 11, 2013 10:40
题目描述: 请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 输入: 每个输入文件仅包含一组测试样例。 对于每组测试案例,输入一行代表要处理的字符串。 输出: 对应每个测试案例,出经过处理后的字符串。 样例输入:We Are Happy样例输出:We%20Are%20Happy
#include<stdio.h>
int main(){
char ret[1000000];
gets(ret);
int originalLen=0;
int numberOfBlank=0;
int i=0;
while(ret[i]!='\0'){
originalLen++;
@luoxiaoxun
luoxiaoxun / LeetCode-Median of Two Sorted Arrays
Last active December 19, 2015 13:58
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n))..
C++:
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int k=m+n;
if(k&0x1) return findKth(A,m,B,n,k/2+1);
else return (findKth(A,m,B,n,k/2)+findKth(A,m,B,n,k/2+1))/2;
}
@luoxiaoxun
luoxiaoxun / LeetCode-Longest Substring Without Repeating Characters
Created July 10, 2013 09:51
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
C++:
class Solution {
private:
bool canUse[256];
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
memset(canUse,true,sizeof(canUse));
int start=0;
@luoxiaoxun
luoxiaoxun / LeetCode-Add Two Numbers
Created July 10, 2013 09:14
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
C++:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
@luoxiaoxun
luoxiaoxun / LeetCode-Longest Palindromic Substring
Created July 10, 2013 08:50
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
C++:
class Solution {
public:
string longestPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s.size()==0) return "";
string ret;
string cur;
for(int i=0;i<s.size();i++){
@luoxiaoxun
luoxiaoxun / LeetCode-ZigZag Conversion
Created July 9, 2013 08:53
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string conver…
C++:
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;
string temp[nRows];
int idx=-1;
int step=1;
@luoxiaoxun
luoxiaoxun / LeetCode-Reverse Integer
Created July 9, 2013 08:02
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the …
C++:
class Solution {
public:
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int res=0;
while(x!=0){
res=res*10+x%10;
x=x/10;
@luoxiaoxun
luoxiaoxun / LeetCode-String to Integer (atoi)
Created July 9, 2013 07:20
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requiremen…
C++:
class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
bool negative=false;
while(*str==' ') str++;
if(*str=='-'){
negative=true;
@luoxiaoxun
luoxiaoxun / LeetCode-Palindrome Number
Created July 9, 2013 06:54
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know th…
C++:
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<0) return false;
int base=1;
while(x/base>=10) base *=10;
while(x){