### 分类：LeetCode

[LeetCode]Roman to Integer

## [LeetCode]Roman to Integer

```    public class Solution
{
public int RomanToInt(string s)
{
int l = s.Length;
int tot = 0;
for (int i = 0; i < l; i++)
{
if (s[i] == 'I')
{
if (i + 1 < l && s[i + 1] == 'V')
{
tot += 4;
i++;
}
else if (i + 1 < l && s[i + 1] == 'X')
{
tot += 9;
i++;
}
else
{
tot += 1;
}
}
else if (s[i] == 'X')
{
if (i + 1 < l && s[i + 1] == 'L')
{
tot += 40;
i++;
}
else if (i + 1 < l && s[i + 1] == 'C')
{
tot += 90;
i++;
}
else
{
tot += 10;
}
}
else if (s[i] == 'C')
{
if (i + 1 < l && s[i + 1] == 'D')
{
tot += 400;
i++;
}
else if (i + 1 < l && s[i + 1] == 'M')
{
tot += 900;
i++;
}
else
{
tot += 100;
}
}
else if (s[i] == 'V') tot += 5;
else if (s[i] == 'L') tot += 50;
else if (s[i] == 'D') tot += 500;
else if (s[i] == 'M') tot += 1000;
}
}
}

```
[LeetCode]Intersection of Two Arrays

## [LeetCode]Intersection of Two Arrays

```    public class Solution
{
public int[] Intersection(int[] nums1, int[] nums2)
{
if (nums1.Length == 0 || nums2.Length == 0)
{
int[] emptyArray = new int[0];
return emptyArray;
}
List<int> listNum = new List<int>();
foreach (var i in nums2)
{
if (nums1.Contains(i) && !listNum.Contains(i))
{
}
}
return listNum.ToArray();
}
}
```
[LeetCode]Find All Duplicates in an Array

## [LeetCode]Find All Duplicates in an Array

```    public class Solution
{
public IList<int> FindDuplicates(int[] nums)
{
IList<int> ans = new List<int>();
for (int i = 0; i < nums.Length; i++)
{
int index = Math.Abs(nums[i]) - 1;
if (nums[index] < 0)
nums[index] = -nums[index];
}
return ans;
}
}
```
[LeetCode]Longest Palindrome

## [LeetCode]Longest Palindrome

```    public class Solution
{
public int LongestPalindrome(string s)
{
Dictionary<char, int> countChar = new Dictionary<char, int>();
foreach (var ch in s)
{
if (countChar.ContainsKey(ch))
countChar[ch]++;
else
}
int numOdd = 0;
int countOdd = 0;
int countEven = 0;
int hasOdd = 0;
foreach (var temp in countChar)
{
if (temp.Value % 2 == 0)
{
countEven += temp.Value;
}
else
{
numOdd++;
countOdd += temp.Value;
hasOdd = 1;
}
}
return countEven + countOdd - numOdd + hasOdd;
}
}
```
[LeetCode]Largest Number

## [LeetCode]Largest Number

```    public class Solution
{
public string LargestNumber(int[] nums)
{
List<string> Nums = new List<string>();
foreach (var temp in nums)
{
}
Nums.Sort(Compare);
string RetData = "";
foreach (var temp in Nums)
{
RetData = string.Concat(RetData, temp);
}
int ZeroCount = 0;
for (ZeroCount = 0; ZeroCount < RetData.Length; ZeroCount++)
{
if (RetData[ZeroCount] == '0') continue;
if (RetData[ZeroCount] != '0') break;
}
RetData = RetData.Remove(0, ZeroCount);
if (RetData == "") RetData = "0";
return RetData;
}
private int Compare(string Comp1, string Comp2)
{
return string.Compare(string.Concat(Comp2, Comp1), string.Concat(Comp1, Comp2));
}
}
```
[LeetCode]Encode and Decode TinyURL

