// find anagrams
using System;
using System.IO;
class MyClass {
public void check_anagrams(string[] firstWords, string[] secondWords) {
if (firstWords == null ||
secondWords == null ||
firstWords.Length != secondWords.Length ||
firstWords.Length == 0) return;
for (int i = 0; i < firstWords.Length; i++)
{
char[] left = firstWords[i].ToCharArray();
char[] right = secondWords[i].ToCharArray();
Array.Sort<char>(left);
Array.Sort<char>(right);
if (new string(left) == new string(right))
Console.WriteLine ("1");
else
Console.WriteLine("0");
}
}
}
// find matching braces pairs
using System;
using System.Collections;
class MyClass {
public void check_braces(string[] expressions) {
foreach (var expression in expressions)
{
Stack s = new Stack();
char[] tokens = expression.ToCharArray();
int output = 1;
foreach (var token in tokens)
{
switch (token)
{
case '(':
s.Push(token);
break;
case '{':
s.Push(token);
break;
case '[':
s.Push(token);
break;
case ')':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '(') output = 0;
continue;}
else output = 0;
case '}':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '{') output = 0;
continue;}
else output = 0;
case ']':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '[') output = 0;
continue;}
else output = 0;
}
}
Console.WriteLine("{0}", output);
}
}
}
find the minimum integer X in a given unordered integer array such that each element needs to be adjusted by at most X to make the array sorted.
static void Main(string[] args)
{
int[] v = new int[] { 5, 4, 1, 2, 3, 8 };
hill(v);
}
static public void hill(int[] v)
{
if (v == null || v.Length == 0) return;
int max = 0;
int min = 0;
int diff = max - min;
for (int i = 1; i < v.Length; i++)
{
if (v[i] < v[i - 1])
{
if (v[i - 1] > max) { max = v[i - 1]; min = v[i]; }
if (v[i] < min) { min = v[i]; }
}
else
{
if (max - min > diff)
diff = max - min;
}
}
Console.WriteLine("{0}", diff);
}
using System;
using System.IO;
class MyClass {
public void check_anagrams(string[] firstWords, string[] secondWords) {
if (firstWords == null ||
secondWords == null ||
firstWords.Length != secondWords.Length ||
firstWords.Length == 0) return;
for (int i = 0; i < firstWords.Length; i++)
{
char[] left = firstWords[i].ToCharArray();
char[] right = secondWords[i].ToCharArray();
Array.Sort<char>(left);
Array.Sort<char>(right);
if (new string(left) == new string(right))
Console.WriteLine ("1");
else
Console.WriteLine("0");
}
}
}
// find matching braces pairs
using System;
using System.Collections;
class MyClass {
public void check_braces(string[] expressions) {
foreach (var expression in expressions)
{
Stack s = new Stack();
char[] tokens = expression.ToCharArray();
int output = 1;
foreach (var token in tokens)
{
switch (token)
{
case '(':
s.Push(token);
break;
case '{':
s.Push(token);
break;
case '[':
s.Push(token);
break;
case ')':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '(') output = 0;
continue;}
else output = 0;
case '}':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '{') output = 0;
continue;}
else output = 0;
case ']':
if (s.Count > 0){
char c = (char)s.Pop();
if (c != '[') output = 0;
continue;}
else output = 0;
}
}
Console.WriteLine("{0}", output);
}
}
}
find the minimum integer X in a given unordered integer array such that each element needs to be adjusted by at most X to make the array sorted.
static void Main(string[] args)
{
int[] v = new int[] { 5, 4, 1, 2, 3, 8 };
hill(v);
}
static public void hill(int[] v)
{
if (v == null || v.Length == 0) return;
int max = 0;
int min = 0;
int diff = max - min;
for (int i = 1; i < v.Length; i++)
{
if (v[i] < v[i - 1])
{
if (v[i - 1] > max) { max = v[i - 1]; min = v[i]; }
if (v[i] < min) { min = v[i]; }
}
else
{
if (max - min > diff)
diff = max - min;
}
}
Console.WriteLine("{0}", diff);
}
No comments:
Post a Comment