内容:
1、寻找数组的中心索引:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] temp = { 1, 7, 3, 6, 5, 6 };
Solution so = new Solution();
Console.WriteLine(so.PivotIndex(temp));
}
}
public class Solution
{
public int PivotIndex(int[] nums)
{
int total = 0, sum = 0;
foreach (int Value in nums)
{
total += Value;
}
for (int i = 0; i < nums.Length; ++i)
{
if (sum * 2 + nums[i] == total)
return i;
sum += nums[i];
}
return -1;
}
}
}
2、搜索插入位置:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[] temp = { 1, 2,5,7,8,9 };
Solution so = new Solution();
Console.WriteLine(so.SearchInsert(temp,5));
}
}
public class Solution
{
public int SearchInsert(int[] nums,int target)
{
int left = 0, right = nums.Length-1, mid;
while (left <=right)
{
mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
}
}
3.旋转矩阵:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[,] temp = new int[3, 3] { {1,2,3 },{4,5,6 },{7,8,9 } };
Solution so = new Solution();
so.rotate(temp);
for (int i = 0; i < temp.GetLength(0); ++i)
{
for (int j = 0; j < temp.GetLength(1); ++j)
{
Console.Write(temp[i,j]);
}
Console.WriteLine();
}
}
}
public class Solution
{
public void rotate(int[,] matrix)
{
int n = matrix.GetLength(0);
for (int i = 0; i < n / 2; ++i)
{
for (int j = 0; j < (n + 1) / 2; ++j)
{
int temp = matrix[i,j];
matrix[i,j] = matrix[n - j - 1,i];
matrix[n - j - 1,i] = matrix[n - i - 1,n - j - 1];
matrix[n - i - 1,n - j - 1] = matrix[j,n - i - 1];
matrix[j,n - i - 1] = temp;
}
}
}
}
}
4.零矩阵:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int[,] temp = new int[3, 3] { {0,0,3 },{4,5,6 },{7,8,9 } };
Solution so = new Solution();
so.SetZeroes(temp);
for (int i = 0; i < temp.GetLength(0); ++i)
{
for (int j = 0; j < temp.GetLength(1); ++j)
{
Console.Write(temp[i,j]);
}
Console.WriteLine();
}
}
}
public class Solution
{
public void SetZeroes(int[,] matrix)
{
int[,] temp = new int[matrix.Length, 2];
int countzero = 0;
for (int i=0;i<matrix.GetLength(0);++i) {
for (int j=0;j<matrix.GetLength(1);++j) {
if (matrix[i, j] == 0) { temp[countzero, 0] = i; temp[countzero, 1] = j;countzero++; }
}
}
for(int i = 0; i < countzero; ++i)
{
int a = temp[i, 0], b = temp[i, 1];
for (int c = 0; c < matrix.GetLength(1); ++c) { matrix[a, c] = 0; }
for (int c = 0; c < matrix.GetLength(0); ++c) { matrix[c, b] = 0; }
}
}
}
}
5.最长公共前缀:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[] temp;
temp = new string[] { “flower”, “flow”, “flight” };
Solution so = new Solution();
string ot = so.longestCommonPrefix(temp);
Console.WriteLine(ot);
}
}
public class Solution
{
public String longestCommonPrefix(String[] strs)
{
if (strs null||strs.Length 0)
{
return “”;
}
int length = strs[0].Length;
int count = strs.Length;
for (int i = 0; i < length; i++)
{
char c = strs[0][i];
for (int j = 1; j < count; j++)
{
if (i == strs[j].Length || strs[j][i] != c)
{
return strs[0].Substring(0, i);
}
}
}
return strs[0];
}
}
}
6.翻转字符串里的单词
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string str1 = "qwwe rere re re re r3w “;
Solution so = new Solution();
string a = so.strreverse(str1);
Console.WriteLine(a);
}
}
public class Solution
{
public String strreverse(String strs)
{
if (strs == null || strs.Length == 0) { return “”; }
string str2 = strs.Trim();
string[] str3 = str2.Split();
int len = str3.Length;
string[] str4 = new string[len];
for(int i = 0; i < len; i++)
{
str4[i] = str3[len - 1 - i];
}
str2 = string.Join(” ",str4);
return str2;
}
}
}
学习重点:掌握数组和字符串的相关知识,并且能够合理使用算法使时间和空间复杂度最小,以减小运算。
学习心得:设计算法要积极寻找其中的规律,再用来简化编程算法。