## [LeetCode]Encode and Decode TinyURL

dictionary就可以了

```	public class Codec
{
Dictionary<string,string> dictUrl = new Dictionary<string,string> ();
List<char> charSet = new List<char> ();

public Codec ()
{
for (int i = 65; i <= 90; i++) {
}
for (int i = 97; i <= 122; i++) {
}
for (int i = 48; i <= 57; i++) {
}
}
//EUREKA
private string genUrl ()
{
Random rand = new Random ();
string subUrl = "";
for (int i = 0; i < 6; i++) {
subUrl += charSet [rand.Next () % (charSet.Count)];
}
return "http://tinyurl.com/" + subUrl;
}
// Encodes a URL to a shortened URL
public string encode (string longUrl)
{
if (dictUrl.ContainsKey (longUrl))
return dictUrl [longUrl];
while (true) {
string shortUrl = genUrl ();
if (dictUrl.ContainsValue (shortUrl))
continue;
else {
break;
}
}
return dictUrl [longUrl];
}

// Decodes a shortened URL to its original URL.
public string decode (string shortUrl)
{
if (!dictUrl.ContainsValue (shortUrl))
return "";
foreach (var temp in dictUrl) {
if (temp.Value == shortUrl)
return temp.Key;
}
return "";
}
}
```
[LeetCode]Remove Element

## [LeetCode]Remove Element

```class Solution {
public:
int removeElement(vector<int> &nums, int val) {
int curr = 0, pos = 0, len = nums.size();
for (curr = 0; curr < len; curr++) {
if (nums[curr] == val) {
continue;
}
nums[pos] = nums[curr];
pos++;
}
return pos;
}
};
```
[LeetCode]Remove Duplicates from Sorted Array

## [LeetCode]Remove Duplicates from Sorted Array

```public class Solution {
public int removeDuplicates(int[] nums) {
Set<Integer> set=new HashSet<>();
List<Integer> list=new ArrayList<>();
for (Integer integer : nums) {
}
for(int i=0;i<list.size();i++)
{
nums[i]=list.get(i);
}
return set.size();
}
}
```
[LeetCode]Swap Nodes in Pairs

## [LeetCode]Swap Nodes in Pairs

```//JAVA
public class Solution {
ListNode prev = null;
boolean flag = true;
while (flag) {
if (currNode.next == null)
break;
if (currNode.next.next == null)
flag = false;
ListNode tempNode = currNode.next;

currNode.next = tempNode.next;
tempNode.next = currNode;
if (prev != null)
prev.next = tempNode;
prev = currNode;
currNode = currNode.next;
}
}
}
```
[LeetCode]146 LRU Cache

## [LeetCode]146 LRU Cache

```    public class LRUCache
{
private int Length;
private Node Tail;
private int Capacity;
public LRUCache(int capacity)
{
Length = 0;
Tail = new Node();
Capacity = capacity;
}

public int Get(int key)
{
if (Length == 0) return -1;
while (FindNode != null)
{
if (FindNode.Key == key)
{
FindNode.Prev.Next = FindNode.Next;
FindNode.Next.Prev = FindNode.Prev;
return FindNode.Value;
}
FindNode = FindNode.Next;
}
return -1;
}

public void Put(int key, int value)
{
if (Get(key) != -1)
{
while (FindNode != null)
{
if (FindNode.Key == key)
{
FindNode.Value = value;
return;
}
FindNode = FindNode.Next;
}
}
Node NewNode = new Node(key, value);
if (Length < Capacity)
{
//Node NewNode = new Node(key, value);
Length++;
return;
}
if (Capacity == 1)
{
//Node NewNode = new Node(key, value);
Tail.Prev = NewNode;
NewNode.Next = Tail;
Length++;
return;
}
Node OldNode = Tail.Prev;
Tail.Prev = Tail.Prev.Prev;
Tail.Prev.Next = Tail;
OldNode = null;